..._vue elementui 管理系统">
赞
踩
前端界面
-
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- <script src="js/vue.js"></script>
- <script src="js/axios-0.18.0.js"></script>
- <script src="element-ui/lib/index.js"></script>
- <link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
- </head>
- <body>
-
- <style>
- .el-table .warning-row {
- background: oldlace;
- }
-
- .el-table .success-row {
- background: #f0f9eb;
- }
- </style>
-
- <div id="app">
-
- <!-- 搜索框-->
- <el-form :inline="true" :model="formInline" class="demo-form-inline">
-
- <el-form-item label="当前状态">
- <el-select v-model="formInline.status" placeholder="当前状态">
- <el-option label="开启" value="1"></el-option>
- <el-option label="关闭" value="0"></el-option>
- </el-select>
- </el-form-item>
-
- <el-form-item label="品牌名称">
- <el-input v-model="formInline.brandName" clearable @clear="selSubmit" placeholder="品牌名称"></el-input>
- </el-form-item>
-
- <el-form-item label="企业名称">
- <el-input v-model="formInline.companyName" clearable @clear="selSubmit" placeholder="企业名称"></el-input>
- </el-form-item>
-
- <el-form-item>
- <el-button type="primary" @click="selSubmit">查询</el-button>
- </el-form-item>
- </el-form>
-
- <!-- 按钮-->
- <el-row>
- <el-button type="danger" plain @click="delAll" @click="delAll">批量删除</el-button>
- <el-button type="primary" plain @click="dialogVisible = true">新增</el-button>
- </el-row>
-
- <!-- 新增表单-->
- <el-dialog title="新增企业" :visible.sync="dialogVisible" width="30%">
-
-
- <el-form ref="form" :model="form" label-width="80px">
- <el-form-item label="品牌名称">
- <el-input v-model="form.brandName"></el-input>
- </el-form-item>
-
- <el-form-item label="企业名称">
- <el-input v-model="form.companyName"></el-input>
- </el-form-item>
-
- <el-form-item label="排序">
- <el-input v-model="form.ordered"></el-input>
- </el-form-item>
-
- <el-form-item label="备注">
- <el-input type="textarea" v-model="form.description"></el-input>
- </el-form-item>
-
- <el-form-item label="状态">
- <el-switch v-model="form.status" active-value="1" inactive-value="0"></el-switch>
- </el-form-item>
-
-
- <el-form-item>
- <el-button type="primary" @click="onSubmit">提交</el-button>
- <el-button @click="dialogVisible = false">取消</el-button>
- </el-form-item>
- </el-form>
-
-
- </span>
- </el-dialog>
-
- <!-- 修改表单-->
- <el-dialog title="修改企业信息" :visible.sync="vie" width="30%">
-
-
- <el-form ref="from" :model="from" label-width="80px">
- <el-form-item label="品牌名称">
- <el-input v-model="from.brandName"></el-input>
- </el-form-item>
-
- <el-form-item label="企业名称">
- <el-input v-model="from.companyName"></el-input>
- </el-form-item>
-
- <el-form-item label="排序">
- <el-input v-model="from.ordered"></el-input>
- </el-form-item>
-
- <el-form-item label="备注">
- <el-input type="textarea" v-model="from.description"></el-input>
- </el-form-item>
-
- <el-form-item label="状态">
- <el-switch v-model="from.status" active-value="1" inactive-value="0"></el-switch>
- </el-form-item>
-
-
- <el-form-item >
- <el-button type="primary" @click="update">提交</el-button>
- <el-button @click="vie = false">取消</el-button>
- </el-form-item>
- </el-form>
-
-
- </span>
- </el-dialog>
-
-
- <!-- 表格-->
- <template >
- <el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName"
- @selection-change="handleSelectionChange">
-
- <!-- 复选框-->
- <el-table-column
- type="selection"
- width="55">
- </el-table-column>
-
- <!-- id-->
- <el-table-column
- type="index">
- </el-table-column>
-
- <el-table-column prop="brandName" label="品牌名称" width="180" align="center">
- </el-table-column>
-
-
- <el-table-column prop="companyName" label="企业名称" width="180" align="center">
- </el-table-column>
-
-
- <el-table-column prop="ordered" label="排序" align="center">
- </el-table-column>
-
- <el-table-column prop="status" label="状态" align="center">
- </el-table-column>
-
-
- <el-table-column prop="address" label="操作" align="center">
- <!-- slot-scope="scope" 用来获取元素id-->
- <template slot-scope="scope">
- <el-button
- size="mini"
- @click="open(scope.$index, scope.row)">修改</el-button>
- <el-button
- size="mini"
- type="danger"
- @click="del(scope.$index, scope.row)">删除</el-button>
- </template>
-
- </el-table-column>
- </el-table>
- </template>
-
- <!-- 分页-->
- <div class="block">
-
- <el-pagination
- @size-change="handleSizeChange"
- @current-change="handleCurrentChange"
- :current-page="currentPage"
- :page-sizes="pageSizes"
- :page-size="pageSize"
- layout="total, sizes, prev, pager, next, jumper"
- :total="total">
- </el-pagination>
- </div>
- </div>
-
- <script>
- new Vue({
- el: "#app",
- // 数据区
- data() {
- return {
- //模型区
-
-
- //分页
- //当前页
- currentPage:2,
-
- //每页显示条数
- pageSizes: [5, 10, 20, 30],
-
- //每页显示条数初始化值
- pageSize: 5,
-
- //总条数
- total: 1,
-
-
- //搜索框模型
- formInline: {
- brandName: '',
- companyName: '',
- status: "",
- },
-
- //新增表单模型
- form: {
- brandName: "",
- companyName: "",
- ordered: "",
- description: "",
- status: ""
- },
- // 修改
- from: {
- brandName: "",
- companyName: "",
- ordered: "",
- description: "",
- status: "",
- },
-
- //对话框
- dialogVisible: false,
-
- // 修改对话框的显示
- vie:false,
-
-
- tableData: [],
-
- //删除复选
- multipleSelection: [],
- //删除复选id
- ids:[],
-
-
- }
- },
-
-
-
-
-
-
-
-
-
-
-
- //方法区
- methods: {
- // 搜索
- // 后端返回 数据+总条数
-
- selSubmit() {
-
- console.log(this.formInline)
- axios({
- method:"post",
- url:"http://localhost:8080/elementdemo1/SelServlet?currentPage="+this.currentPage+"&pageSize="+this.pageSize,
- data:this.formInline
- }).then(resp=>{
- this.tableData=resp.data.list
- this.total=resp.data.total
- })
- },
-
- // 开启修改框并 传递数据
- open(index, row){
- this.vie=true
- this.from=row
- },
- // 修改
- update(index, row){
- this.vie=false
- axios({
- method: "post",
- url: "http://localhost:8080/elementdemo1/UpdateServlet",
- data:this.from
- }).then(resp =>{
- if(resp.data){
-
- //1,关闭对话框
- this.vie=false
- //2,弹出提示
- this.$message({
- message: '修改成功',
- type: 'success'
- });
- //3,重新查询
- this.handleCurrentChange(this.currentPage)
- }else {
- //添加失败
- this.$message.error('修改失败');
- }
- })
-
-
-
- },
-
- // 删除
- del(index, row){
- axios({
- method:"get",
- url:"http://localhost:8080/elementdemo1/DelServlet?id="+row.id,
- }).then(resp=>{
- if(resp.data){
- //1弹出提示
- this.$message({
- message: '删除成功',
- type: 'success'
- });
- //2,重新查询
- this.handleCurrentChange(this.currentPage)
- }else {
- //删除失败
- this.$message.error('删除失败');
- }
- })
- },
- tableRowClassName({row, rowIndex}) {
- if (rowIndex === 1) {
- return 'warning-row';
- } else if (rowIndex === 3) {
- return 'success-row';
- }
- return '';
- },
-
-
- //批量删除方法
- delAll(){
- //创建一个模型 id
- for(let i=0;i<this.multipleSelection.length;i++){
- var id=this.multipleSelection[i].id
- this.ids[i]=id
- }
-
- //发送ajax
- axios({
- method:"post",
- url:"http://localhost:8080/elementdemo1/DelAllServlet",
- data:this.ids
- }).then(resp =>{
- if(resp.data){
- //1弹出提示
- this.$message({
- message: '删除成功',
- type: 'success'
- });
- //2,重新查询
- this.handleCurrentChange(this.currentPage)
- }else {
- //删除失败
- this.$message.error('删除失败');
- }
- })
- },
- //删除复选
- handleSelectionChange(val) {
- this.multipleSelection = val;
- },
-
- //新增表单提交
- onSubmit() {
-
- axios({
- method: "post",
- url: "http://localhost:8080/elementdemo1/AddServlet",
- data:this.form
- }).then(resp =>{
- if(resp.data){
- //添加成功
- //1,关闭对话框
- this.dialogVisible=false
- //2,弹出提示
- this.$message({
- message: '添加成功',
- type: 'success'
- });
- //3,重新查询
- this.handleCurrentChange(this.currentPage)
- }else {
- //添加失败
- this.$message.error('添加失败');
- }
- })
-
- },
-
-
-
- //每页显示条数改变
- handleSizeChange(val) {
- this.pageSize=val
-
- axios({
- method:"get",
- url:"http://localhost:8080/elementdemo1/PageServlet?currentPage="+this.currentPage+"&pageSize="+this.pageSize
- }).then(resp =>{
- this.tableData=resp.data.list
- this.total=resp.data.total
- })
-
- },
-
-
- //当前页改变触发
- handleCurrentChange(val) {
- this.currentPage= val
-
- axios({
- method:"get",
- url:"http://localhost:8080/elementdemo1/PageServlet?currentPage="+this.currentPage+"&pageSize="+this.pageSize
- }).then(resp =>{
- this.tableData=resp.data.list
- this.total=resp.data.total
- })
- },
-
- SelAll(){
- axios({
- method:"get",
- url:"http://localhost:8080/elementdemo1/SelAllServlet"
- }).then(resp =>{
- this.tableData=resp.data
- })
- }
-
-
-
- },
-
- //钩子
- mounted(){
- //1=当前页
- this.handleCurrentChange(1)
-
- }
- })
- </script>
- </body>
- </html>
查询所有功能 web层 service层 dao层
- package web;
-
- import com.alibaba.fastjson.JSON;
- import po.Brand;
- import service.SelAllService;
-
- import javax.servlet.*;
- import javax.servlet.http.*;
- import javax.servlet.annotation.*;
- import java.io.IOException;
- import java.util.List;
-
- @WebServlet("/SelAllServlet")
- public class SelAllServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- //掉用下一层
- SelAllService selService=new SelAllService();
- // 调用方法 并接受返回值
- List<Brand> sel = selService.sel();
- // 转成json字符串
- String s = JSON.toJSONString(sel);
- // 设置响应头
- response.setContentType("text/json;charset=utf-8");
- // 返回数据
- response.getWriter().write(s);
-
-
- }
-
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- request.setCharacterEncoding("utf-8");//手动解码
- this.doGet(request, response);//把数据传给get
- }
- }
- package service;
-
- import dao.SelAllDao;
- import po.Brand;
-
- import java.util.List;
-
- public class SelAllService {
- public List<Brand> sel() {
- // 不用进行逻辑处理 直接调用dao
- SelAllDao selDao=new SelAllDao();
- return selDao.sel();
- }
- }
- package dao;
-
- import po.Brand;
- import util.JdbcUtil;
-
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
-
- public class SelAllDao {
- public List<Brand> sel() {
- // 连接数据库
- Connection conn= JdbcUtil.getconn();
- ResultSet re=null;
- PreparedStatement pr=null;
- List<Brand> list=new ArrayList<>();
-
- //查询语句
- String sql="select*from tb_brand";
- try {
- pr=conn.prepareStatement(sql);
- re=pr.executeQuery();
- while (re.next()){
- Brand brand=new Brand();
- brand.setId(re.getInt("id"));
- brand.setBrandName(re.getString("brand_name"));
- brand.setCompanyName(re.getString("company_name"));
- brand.setOrdered(re.getInt("ordered"));
- brand.setDescription(re.getString("description"));
- brand.setStatus(re.getInt("status"));
-
- // 将查找到的数据添加到list集合
- list.add(brand);
-
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- // 关流
- JdbcUtil.getclose(pr, conn, re);
- // 返回list集合
- return list;
-
-
- }
- }
分页功能 查询总条数功能 web层 service层 dao层
- package web;
-
- import com.alibaba.fastjson.JSON;
- import po.Brand;
- import po.PageBrand;
- import service.PageService;
-
- import javax.servlet.*;
- import javax.servlet.http.*;
- import javax.servlet.annotation.*;
- import java.io.IOException;
-
- @WebServlet("/PageServlet")
- public class PageServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
- // 接收参数
- String currentPage = request.getParameter("currentPage");
- String pageSize = request.getParameter("pageSize");
- // 转成int类型
- int currentPages = Integer.parseInt(currentPage);
- int pageSizes = Integer.parseInt(pageSize);
- // 调用下一层
- PageService pageService=new PageService();
- PageBrand<Brand> page = pageService.page(currentPages, pageSizes);
-
- // 转json
- String s = JSON.toJSONString(page);
- // 设置响应头
- response.setContentType("text/json;charset=utf-8");
- // 返回数据
- response.getWriter().write(s);
-
- }
-
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- request.setCharacterEncoding("utf-8");//手动解码
- this.doGet(request, response);//把数据传给get
- }
- }
- package service;
-
- import dao.PageDao;
- import po.Brand;
- import po.PageBrand;
-
- import java.util.List;
-
- public class PageService {
- public PageBrand<Brand> page(int currentPages, int pageSizes) {
- // 返回两个 数据集合 总条数
-
- // 创建返回对象
- PageBrand<Brand> pageBrand=new PageBrand<>();
- // 查询总条数
- PageDao pageDao=new PageDao();
- int total = pageDao.total();
- // 封装
- pageBrand.setTotal(total);
-
- // 查询数据
- List<Brand> sel = pageDao.sel(currentPages, pageSizes);
- // 封装
- pageBrand.setList(sel);
- // 返回
-
- return pageBrand;
-
-
-
-
-
- }
- }
- package dao;
-
- import po.Brand;
- import util.JdbcUtil;
-
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.ArrayList;
- import java.util.List;
-
- public class PageDao {
- // 查询总条数
- public int total() {
- Connection conn = JdbcUtil.getconn();
- ResultSet re = null;
- PreparedStatement pr = null;
- int total = -1;
-
- String sql = "select count(*) as count from tb_brand";
-
- try {
- pr = conn.prepareStatement(sql);
-
- re = pr.executeQuery();
-
- while (re.next()) {
- total = re.getInt("count");
- }
-
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- return total;
-
-
- }
-
- //查询数据
- public List<Brand> sel(int currentPages, int pageSizes) {
- Connection conn = JdbcUtil.getconn();
- ResultSet re = null;
- PreparedStatement pr = null;
- List<Brand> list = new ArrayList<>();
- // 5 ,5
- String sql = "select * from tb_brand limit ?,?";
-
- try {
- pr = conn.prepareStatement(sql);
- //当前页-1*每页条数 == 起始值
- //每页条数 == 终止值
- // 2 5 2-1=1*5
- pr.setInt(1, (currentPages - 1) * pageSizes);
- pr.setInt(2, pageSizes);
-
- re = pr.executeQuery();
-
- while (re.next()) {
- Brand brand = new Brand();
- brand.setId(re.getInt("id"));
- brand.setBrandName(re.getString("brand_name"));
- brand.setCompanyName(re.getString("company_name"));
- brand.setOrdered(re.getInt("ordered"));
- brand.setDescription(re.getString("description"));
- brand.setStatus(re.getInt("status"));
-
- list.add(brand);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- return list;
- }
- }
删除功能 web层 service层 dao层
- package web;
-
- import service.DelService;
-
- import javax.servlet.*;
- import javax.servlet.http.*;
- import javax.servlet.annotation.*;
- import java.io.IOException;
-
- @WebServlet( "/DelServlet")
- public class DelServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // 接收数据
- String s = request.getParameter("id");
- // 将id强转为int类型
- int id=Integer.parseInt(s);
- // 调用下一层
- DelService delService=new DelService();
- boolean a = delService.del(id);
- // 返回数据
- if (a){
- response.getWriter().write("true");
- }
-
-
- }
-
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- request.setCharacterEncoding("utf-8");//手动解码
- this.doGet(request, response);//把数据传给get
- }
- }
- package service;
-
- import dao.DelDao;
-
- public class DelService {
-
- public boolean del(int id) {
- // 调用下一层
- DelDao delDao=new DelDao();
- return delDao.del(id);
- }
- }
- package dao;
-
- import util.JdbcUtil;
-
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
- public class DelDao {
- public boolean del(int id) {
- // 连接数据库
-
- Connection conn = JdbcUtil.getconn();
- ResultSet re=null;
- PreparedStatement pr=null;
- boolean a=false;
-
- String sql="delete from tb_brand where id=?";
-
- try {
- pr = conn.prepareStatement(sql);
-
- pr.setInt(1,id);
-
- int i = pr.executeUpdate();
- // 判断是否删除成功
- if (i==1){
- a=true;
- }else {
- a=false;
- }
-
-
- } catch (SQLException e) {
- e.printStackTrace();
- }
-
- // 关流
- JdbcUtil.getclose(pr, conn, re);
- return a;
-
- }
- }
批量删除功能 web层 service层 dao层
- package web;
-
- import com.alibaba.fastjson.JSON;
- import po.Brand;
- import service.DelAll;
-
- import javax.servlet.*;
- import javax.servlet.http.*;
- import javax.servlet.annotation.*;
- import java.io.IOException;
-
- @WebServlet( "/DelAllServlet")
- public class DelAllServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
- // 接收数据
- String s = request.getReader().readLine();
-
- // 转格式
- int[] ints = JSON.parseObject(s, int[].class);
- // Integer integer = JSON.parseObject(s, int.class);
-
- // 调用下一层1
- DelAll delAll=new DelAll();
- delAll.del(ints);
- // delAll.del(integer);
- //返回数据
- response.getWriter().write("true");
-
- }
-
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- request.setCharacterEncoding("utf-8");//手动解码
- this.doGet(request, response);//把数据传给get
- }
- }
- package service;
-
- import dao.DelAlldao;
-
- public class DelAll {
-
- public void del(int[] ints) {
- DelAlldao delAlldao=new DelAlldao();
- delAlldao.del(ints);
-
- }
- }
- package dao;
-
- import po.Brand;
- import util.JdbcUtil;
-
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
-
- public class DelAlldao {
- // public void delAll(Integer integer) {
- //
- // Brand brand=new Brand();
- // brand.setId(integer);
- // Connection conn = JdbcUtil.getconn();
- // ResultSet re = null;
- // PreparedStatement pr = null;
- //
- //
- 用批量删除语句进行删除
- // String sql = "delete from tb_brand where id in ( ? )";
- //
- // try {
- // pr = conn.prepareStatement(sql);
- //
- // pr.setInt(1, brand.getId());
- //
- // int i = pr.executeUpdate();
- //
- //
- // } catch (SQLException e) {
- // e.printStackTrace();
- // } finally {
- // // 关流
- // JdbcUtil.getclose(pr, conn, re);
- //
- // }
- //
- //
- // }
-
-
-
-
-
-
-
- public void del(int[] ints) {
-
-
- for (int id : ints) {
-
-
-
-
-
- Connection conn = JdbcUtil.getconn();
- ResultSet re = null;
- PreparedStatement pr = null;
-
-
- // 用批量删除语句进行删除
- String sql = "delete from tb_brand where id in ( ? )";
-
- try {
- pr = conn.prepareStatement(sql);
-
- pr.setInt(1, id);
-
- int i = pr.executeUpdate();
-
-
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- // 关流
- JdbcUtil.getclose(pr, conn, re);
-
- }
-
-
- }
- //
- //
- //
- //
- }
- }
新增表单功能 web层 service层 dao层
- package web;
-
- import com.alibaba.fastjson.JSON;
- import po.Brand;
- import service.Addservice;
-
- import javax.servlet.*;
- import javax.servlet.http.*;
- import javax.servlet.annotation.*;
- import java.io.IOException;
-
- @WebServlet( "/AddServlet")
- public class AddServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // 接收数据
- String s = request.getReader().readLine();
- // 把json字符串转成对象
- Brand brand = JSON.parseObject(s, Brand.class);
- //调用下一层
- Addservice addservice=new Addservice();
- boolean bo = addservice.add(brand);
- // 向前端返回结果
- response.getWriter().write(String.valueOf(bo));
-
- }
-
-
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- request.setCharacterEncoding("utf-8");//手动解码
- this.doGet(request, response);//把数据传给get
- }
- }
- package service;
-
- import dao.AddDao;
- import po.Brand;
-
- public class Addservice {
-
- public boolean add(Brand brand) {
- // 屌用下一层
- AddDao addDao=new AddDao();
- return addDao.add(brand);
- }
- }
-
- Connection conn = JdbcUtil.getconn();
- ResultSet re=null;
- PreparedStatement pr=null;
- boolean bo=false;
-
- String sql="insert into tb_brand (brand_name,company_name,ordered,description,status)values (?,?,?,?,?)";
-
- try {
- pr = conn.prepareStatement(sql);
-
- pr.setString(1, brand.getBrandName());
- pr.setString(2, brand.getCompanyName());
- pr.setInt(3, brand.getOrdered());
- pr.setString(4, brand.getDescription());
- pr.setInt(5,brand.getStatus());
-
- //i 受影响条数
- int i = pr.executeUpdate();
-
- if(i==1){
- bo=true;
- }
-
-
- } catch (SQLException e) {
- e.printStackTrace();
- }
- JdbcUtil.getclose(pr, conn, re);
- return bo;
修改表单信息功能 web层 service层 dao层
- package web;
-
- import com.alibaba.fastjson.JSON;
- import po.Brand;
- import service.UpdateService;
-
- import javax.servlet.*;
- import javax.servlet.http.*;
- import javax.servlet.annotation.*;
- import java.io.IOException;
-
- @WebServlet( "/UpdateServlet")
- public class UpdateServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // 接收数据
- String s = request.getReader().readLine();
- // 把json字符串转成对象
- Brand brand = JSON.parseObject(s, Brand.class);
- // 调用下一层
- UpdateService updateService=new UpdateService();
- boolean upd = updateService.upd(brand);
-
- // 返回数据
- response.getWriter().write(String.valueOf(upd));
-
- }
-
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- request.setCharacterEncoding("utf-8");//手动解码
- this.doGet(request, response);//把数据传给get
- }
- }
- package service;
-
- import dao.UpdateDao;
- import po.Brand;
-
- public class UpdateService {
- public boolean upd(Brand brand) {
- // 调用下一层
- UpdateDao updateDao=new UpdateDao();
- return updateDao.upd(brand);
-
- }
- }
- package dao;
-
- import po.Brand;
- import util.JdbcUtil;
-
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
- public class UpdateDao {
- public boolean upd(Brand brand) {
-
- Connection conn = JdbcUtil.getconn();
- ResultSet re=null;
- PreparedStatement pr=null;
- boolean bo=false;
-
- String sql = "update `tb_brand`set brand_name=?,company_name=?,ordered=?,description=?,status=? where id=?";
-
-
-
- try {
- pr = conn.prepareStatement(sql);
- // 给问号赋值
- pr.setString(1,brand.getBrandName());
- pr.setString(2,brand.getCompanyName());
- pr.setInt(3,brand.getOrdered());
- pr.setString(4,brand.getDescription());
- pr.setInt(5,brand.getStatus());
- pr.setInt(6,brand.getId());
-
-
- //i 受影响条数
- int i = pr.executeUpdate();
-
-
-
-
- if(i==1){
- bo=true;
- }
-
-
- } catch (SQLException e) {
- e.printStackTrace();
- }finally {
- JdbcUtil.getclose(pr, conn, re);
- return bo;
- }
- }
-
- }
搜索功能 模糊查询 web层 service层 dao层
- package web;
-
- import com.alibaba.fastjson.JSON;
- import po.Brand;
- import po.PageBrand;
- import service.PageService;
- import service.SelService;
-
- import javax.servlet.*;
- import javax.servlet.http.*;
- import javax.servlet.annotation.*;
- import java.io.IOException;
- import java.util.List;
-
- @WebServlet( "/SelServlet")
- public class SelServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
- // 接收数据
- String s = request.getReader().readLine();
- String currentPage = request.getParameter("currentPage");
- String pageSize = request.getParameter("pageSize");
-
- // 转格式
- Brand brand1 = JSON.parseObject(s, Brand.class);
- int currentPages = Integer.parseInt(currentPage);
- int pageSizes = Integer.parseInt(pageSize);
-
-
- // 调用下一层
- SelService sel=new SelService();
- PageBrand<Brand> sel1 = sel.sel(brand1, currentPages, pageSizes);
-
-
- // 转json格式
- String s1 = JSON.toJSONString(sel1);
- // 设置响应头
- response.setContentType("text/json;charset=utf-8");
- System.out.println(s1);
-
- // 返回数据
- response.getWriter().write(s1);
- }
-
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- request.setCharacterEncoding("utf-8");//手动解码
- this.doGet(request, response);//把数据传给get
- }
- }
- package service;
-
- import dao.SelDao;
- import po.Brand;
- import po.PageBrand;
-
- import java.util.List;
-
- public class SelService {
-
- public PageBrand<Brand> sel(Brand brand1, int currentPages, int pageSizes) {
- PageBrand<Brand> pageBrand=new PageBrand<>();
- // 调用下一层
- SelDao selDao=new SelDao();
- List<Brand> sel = selDao.sel(brand1, currentPages, pageSizes);
- // 封装
- pageBrand.setList(sel);
-
- // 调用下一层
- int total = selDao.total(brand1);
- // 封装
- pageBrand.setTotal(total);
-
- return pageBrand;
-
-
- }
-
-
-
-
- }
- package dao;
-
- import po.Brand;
- import util.JdbcUtil;
-
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
-
- public class SelDao {
-
-
- // 查询数据方法
- public List<Brand> sel(Brand brand1, int currentPages, int pageSizes) {
- // 连接数据库
- Connection conn = JdbcUtil.getconn();
- ResultSet re = null;
- PreparedStatement pr = null;
- List<Brand> list = new ArrayList<>();
- // 查询语句
- System.out.println(brand1.toString());
-
- String sql = "select * from tb_brand where 1=1 ";
- // System.out.println("111111111111111");
-
- if (brand1.getStatus() != null) {
- sql += "and status= ? ";
- }
- // System.out.println("222222222222");
-
- if (!"".equals(brand1.getBrandName())) {
- sql += "and brand_name like ?";
- }
-
- if (!"".equals(brand1.getCompanyName())) {
- sql += "and company_name like ?";
- }
- // System.out.println("3333333333333333");
- sql += "limit ?,?";
- try {
- pr = conn.prepareStatement(sql);
- // 定义int类型 变量i为后续占位符进行赋值
- int i = 1;
- if (brand1.getStatus() != null) {
- pr.setInt(i, brand1.getStatus());
- i++;
- }
-
- if (!"".equals(brand1.getBrandName())) {
- pr.setString(i, "%" + brand1.getBrandName() + "%");
- i++;
- }
- if (!"".equals(brand1.getCompanyName())) {
- pr.setString(i, "%" + brand1.getCompanyName() + "%");
- i++;
- }
-
- pr.setInt(i,(currentPages-1) * pageSizes);
- pr.setInt(++i, pageSizes);
-
- re = pr.executeQuery();
-
- while (re.next()) {
- Brand brand = new Brand();
- brand.setId(re.getInt("id"));
- brand.setBrandName(re.getString("brand_name"));
- brand.setCompanyName(re.getString("company_name"));
- brand.setOrdered(re.getInt("ordered"));
- brand.setDescription(re.getString("description"));
- brand.setStatus(re.getInt("status"));
- // 将查找到的数据添加到list集合
- list.add(brand);
- }
-
-
- }catch(SQLException e){
- e.printStackTrace();
-
-
- }finally {
-
- // 关流
- JdbcUtil.getclose(pr, conn, re);
- return list;
- }
-
-
- }
-
-
- public int total(Brand brand1) {
- // 连接数据库
- Connection conn = JdbcUtil.getconn();
- ResultSet re = null;
- PreparedStatement pr = null;
- int total=-1;
- // 查询语句
- System.out.println(brand1.toString());
-
- String sql = "select count(*) as count from tb_brand where 1=1 ";
- // System.out.println("111111111111111");
-
- if (brand1.getStatus() != null) {
- sql += "and status= ? ";
- }
- // System.out.println("222222222222");
-
- if (!"".equals(brand1.getBrandName())) {
- sql += "and brand_name like ?";
- }
-
- if (!"".equals(brand1.getCompanyName())) {
- sql += "and company_name like ?";
- }
-
- try {
- pr = conn.prepareStatement(sql);
- // 定义int类型 变量i为后续占位符进行赋值
- int i = 1;
- if (brand1.getStatus() != null) {
- pr.setInt(i, brand1.getStatus());
- i++;
- }
-
- if (!"".equals(brand1.getBrandName())) {
- pr.setString(i, "%" + brand1.getBrandName() + "%");
- i++;
- }
- if (!"".equals(brand1.getCompanyName())) {
- pr.setString(i, "%" + brand1.getCompanyName() + "%");
- }
-
-
- re = pr.executeQuery();
- while (re.next()) {
- total = re.getInt("count");
-
- }
-
-
- }catch(SQLException e){
- e.printStackTrace();
-
-
- }finally {
-
- // 关流
- JdbcUtil.getclose(pr, conn, re);
- }
-
-
- return total;
-
- }
- }
容器
Brand
- package po;
-
-
- public class Brand {
- private int id;
- private String brandName;
- private String companyName;
- private int ordered;
- String description;
- // 需要把 int类型status 改Integer类型 不然能int默认值是0 但0代表禁用 所以改为改Integer类型
- // 并且get set方法也需要改为ineger类型
-
-
-
-
-
-
-
-
-
-
-
- private Integer status;
-
- public Integer getStatus() {
- return status;
- }
-
- public void setStatus(Integer status) {
- this.status = status;
- }
-
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- public String getBrandName() {
- return brandName;
- }
-
- public void setBrandName(String brandName) {
- this.brandName = brandName;
- }
-
- public String getCompanyName() {
- return companyName;
- }
-
- public void setCompanyName(String companyName) {
- this.companyName = companyName;
- }
-
- public int getOrdered() {
- return ordered;
- }
-
- public void setOrdered(int ordered) {
- this.ordered = ordered;
- }
-
- public String getDescription() {
- return description;
- }
-
- public void setDescription(String description) {
- this.description = description;
- }
-
- public Brand() {
- }
-
- public Brand(int id, String brandName, String companyName, int ordered, String description) {
- this.id = id;
- this.brandName = brandName;
- this.companyName = companyName;
- this.ordered = ordered;
- this.description = description;
- }
-
- @Override
- public String toString() {
- return "Brand{" +
- "id=" + id +
- ", brandName='" + brandName + '\'' +
- ", companyName='" + companyName + '\'' +
- ", ordered=" + ordered +
- ", description='" + description + '\'' +
- ", status=" + status +
- '}';
- }
-
-
-
- }
PageBrand容器
- package po;
-
- import java.util.List;
-
- public class PageBrand<T>{
- private int total;
-
-
- private List<T>list;
-
- public PageBrand() {
- }
-
- public PageBrand(int total, List<T> list) {
- this.total = total;
- this.list = list;
- }
-
- public int getTotal() {
- return total;
- }
-
- public void setTotal(int total) {
- this.total = total;
- }
-
- public List<T> getList() {
- return list;
- }
-
- public void setList(List<T> list) {
- this.list = list;
- }
-
-
- }
-
-
-
项目目录
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。