<..._vue elementui 管理系统">
当前位置:   article > 正文

vue+element ui 项目 后台管理系统_vue elementui 管理系统

vue elementui 管理系统

  前端界面

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="js/vue.js"></script>
  7. <script src="js/axios-0.18.0.js"></script>
  8. <script src="element-ui/lib/index.js"></script>
  9. <link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
  10. </head>
  11. <body>
  12. <style>
  13. .el-table .warning-row {
  14. background: oldlace;
  15. }
  16. .el-table .success-row {
  17. background: #f0f9eb;
  18. }
  19. </style>
  20. <div id="app">
  21. <!-- 搜索框-->
  22. <el-form :inline="true" :model="formInline" class="demo-form-inline">
  23. <el-form-item label="当前状态">
  24. <el-select v-model="formInline.status" placeholder="当前状态">
  25. <el-option label="开启" value="1"></el-option>
  26. <el-option label="关闭" value="0"></el-option>
  27. </el-select>
  28. </el-form-item>
  29. <el-form-item label="品牌名称">
  30. <el-input v-model="formInline.brandName" clearable @clear="selSubmit" placeholder="品牌名称"></el-input>
  31. </el-form-item>
  32. <el-form-item label="企业名称">
  33. <el-input v-model="formInline.companyName" clearable @clear="selSubmit" placeholder="企业名称"></el-input>
  34. </el-form-item>
  35. <el-form-item>
  36. <el-button type="primary" @click="selSubmit">查询</el-button>
  37. </el-form-item>
  38. </el-form>
  39. <!-- 按钮-->
  40. <el-row>
  41. <el-button type="danger" plain @click="delAll" @click="delAll">批量删除</el-button>
  42. <el-button type="primary" plain @click="dialogVisible = true">新增</el-button>
  43. </el-row>
  44. <!-- 新增表单-->
  45. <el-dialog title="新增企业" :visible.sync="dialogVisible" width="30%">
  46. <el-form ref="form" :model="form" label-width="80px">
  47. <el-form-item label="品牌名称">
  48. <el-input v-model="form.brandName"></el-input>
  49. </el-form-item>
  50. <el-form-item label="企业名称">
  51. <el-input v-model="form.companyName"></el-input>
  52. </el-form-item>
  53. <el-form-item label="排序">
  54. <el-input v-model="form.ordered"></el-input>
  55. </el-form-item>
  56. <el-form-item label="备注">
  57. <el-input type="textarea" v-model="form.description"></el-input>
  58. </el-form-item>
  59. <el-form-item label="状态">
  60. <el-switch v-model="form.status" active-value="1" inactive-value="0"></el-switch>
  61. </el-form-item>
  62. <el-form-item>
  63. <el-button type="primary" @click="onSubmit">提交</el-button>
  64. <el-button @click="dialogVisible = false">取消</el-button>
  65. </el-form-item>
  66. </el-form>
  67. </span>
  68. </el-dialog>
  69. <!-- 修改表单-->
  70. <el-dialog title="修改企业信息" :visible.sync="vie" width="30%">
  71. <el-form ref="from" :model="from" label-width="80px">
  72. <el-form-item label="品牌名称">
  73. <el-input v-model="from.brandName"></el-input>
  74. </el-form-item>
  75. <el-form-item label="企业名称">
  76. <el-input v-model="from.companyName"></el-input>
  77. </el-form-item>
  78. <el-form-item label="排序">
  79. <el-input v-model="from.ordered"></el-input>
  80. </el-form-item>
  81. <el-form-item label="备注">
  82. <el-input type="textarea" v-model="from.description"></el-input>
  83. </el-form-item>
  84. <el-form-item label="状态">
  85. <el-switch v-model="from.status" active-value="1" inactive-value="0"></el-switch>
  86. </el-form-item>
  87. <el-form-item >
  88. <el-button type="primary" @click="update">提交</el-button>
  89. <el-button @click="vie = false">取消</el-button>
  90. </el-form-item>
  91. </el-form>
  92. </span>
  93. </el-dialog>
  94. <!-- 表格-->
  95. <template >
  96. <el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName"
  97. @selection-change="handleSelectionChange">
  98. <!-- 复选框-->
  99. <el-table-column
  100. type="selection"
  101. width="55">
  102. </el-table-column>
  103. <!-- id-->
  104. <el-table-column
  105. type="index">
  106. </el-table-column>
  107. <el-table-column prop="brandName" label="品牌名称" width="180" align="center">
  108. </el-table-column>
  109. <el-table-column prop="companyName" label="企业名称" width="180" align="center">
  110. </el-table-column>
  111. <el-table-column prop="ordered" label="排序" align="center">
  112. </el-table-column>
  113. <el-table-column prop="status" label="状态" align="center">
  114. </el-table-column>
  115. <el-table-column prop="address" label="操作" align="center">
  116. <!-- slot-scope="scope" 用来获取元素id-->
  117. <template slot-scope="scope">
  118. <el-button
  119. size="mini"
  120. @click="open(scope.$index, scope.row)">修改</el-button>
  121. <el-button
  122. size="mini"
  123. type="danger"
  124. @click="del(scope.$index, scope.row)">删除</el-button>
  125. </template>
  126. </el-table-column>
  127. </el-table>
  128. </template>
  129. <!-- 分页-->
  130. <div class="block">
  131. <el-pagination
  132. @size-change="handleSizeChange"
  133. @current-change="handleCurrentChange"
  134. :current-page="currentPage"
  135. :page-sizes="pageSizes"
  136. :page-size="pageSize"
  137. layout="total, sizes, prev, pager, next, jumper"
  138. :total="total">
  139. </el-pagination>
  140. </div>
  141. </div>
  142. <script>
  143. new Vue({
  144. el: "#app",
  145. // 数据区
  146. data() {
  147. return {
  148. //模型区
  149. //分页
  150. //当前页
  151. currentPage:2,
  152. //每页显示条数
  153. pageSizes: [5, 10, 20, 30],
  154. //每页显示条数初始化值
  155. pageSize: 5,
  156. //总条数
  157. total: 1,
  158. //搜索框模型
  159. formInline: {
  160. brandName: '',
  161. companyName: '',
  162. status: "",
  163. },
  164. //新增表单模型
  165. form: {
  166. brandName: "",
  167. companyName: "",
  168. ordered: "",
  169. description: "",
  170. status: ""
  171. },
  172. // 修改
  173. from: {
  174. brandName: "",
  175. companyName: "",
  176. ordered: "",
  177. description: "",
  178. status: "",
  179. },
  180. //对话框
  181. dialogVisible: false,
  182. // 修改对话框的显示
  183. vie:false,
  184. tableData: [],
  185. //删除复选
  186. multipleSelection: [],
  187. //删除复选id
  188. ids:[],
  189. }
  190. },
  191. //方法区
  192. methods: {
  193. // 搜索
  194. // 后端返回 数据+总条数
  195. selSubmit() {
  196. console.log(this.formInline)
  197. axios({
  198. method:"post",
  199. url:"http://localhost:8080/elementdemo1/SelServlet?currentPage="+this.currentPage+"&pageSize="+this.pageSize,
  200. data:this.formInline
  201. }).then(resp=>{
  202. this.tableData=resp.data.list
  203. this.total=resp.data.total
  204. })
  205. },
  206. // 开启修改框并 传递数据
  207. open(index, row){
  208. this.vie=true
  209. this.from=row
  210. },
  211. // 修改
  212. update(index, row){
  213. this.vie=false
  214. axios({
  215. method: "post",
  216. url: "http://localhost:8080/elementdemo1/UpdateServlet",
  217. data:this.from
  218. }).then(resp =>{
  219. if(resp.data){
  220. //1,关闭对话框
  221. this.vie=false
  222. //2,弹出提示
  223. this.$message({
  224. message: '修改成功',
  225. type: 'success'
  226. });
  227. //3,重新查询
  228. this.handleCurrentChange(this.currentPage)
  229. }else {
  230. //添加失败
  231. this.$message.error('修改失败');
  232. }
  233. })
  234. },
  235. // 删除
  236. del(index, row){
  237. axios({
  238. method:"get",
  239. url:"http://localhost:8080/elementdemo1/DelServlet?id="+row.id,
  240. }).then(resp=>{
  241. if(resp.data){
  242. //1弹出提示
  243. this.$message({
  244. message: '删除成功',
  245. type: 'success'
  246. });
  247. //2,重新查询
  248. this.handleCurrentChange(this.currentPage)
  249. }else {
  250. //删除失败
  251. this.$message.error('删除失败');
  252. }
  253. })
  254. },
  255. tableRowClassName({row, rowIndex}) {
  256. if (rowIndex === 1) {
  257. return 'warning-row';
  258. } else if (rowIndex === 3) {
  259. return 'success-row';
  260. }
  261. return '';
  262. },
  263. //批量删除方法
  264. delAll(){
  265. //创建一个模型 id
  266. for(let i=0;i<this.multipleSelection.length;i++){
  267. var id=this.multipleSelection[i].id
  268. this.ids[i]=id
  269. }
  270. //发送ajax
  271. axios({
  272. method:"post",
  273. url:"http://localhost:8080/elementdemo1/DelAllServlet",
  274. data:this.ids
  275. }).then(resp =>{
  276. if(resp.data){
  277. //1弹出提示
  278. this.$message({
  279. message: '删除成功',
  280. type: 'success'
  281. });
  282. //2,重新查询
  283. this.handleCurrentChange(this.currentPage)
  284. }else {
  285. //删除失败
  286. this.$message.error('删除失败');
  287. }
  288. })
  289. },
  290. //删除复选
  291. handleSelectionChange(val) {
  292. this.multipleSelection = val;
  293. },
  294. //新增表单提交
  295. onSubmit() {
  296. axios({
  297. method: "post",
  298. url: "http://localhost:8080/elementdemo1/AddServlet",
  299. data:this.form
  300. }).then(resp =>{
  301. if(resp.data){
  302. //添加成功
  303. //1,关闭对话框
  304. this.dialogVisible=false
  305. //2,弹出提示
  306. this.$message({
  307. message: '添加成功',
  308. type: 'success'
  309. });
  310. //3,重新查询
  311. this.handleCurrentChange(this.currentPage)
  312. }else {
  313. //添加失败
  314. this.$message.error('添加失败');
  315. }
  316. })
  317. },
  318. //每页显示条数改变
  319. handleSizeChange(val) {
  320. this.pageSize=val
  321. axios({
  322. method:"get",
  323. url:"http://localhost:8080/elementdemo1/PageServlet?currentPage="+this.currentPage+"&pageSize="+this.pageSize
  324. }).then(resp =>{
  325. this.tableData=resp.data.list
  326. this.total=resp.data.total
  327. })
  328. },
  329. //当前页改变触发
  330. handleCurrentChange(val) {
  331. this.currentPage= val
  332. axios({
  333. method:"get",
  334. url:"http://localhost:8080/elementdemo1/PageServlet?currentPage="+this.currentPage+"&pageSize="+this.pageSize
  335. }).then(resp =>{
  336. this.tableData=resp.data.list
  337. this.total=resp.data.total
  338. })
  339. },
  340. SelAll(){
  341. axios({
  342. method:"get",
  343. url:"http://localhost:8080/elementdemo1/SelAllServlet"
  344. }).then(resp =>{
  345. this.tableData=resp.data
  346. })
  347. }
  348. },
  349. //钩子
  350. mounted(){
  351. //1=当前页
  352. this.handleCurrentChange(1)
  353. }
  354. })
  355. </script>
  356. </body>
  357. </html>

查询所有功能 web层   service层  dao层

  1. package web;
  2. import com.alibaba.fastjson.JSON;
  3. import po.Brand;
  4. import service.SelAllService;
  5. import javax.servlet.*;
  6. import javax.servlet.http.*;
  7. import javax.servlet.annotation.*;
  8. import java.io.IOException;
  9. import java.util.List;
  10. @WebServlet("/SelAllServlet")
  11. public class SelAllServlet extends HttpServlet {
  12. @Override
  13. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  14. //掉用下一层
  15. SelAllService selService=new SelAllService();
  16. // 调用方法 并接受返回值
  17. List<Brand> sel = selService.sel();
  18. // 转成json字符串
  19. String s = JSON.toJSONString(sel);
  20. // 设置响应头
  21. response.setContentType("text/json;charset=utf-8");
  22. // 返回数据
  23. response.getWriter().write(s);
  24. }
  25. @Override
  26. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  27. request.setCharacterEncoding("utf-8");//手动解码
  28. this.doGet(request, response);//把数据传给get
  29. }
  30. }
  1. package service;
  2. import dao.SelAllDao;
  3. import po.Brand;
  4. import java.util.List;
  5. public class SelAllService {
  6. public List<Brand> sel() {
  7. // 不用进行逻辑处理 直接调用dao
  8. SelAllDao selDao=new SelAllDao();
  9. return selDao.sel();
  10. }
  11. }
  1. package dao;
  2. import po.Brand;
  3. import util.JdbcUtil;
  4. import java.sql.Connection;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. public class SelAllDao {
  11. public List<Brand> sel() {
  12. // 连接数据库
  13. Connection conn= JdbcUtil.getconn();
  14. ResultSet re=null;
  15. PreparedStatement pr=null;
  16. List<Brand> list=new ArrayList<>();
  17. //查询语句
  18. String sql="select*from tb_brand";
  19. try {
  20. pr=conn.prepareStatement(sql);
  21. re=pr.executeQuery();
  22. while (re.next()){
  23. Brand brand=new Brand();
  24. brand.setId(re.getInt("id"));
  25. brand.setBrandName(re.getString("brand_name"));
  26. brand.setCompanyName(re.getString("company_name"));
  27. brand.setOrdered(re.getInt("ordered"));
  28. brand.setDescription(re.getString("description"));
  29. brand.setStatus(re.getInt("status"));
  30. // 将查找到的数据添加到list集合
  31. list.add(brand);
  32. }
  33. } catch (SQLException e) {
  34. e.printStackTrace();
  35. }
  36. // 关流
  37. JdbcUtil.getclose(pr, conn, re);
  38. // 返回list集合
  39. return list;
  40. }
  41. }

分页功能  查询总条数功能  web层   service层  dao层

 

  1. package web;
  2. import com.alibaba.fastjson.JSON;
  3. import po.Brand;
  4. import po.PageBrand;
  5. import service.PageService;
  6. import javax.servlet.*;
  7. import javax.servlet.http.*;
  8. import javax.servlet.annotation.*;
  9. import java.io.IOException;
  10. @WebServlet("/PageServlet")
  11. public class PageServlet extends HttpServlet {
  12. @Override
  13. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  14. // 接收参数
  15. String currentPage = request.getParameter("currentPage");
  16. String pageSize = request.getParameter("pageSize");
  17. // 转成int类型
  18. int currentPages = Integer.parseInt(currentPage);
  19. int pageSizes = Integer.parseInt(pageSize);
  20. // 调用下一层
  21. PageService pageService=new PageService();
  22. PageBrand<Brand> page = pageService.page(currentPages, pageSizes);
  23. // 转json
  24. String s = JSON.toJSONString(page);
  25. // 设置响应头
  26. response.setContentType("text/json;charset=utf-8");
  27. // 返回数据
  28. response.getWriter().write(s);
  29. }
  30. @Override
  31. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  32. request.setCharacterEncoding("utf-8");//手动解码
  33. this.doGet(request, response);//把数据传给get
  34. }
  35. }

 

  1. package service;
  2. import dao.PageDao;
  3. import po.Brand;
  4. import po.PageBrand;
  5. import java.util.List;
  6. public class PageService {
  7. public PageBrand<Brand> page(int currentPages, int pageSizes) {
  8. // 返回两个 数据集合 总条数
  9. // 创建返回对象
  10. PageBrand<Brand> pageBrand=new PageBrand<>();
  11. // 查询总条数
  12. PageDao pageDao=new PageDao();
  13. int total = pageDao.total();
  14. // 封装
  15. pageBrand.setTotal(total);
  16. // 查询数据
  17. List<Brand> sel = pageDao.sel(currentPages, pageSizes);
  18. // 封装
  19. pageBrand.setList(sel);
  20. // 返回
  21. return pageBrand;
  22. }
  23. }
  1. package dao;
  2. import po.Brand;
  3. import util.JdbcUtil;
  4. import java.sql.Connection;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. public class PageDao {
  10. // 查询总条数
  11. public int total() {
  12. Connection conn = JdbcUtil.getconn();
  13. ResultSet re = null;
  14. PreparedStatement pr = null;
  15. int total = -1;
  16. String sql = "select count(*) as count from tb_brand";
  17. try {
  18. pr = conn.prepareStatement(sql);
  19. re = pr.executeQuery();
  20. while (re.next()) {
  21. total = re.getInt("count");
  22. }
  23. } catch (Exception e) {
  24. e.printStackTrace();
  25. }
  26. return total;
  27. }
  28. //查询数据
  29. public List<Brand> sel(int currentPages, int pageSizes) {
  30. Connection conn = JdbcUtil.getconn();
  31. ResultSet re = null;
  32. PreparedStatement pr = null;
  33. List<Brand> list = new ArrayList<>();
  34. // 5 ,5
  35. String sql = "select * from tb_brand limit ?,?";
  36. try {
  37. pr = conn.prepareStatement(sql);
  38. //当前页-1*每页条数 == 起始值
  39. //每页条数 == 终止值
  40. // 2 5 2-1=1*5
  41. pr.setInt(1, (currentPages - 1) * pageSizes);
  42. pr.setInt(2, pageSizes);
  43. re = pr.executeQuery();
  44. while (re.next()) {
  45. Brand brand = new Brand();
  46. brand.setId(re.getInt("id"));
  47. brand.setBrandName(re.getString("brand_name"));
  48. brand.setCompanyName(re.getString("company_name"));
  49. brand.setOrdered(re.getInt("ordered"));
  50. brand.setDescription(re.getString("description"));
  51. brand.setStatus(re.getInt("status"));
  52. list.add(brand);
  53. }
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. }
  57. return list;
  58. }
  59. }

删除功能  web层   service层  dao层

  1. package web;
  2. import service.DelService;
  3. import javax.servlet.*;
  4. import javax.servlet.http.*;
  5. import javax.servlet.annotation.*;
  6. import java.io.IOException;
  7. @WebServlet( "/DelServlet")
  8. public class DelServlet extends HttpServlet {
  9. @Override
  10. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  11. // 接收数据
  12. String s = request.getParameter("id");
  13. // 将id强转为int类型
  14. int id=Integer.parseInt(s);
  15. // 调用下一层
  16. DelService delService=new DelService();
  17. boolean a = delService.del(id);
  18. // 返回数据
  19. if (a){
  20. response.getWriter().write("true");
  21. }
  22. }
  23. @Override
  24. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  25. request.setCharacterEncoding("utf-8");//手动解码
  26. this.doGet(request, response);//把数据传给get
  27. }
  28. }

 

  1. package service;
  2. import dao.DelDao;
  3. public class DelService {
  4. public boolean del(int id) {
  5. // 调用下一层
  6. DelDao delDao=new DelDao();
  7. return delDao.del(id);
  8. }
  9. }
  1. package dao;
  2. import util.JdbcUtil;
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. public class DelDao {
  8. public boolean del(int id) {
  9. // 连接数据库
  10. Connection conn = JdbcUtil.getconn();
  11. ResultSet re=null;
  12. PreparedStatement pr=null;
  13. boolean a=false;
  14. String sql="delete from tb_brand where id=?";
  15. try {
  16. pr = conn.prepareStatement(sql);
  17. pr.setInt(1,id);
  18. int i = pr.executeUpdate();
  19. // 判断是否删除成功
  20. if (i==1){
  21. a=true;
  22. }else {
  23. a=false;
  24. }
  25. } catch (SQLException e) {
  26. e.printStackTrace();
  27. }
  28. // 关流
  29. JdbcUtil.getclose(pr, conn, re);
  30. return a;
  31. }
  32. }

批量删除功能    web层   service层  dao层

 

  1. package web;
  2. import com.alibaba.fastjson.JSON;
  3. import po.Brand;
  4. import service.DelAll;
  5. import javax.servlet.*;
  6. import javax.servlet.http.*;
  7. import javax.servlet.annotation.*;
  8. import java.io.IOException;
  9. @WebServlet( "/DelAllServlet")
  10. public class DelAllServlet extends HttpServlet {
  11. @Override
  12. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  13. // 接收数据
  14. String s = request.getReader().readLine();
  15. // 转格式
  16. int[] ints = JSON.parseObject(s, int[].class);
  17. // Integer integer = JSON.parseObject(s, int.class);
  18. // 调用下一层1
  19. DelAll delAll=new DelAll();
  20. delAll.del(ints);
  21. // delAll.del(integer);
  22. //返回数据
  23. response.getWriter().write("true");
  24. }
  25. @Override
  26. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  27. request.setCharacterEncoding("utf-8");//手动解码
  28. this.doGet(request, response);//把数据传给get
  29. }
  30. }
  1. package service;
  2. import dao.DelAlldao;
  3. public class DelAll {
  4. public void del(int[] ints) {
  5. DelAlldao delAlldao=new DelAlldao();
  6. delAlldao.del(ints);
  7. }
  8. }
  1. package dao;
  2. import po.Brand;
  3. import util.JdbcUtil;
  4. import java.sql.Connection;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9. import java.util.Iterator;
  10. import java.util.List;
  11. public class DelAlldao {
  12. // public void delAll(Integer integer) {
  13. //
  14. // Brand brand=new Brand();
  15. // brand.setId(integer);
  16. // Connection conn = JdbcUtil.getconn();
  17. // ResultSet re = null;
  18. // PreparedStatement pr = null;
  19. //
  20. //
  21. 用批量删除语句进行删除
  22. // String sql = "delete from tb_brand where id in ( ? )";
  23. //
  24. // try {
  25. // pr = conn.prepareStatement(sql);
  26. //
  27. // pr.setInt(1, brand.getId());
  28. //
  29. // int i = pr.executeUpdate();
  30. //
  31. //
  32. // } catch (SQLException e) {
  33. // e.printStackTrace();
  34. // } finally {
  35. // // 关流
  36. // JdbcUtil.getclose(pr, conn, re);
  37. //
  38. // }
  39. //
  40. //
  41. // }
  42. public void del(int[] ints) {
  43. for (int id : ints) {
  44. Connection conn = JdbcUtil.getconn();
  45. ResultSet re = null;
  46. PreparedStatement pr = null;
  47. // 用批量删除语句进行删除
  48. String sql = "delete from tb_brand where id in ( ? )";
  49. try {
  50. pr = conn.prepareStatement(sql);
  51. pr.setInt(1, id);
  52. int i = pr.executeUpdate();
  53. } catch (SQLException e) {
  54. e.printStackTrace();
  55. } finally {
  56. // 关流
  57. JdbcUtil.getclose(pr, conn, re);
  58. }
  59. }
  60. //
  61. //
  62. //
  63. //
  64. }
  65. }

新增表单功能    web层   service层  dao层

 

  1. package web;
  2. import com.alibaba.fastjson.JSON;
  3. import po.Brand;
  4. import service.Addservice;
  5. import javax.servlet.*;
  6. import javax.servlet.http.*;
  7. import javax.servlet.annotation.*;
  8. import java.io.IOException;
  9. @WebServlet( "/AddServlet")
  10. public class AddServlet extends HttpServlet {
  11. @Override
  12. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  13. // 接收数据
  14. String s = request.getReader().readLine();
  15. // 把json字符串转成对象
  16. Brand brand = JSON.parseObject(s, Brand.class);
  17. //调用下一层
  18. Addservice addservice=new Addservice();
  19. boolean bo = addservice.add(brand);
  20. // 向前端返回结果
  21. response.getWriter().write(String.valueOf(bo));
  22. }
  23. @Override
  24. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  25. request.setCharacterEncoding("utf-8");//手动解码
  26. this.doGet(request, response);//把数据传给get
  27. }
  28. }
  1. package service;
  2. import dao.AddDao;
  3. import po.Brand;
  4. public class Addservice {
  5. public boolean add(Brand brand) {
  6. // 屌用下一层
  7. AddDao addDao=new AddDao();
  8. return addDao.add(brand);
  9. }
  10. }
  1. Connection conn = JdbcUtil.getconn();
  2. ResultSet re=null;
  3. PreparedStatement pr=null;
  4. boolean bo=false;
  5. String sql="insert into tb_brand (brand_name,company_name,ordered,description,status)values (?,?,?,?,?)";
  6. try {
  7. pr = conn.prepareStatement(sql);
  8. pr.setString(1, brand.getBrandName());
  9. pr.setString(2, brand.getCompanyName());
  10. pr.setInt(3, brand.getOrdered());
  11. pr.setString(4, brand.getDescription());
  12. pr.setInt(5,brand.getStatus());
  13. //i 受影响条数
  14. int i = pr.executeUpdate();
  15. if(i==1){
  16. bo=true;
  17. }
  18. } catch (SQLException e) {
  19. e.printStackTrace();
  20. }
  21. JdbcUtil.getclose(pr, conn, re);
  22. return bo;

修改表单信息功能     web层   service层  dao层

 

  1. package web;
  2. import com.alibaba.fastjson.JSON;
  3. import po.Brand;
  4. import service.UpdateService;
  5. import javax.servlet.*;
  6. import javax.servlet.http.*;
  7. import javax.servlet.annotation.*;
  8. import java.io.IOException;
  9. @WebServlet( "/UpdateServlet")
  10. public class UpdateServlet extends HttpServlet {
  11. @Override
  12. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  13. // 接收数据
  14. String s = request.getReader().readLine();
  15. // 把json字符串转成对象
  16. Brand brand = JSON.parseObject(s, Brand.class);
  17. // 调用下一层
  18. UpdateService updateService=new UpdateService();
  19. boolean upd = updateService.upd(brand);
  20. // 返回数据
  21. response.getWriter().write(String.valueOf(upd));
  22. }
  23. @Override
  24. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  25. request.setCharacterEncoding("utf-8");//手动解码
  26. this.doGet(request, response);//把数据传给get
  27. }
  28. }
  1. package service;
  2. import dao.UpdateDao;
  3. import po.Brand;
  4. public class UpdateService {
  5. public boolean upd(Brand brand) {
  6. // 调用下一层
  7. UpdateDao updateDao=new UpdateDao();
  8. return updateDao.upd(brand);
  9. }
  10. }
  1. package dao;
  2. import po.Brand;
  3. import util.JdbcUtil;
  4. import java.sql.Connection;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. public class UpdateDao {
  9. public boolean upd(Brand brand) {
  10. Connection conn = JdbcUtil.getconn();
  11. ResultSet re=null;
  12. PreparedStatement pr=null;
  13. boolean bo=false;
  14. String sql = "update `tb_brand`set brand_name=?,company_name=?,ordered=?,description=?,status=? where id=?";
  15. try {
  16. pr = conn.prepareStatement(sql);
  17. // 给问号赋值
  18. pr.setString(1,brand.getBrandName());
  19. pr.setString(2,brand.getCompanyName());
  20. pr.setInt(3,brand.getOrdered());
  21. pr.setString(4,brand.getDescription());
  22. pr.setInt(5,brand.getStatus());
  23. pr.setInt(6,brand.getId());
  24. //i 受影响条数
  25. int i = pr.executeUpdate();
  26. if(i==1){
  27. bo=true;
  28. }
  29. } catch (SQLException e) {
  30. e.printStackTrace();
  31. }finally {
  32. JdbcUtil.getclose(pr, conn, re);
  33. return bo;
  34. }
  35. }
  36. }

搜索功能  模糊查询   web层   service层  dao层

  1. package web;
  2. import com.alibaba.fastjson.JSON;
  3. import po.Brand;
  4. import po.PageBrand;
  5. import service.PageService;
  6. import service.SelService;
  7. import javax.servlet.*;
  8. import javax.servlet.http.*;
  9. import javax.servlet.annotation.*;
  10. import java.io.IOException;
  11. import java.util.List;
  12. @WebServlet( "/SelServlet")
  13. public class SelServlet extends HttpServlet {
  14. @Override
  15. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  16. // 接收数据
  17. String s = request.getReader().readLine();
  18. String currentPage = request.getParameter("currentPage");
  19. String pageSize = request.getParameter("pageSize");
  20. // 转格式
  21. Brand brand1 = JSON.parseObject(s, Brand.class);
  22. int currentPages = Integer.parseInt(currentPage);
  23. int pageSizes = Integer.parseInt(pageSize);
  24. // 调用下一层
  25. SelService sel=new SelService();
  26. PageBrand<Brand> sel1 = sel.sel(brand1, currentPages, pageSizes);
  27. // 转json格式
  28. String s1 = JSON.toJSONString(sel1);
  29. // 设置响应头
  30. response.setContentType("text/json;charset=utf-8");
  31. System.out.println(s1);
  32. // 返回数据
  33. response.getWriter().write(s1);
  34. }
  35. @Override
  36. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  37. request.setCharacterEncoding("utf-8");//手动解码
  38. this.doGet(request, response);//把数据传给get
  39. }
  40. }
  1. package service;
  2. import dao.SelDao;
  3. import po.Brand;
  4. import po.PageBrand;
  5. import java.util.List;
  6. public class SelService {
  7. public PageBrand<Brand> sel(Brand brand1, int currentPages, int pageSizes) {
  8. PageBrand<Brand> pageBrand=new PageBrand<>();
  9. // 调用下一层
  10. SelDao selDao=new SelDao();
  11. List<Brand> sel = selDao.sel(brand1, currentPages, pageSizes);
  12. // 封装
  13. pageBrand.setList(sel);
  14. // 调用下一层
  15. int total = selDao.total(brand1);
  16. // 封装
  17. pageBrand.setTotal(total);
  18. return pageBrand;
  19. }
  20. }
  1. package dao;
  2. import po.Brand;
  3. import util.JdbcUtil;
  4. import java.sql.Connection;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. public class SelDao {
  11. // 查询数据方法
  12. public List<Brand> sel(Brand brand1, int currentPages, int pageSizes) {
  13. // 连接数据库
  14. Connection conn = JdbcUtil.getconn();
  15. ResultSet re = null;
  16. PreparedStatement pr = null;
  17. List<Brand> list = new ArrayList<>();
  18. // 查询语句
  19. System.out.println(brand1.toString());
  20. String sql = "select * from tb_brand where 1=1 ";
  21. // System.out.println("111111111111111");
  22. if (brand1.getStatus() != null) {
  23. sql += "and status= ? ";
  24. }
  25. // System.out.println("222222222222");
  26. if (!"".equals(brand1.getBrandName())) {
  27. sql += "and brand_name like ?";
  28. }
  29. if (!"".equals(brand1.getCompanyName())) {
  30. sql += "and company_name like ?";
  31. }
  32. // System.out.println("3333333333333333");
  33. sql += "limit ?,?";
  34. try {
  35. pr = conn.prepareStatement(sql);
  36. // 定义int类型 变量i为后续占位符进行赋值
  37. int i = 1;
  38. if (brand1.getStatus() != null) {
  39. pr.setInt(i, brand1.getStatus());
  40. i++;
  41. }
  42. if (!"".equals(brand1.getBrandName())) {
  43. pr.setString(i, "%" + brand1.getBrandName() + "%");
  44. i++;
  45. }
  46. if (!"".equals(brand1.getCompanyName())) {
  47. pr.setString(i, "%" + brand1.getCompanyName() + "%");
  48. i++;
  49. }
  50. pr.setInt(i,(currentPages-1) * pageSizes);
  51. pr.setInt(++i, pageSizes);
  52. re = pr.executeQuery();
  53. while (re.next()) {
  54. Brand brand = new Brand();
  55. brand.setId(re.getInt("id"));
  56. brand.setBrandName(re.getString("brand_name"));
  57. brand.setCompanyName(re.getString("company_name"));
  58. brand.setOrdered(re.getInt("ordered"));
  59. brand.setDescription(re.getString("description"));
  60. brand.setStatus(re.getInt("status"));
  61. // 将查找到的数据添加到list集合
  62. list.add(brand);
  63. }
  64. }catch(SQLException e){
  65. e.printStackTrace();
  66. }finally {
  67. // 关流
  68. JdbcUtil.getclose(pr, conn, re);
  69. return list;
  70. }
  71. }
  72. public int total(Brand brand1) {
  73. // 连接数据库
  74. Connection conn = JdbcUtil.getconn();
  75. ResultSet re = null;
  76. PreparedStatement pr = null;
  77. int total=-1;
  78. // 查询语句
  79. System.out.println(brand1.toString());
  80. String sql = "select count(*) as count from tb_brand where 1=1 ";
  81. // System.out.println("111111111111111");
  82. if (brand1.getStatus() != null) {
  83. sql += "and status= ? ";
  84. }
  85. // System.out.println("222222222222");
  86. if (!"".equals(brand1.getBrandName())) {
  87. sql += "and brand_name like ?";
  88. }
  89. if (!"".equals(brand1.getCompanyName())) {
  90. sql += "and company_name like ?";
  91. }
  92. try {
  93. pr = conn.prepareStatement(sql);
  94. // 定义int类型 变量i为后续占位符进行赋值
  95. int i = 1;
  96. if (brand1.getStatus() != null) {
  97. pr.setInt(i, brand1.getStatus());
  98. i++;
  99. }
  100. if (!"".equals(brand1.getBrandName())) {
  101. pr.setString(i, "%" + brand1.getBrandName() + "%");
  102. i++;
  103. }
  104. if (!"".equals(brand1.getCompanyName())) {
  105. pr.setString(i, "%" + brand1.getCompanyName() + "%");
  106. }
  107. re = pr.executeQuery();
  108. while (re.next()) {
  109. total = re.getInt("count");
  110. }
  111. }catch(SQLException e){
  112. e.printStackTrace();
  113. }finally {
  114. // 关流
  115. JdbcUtil.getclose(pr, conn, re);
  116. }
  117. return total;
  118. }
  119. }

容器

Brand

  1. package po;
  2. public class Brand {
  3. private int id;
  4. private String brandName;
  5. private String companyName;
  6. private int ordered;
  7. String description;
  8. // 需要把 int类型status 改Integer类型 不然能int默认值是0 但0代表禁用 所以改为改Integer类型
  9. // 并且get set方法也需要改为ineger类型
  10. private Integer status;
  11. public Integer getStatus() {
  12. return status;
  13. }
  14. public void setStatus(Integer status) {
  15. this.status = status;
  16. }
  17. public int getId() {
  18. return id;
  19. }
  20. public void setId(int id) {
  21. this.id = id;
  22. }
  23. public String getBrandName() {
  24. return brandName;
  25. }
  26. public void setBrandName(String brandName) {
  27. this.brandName = brandName;
  28. }
  29. public String getCompanyName() {
  30. return companyName;
  31. }
  32. public void setCompanyName(String companyName) {
  33. this.companyName = companyName;
  34. }
  35. public int getOrdered() {
  36. return ordered;
  37. }
  38. public void setOrdered(int ordered) {
  39. this.ordered = ordered;
  40. }
  41. public String getDescription() {
  42. return description;
  43. }
  44. public void setDescription(String description) {
  45. this.description = description;
  46. }
  47. public Brand() {
  48. }
  49. public Brand(int id, String brandName, String companyName, int ordered, String description) {
  50. this.id = id;
  51. this.brandName = brandName;
  52. this.companyName = companyName;
  53. this.ordered = ordered;
  54. this.description = description;
  55. }
  56. @Override
  57. public String toString() {
  58. return "Brand{" +
  59. "id=" + id +
  60. ", brandName='" + brandName + '\'' +
  61. ", companyName='" + companyName + '\'' +
  62. ", ordered=" + ordered +
  63. ", description='" + description + '\'' +
  64. ", status=" + status +
  65. '}';
  66. }
  67. }

PageBrand容器

  1. package po;
  2. import java.util.List;
  3. public class PageBrand<T>{
  4. private int total;
  5. private List<T>list;
  6. public PageBrand() {
  7. }
  8. public PageBrand(int total, List<T> list) {
  9. this.total = total;
  10. this.list = list;
  11. }
  12. public int getTotal() {
  13. return total;
  14. }
  15. public void setTotal(int total) {
  16. this.total = total;
  17. }
  18. public List<T> getList() {
  19. return list;
  20. }
  21. public void setList(List<T> list) {
  22. this.list = list;
  23. }
  24. }

项目目录

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/113289
推荐阅读
相关标签
  

闽ICP备14008679号