A,案例结构:

综合案例 - 图1

1,服务器端代码 !:

a,DAO层:

  1. //Mapper:
  2. package com.itheima.mapper;
  3. import com.itheima.pojo.Brand;
  4. import org.apache.ibatis.annotations.Insert;
  5. import org.apache.ibatis.annotations.Param;
  6. import org.apache.ibatis.annotations.Select;
  7. import java.util.List;
  8. public interface BrandMapper {
  9. /**
  10. * 查询所有品牌
  11. *
  12. * @return List<Brand>
  13. */
  14. @Select ("select * from tb_brand;")
  15. List <Brand> selectAll ();
  16. /**
  17. * 添加品牌
  18. *
  19. */
  20. @Insert ("insert into tb_brand values (null,#{brandName},#{companyName},#{ordered},#{description},#{status})")
  21. void addBrands (Brand brand);
  22. /**
  23. * 批量删除品牌
  24. *
  25. */
  26. void deleteBrands (@Param("brandsId") int[] brandsId);
  27. /**
  28. * 查询单个品牌
  29. */
  30. List<Brand> selectOneBrand(@Param("brandName") String brandName,@Param("companyName") String companyName ,@Param("status")Integer status);
  31. /**
  32. * 分页查询一页的数据
  33. * @return 数据信息
  34. */
  35. @Select("select * from tb_brand limit #{begin},#{size} ;")
  36. List<Brand> selectAllByLimit(@Param("begin") int begin , @Param("size") int size);
  37. /**
  38. * 分页查询的数据数量
  39. * @return 数量
  40. */
  41. @Select("select count(*) from tb_brand;")
  42. int selectTotalName();
  43. /**
  44. * 条件分页查询的数据数量
  45. */
  46. int selectByPageCount(Brand brand);
  47. /**
  48. * 多条件分页查询
  49. */
  50. List<Brand> selectBypage(@Param("begin") int begin , @Param("size") int size,@Param("brand") Brand brand);
  51. }
  52. //Mapper.xml
  53. <?xml version="1.0" encoding="UTF-8" ?>
  54. <!DOCTYPE mapper
  55. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  56. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  57. <mapper namespace = "com.itheima.mapper.BrandMapper">
  58. <resultMap id = "brandResultMap" type = "Brand">
  59. <result property = "brandName" column = "brand_name"/>
  60. <result property = "companyName" column = "company_name"/>
  61. </resultMap>
  62. <delete id = "deleteBrands">
  63. delete from tb_brand where id in
  64. <foreach collection = "brandsId" open = "(" separator = "," close = ")" item = "id">
  65. #{id}
  66. </foreach>
  67. </delete>
  68. <!-- 多条件查询 -->
  69. <select id = "selectOneBrand" resultType = "com.itheima.pojo.Brand">
  70. select * from tb_brand
  71. <where>
  72. <if test = "status != null">
  73. `status`= #{status}
  74. </if>
  75. <if test = "brandName != null and brandName !='' ">
  76. AND brand_Name like "%"#{brandName}"%"
  77. </if>
  78. <if test = "companyName != null and companyName !='' ">
  79. AND company_Name like "%"#{companyName}"%"
  80. </if>
  81. </where>
  82. </select>
  83. <!-- 分页查询的总数量-->
  84. <select id = "selectByPageCount" resultType = "java.lang.Integer">
  85. select * from tb_brand
  86. <where>
  87. <if test = "status != null">
  88. `status`= #{status}
  89. </if>
  90. <if test = "brandName != null and brandName !='' ">
  91. AND brand_Name like "%"#{brandName}"%"
  92. </if>
  93. <if test = "companyName != null and companyName !='' ">
  94. AND company_Name like "%"#{companyName}"%"
  95. </if>
  96. </where>
  97. </select>
  98. <!-- 分页条件查询 -->
  99. <select id = "selectBypage" resultType = "com.itheima.pojo.Brand">
  100. select * from tb_brand
  101. <where>
  102. <if test = "brand.status != null">
  103. `status`= #{brand.status}
  104. </if>
  105. <if test = "brand.brandName != null and brand.brandName !='' ">
  106. AND brand_Name like "%"#{brand.brandName}"%"
  107. </if>
  108. <if test = "brand.companyName != null and brand.companyName !='' ">
  109. AND company_Name like "%"#{brand.companyName}"%"
  110. </if>
  111. </where>
  112. </select>
  113. </mapper>

b,Service层:

  1. package com.itheima.service;
  2. import com.itheima.mapper.BrandMapper;
  3. import com.itheima.pojo.Brand;
  4. import com.itheima.pojo.PageBean;
  5. import com.itheima.util.DaoInstanceFactory;
  6. import java.util.List;
  7. /**
  8. * @author Jztice5
  9. * @date 2022年03月19日 17:22
  10. */
  11. public class BrandService {
  12. private final BrandMapper brandMapper = DaoInstanceFactory.getBean(BrandMapper.class);
  13. /**
  14. * 查询所有品牌
  15. *
  16. * @return 查询到的品牌信息 -> web层
  17. */
  18. public List <Brand> selectBrand () {
  19. return brandMapper.selectAll();
  20. }
  21. /**
  22. * 添加品牌
  23. * 添加的品牌信息 -> 服务器后端 (最终结果) -> 前端刷新页面
  24. */
  25. public void addBrand (Brand brand) {
  26. brandMapper.addBrands(brand);
  27. }
  28. /**
  29. * 批量删除
  30. *
  31. * @param brandListIds id -> 服务器
  32. */
  33. public void deleteBrand (int[] brandListIds) {
  34. brandMapper.deleteBrands(brandListIds);
  35. }
  36. /**
  37. * 查询单个品牌
  38. * 前端返回的条件数据 -> 服务器 (查询结果) -> 前端
  39. */
  40. public List <Brand> selectOneBrand (Brand brand) {
  41. return brandMapper.selectOneBrand(brand.getBrandName() , brand.getCompanyName() , brand.getStatus());
  42. }
  43. /**
  44. * 分页查询
  45. *
  46. */
  47. public PageBean <Brand> selectByPage (int currentPage , int pageSize) {
  48. //计算开始的值(第一页开始) currentPage:当前页数(前端显示的那个) pageSize:前端显示多少条数据
  49. //数据库:0,10 10,10 20,10...
  50. int pageHtml = (currentPage - 1) * pageSize;
  51. //调用dao层的分页查询返回查找到的数据
  52. List <Brand> brands = brandMapper.selectAllByLimit(pageHtml , pageSize);
  53. //获取总的记录数
  54. int sum = brandMapper.selectTotalName();
  55. //返回到pageBean实体类对象:
  56. // /**
  57. // * // 总记录数
  58. // * private int totalCount;
  59. // * // 当前页数据
  60. // * private List<T> rows;
  61. // */
  62. return new PageBean <>(sum , brands);
  63. }
  64. /**
  65. * 分页多条件查询
  66. */
  67. public PageBean <Brand> selectByPageWhere (int currentPage , int pageSize , Brand brand) {
  68. int pageHtmlSize = (currentPage - 1) * pageSize;
  69. List <Brand> brands = brandMapper.selectBypage(pageHtmlSize , pageSize , brand);
  70. int sum = brandMapper.selectTotalName();
  71. return new PageBean <>(sum , brands);
  72. }
  73. }

c,Web层:

  1. //参考可见分页查询

2,前端页面代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <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. <style>
  11. .el-table .warning-row {
  12. background: oldlace;
  13. }
  14. .el-table .success-row {
  15. background: #f0f9eb;
  16. }
  17. .el-dropdown-link {
  18. cursor: pointer;
  19. float: right;
  20. color: #409EFF;
  21. }
  22. .el-icon-arrow-down {
  23. font-size: 20px;
  24. }
  25. .div-topcss {
  26. width: 1000px;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <!--<div id="message">-->
  32. <!-- <template>-->
  33. <!-- <el-alert-->
  34. <!-- title="添加成功"-->
  35. <!-- type="success"-->
  36. <!-- center-->
  37. <!-- show-icon-->
  38. <!-- >-->
  39. <!-- </el-alert>-->
  40. <!-- </template>-->
  41. <!--</div>-->
  42. <div id="app">
  43. <!--第一行-->
  44. <el-row>
  45. <el-form :inline="true" :model="searchBrand" class="demo-form-inline">
  46. <el-form-item label="当前状态">
  47. <el-select v-model="searchBrand.status" placeholder="当前状态">
  48. <el-option label="禁用" value="0"></el-option>
  49. <el-option label="启用" value="1"></el-option>
  50. </el-select>
  51. </el-form-item>
  52. <el-form-item label="企业名称">
  53. <el-input v-model="searchBrand.companyName" placeholder="企业名称"></el-input>
  54. </el-form-item>
  55. <el-form-item label="品牌名称">
  56. <el-input v-model="searchBrand.brandName" placeholder="品牌名称"></el-input>
  57. </el-form-item>
  58. <el-form-item>
  59. <el-button type="primary" @click="onSubmit">查询</el-button>
  60. </el-form-item>
  61. <el-form-item>
  62. <el-button type="primary" @click="clean">清空</el-button>
  63. </el-form-item>
  64. <el-form-item>
  65. <div-topcss>
  66. </div-topcss>
  67. </el-form-item>
  68. <el-dropdown>
  69. <span class="el-dropdown-link">个人中心<i class="el-icon-arrow-down el-icon--right"></i>
  70. </span>
  71. <el-dropdown-menu slot="dropdown">
  72. <el-dropdown-item>我的订单</el-dropdown-item>
  73. <el-dropdown-item>我的钱包</el-dropdown-item>
  74. <el-dropdown-item>账户信息</el-dropdown-item>
  75. <el-dropdown-item divided @click="userout">退出登录</el-dropdown-item>
  76. </el-dropdown-menu>
  77. </el-dropdown>
  78. </el-form>
  79. </el-row>
  80. <!--第二行-->
  81. <el-row>
  82. <el-button type="danger" plain @click="deleteBrands">批量删除</el-button>
  83. <el-button type="primary" plain @click="addButton">新增</el-button>
  84. </el-row>
  85. <!--对话框-->
  86. <el-dialog
  87. title="编辑品牌"
  88. :visible.sync="dialogVisible"
  89. width="30%">
  90. <!--对话框里面的表单-->
  91. <el-form :model="addBrand" :rules="rules" ref="addBrand" label-width="100px" class="demo-ruleForm">
  92. <el-form-item label="品牌名称" prop="brandName">
  93. <el-input v-model="addBrand.brandName"></el-input>
  94. </el-form-item>
  95. <el-form-item label="企业名称" prop="companyName">
  96. <el-input v-model="addBrand.companyName"></el-input>
  97. </el-form-item>
  98. <el-form-item label="排序">
  99. <el-input v-model="addBrand.ordered"></el-input>
  100. </el-form-item>
  101. <el-form-item label="备注">
  102. <el-input type="textarea" v-model="addBrand.description"></el-input>
  103. </el-form-item>
  104. <el-form-item label="状态">
  105. <el-switch v-model="addBrand.status"
  106. active-value="1"
  107. inactive-value="0"></el-switch>
  108. </el-form-item>
  109. <el-form-item>
  110. <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
  111. <el-button @click="resetForm('ruleForm')">取消</el-button>
  112. </el-form-item>
  113. </el-form>
  114. </el-dialog>
  115. <!--第三行-->
  116. <el-row>
  117. <template>
  118. <el-table
  119. :data="tableData"
  120. style="width: 100%"
  121. :row-class-name="tableRowClassName"
  122. @selection-change="handleSelectionChange">
  123. <el-table-column
  124. type="selection"
  125. width="55">
  126. </el-table-column>
  127. <el-table-column
  128. type="index"
  129. width="50">
  130. </el-table-column>
  131. <el-table-column
  132. align="center"
  133. prop="brandName"
  134. label="品牌名称">
  135. </el-table-column>
  136. <el-table-column
  137. align="center"
  138. prop="companyName"
  139. label="企业名称">
  140. </el-table-column>
  141. <el-table-column
  142. prop="ordered"
  143. label="排序"
  144. width="150">
  145. </el-table-column>
  146. <el-table-column
  147. prop="status"
  148. label="当前状态"
  149. width="150">
  150. </el-table-column>
  151. <el-table-column label="操作" width="250">
  152. <template slot-scope="scope">
  153. <el-button
  154. size="mini"
  155. type="primary"
  156. @click="handleEdit(scope.$index, scope.row)">修改
  157. </el-button>
  158. <el-button
  159. size="mini"
  160. type="danger"
  161. @click="handleDelete(scope.$index, scope.row)">删除
  162. </el-button>
  163. </template>
  164. </el-table-column>
  165. </el-table>
  166. </template>
  167. </el-row>
  168. <!--第四行-->
  169. <el-row>
  170. <div class="block" style="text-align: center; margin-top: 15px">
  171. <el-pagination
  172. background
  173. @size-change="handleSizeChange"
  174. @current-change="handleCurrentChange"
  175. :current-page="currentPage"
  176. :page-sizes="[5, 10, 15, 20]"
  177. :page-size="5"
  178. layout="total, sizes, prev, pager, next, jumper"
  179. :total="400">
  180. </el-pagination>
  181. </div>
  182. </el-row>
  183. </div>
  184. <script>
  185. import alert from "./element-ui/packages/alert";
  186. new Vue({
  187. el: "#app",
  188. //设置页面加载完毕后就响应JSON数据
  189. mounted() {
  190. //调用查询;
  191. this.reselectAll();
  192. },
  193. methods: {
  194. //封装查询方法
  195. reselectAll() {
  196. let _this = this;
  197. axios.get("/ServletAllBrand").then(resp => {
  198. _this.tableData = resp.data;
  199. });
  200. },
  201. tableRowClassName({row, rowIndex}) {
  202. if (rowIndex === 1) {
  203. return 'warning-row';
  204. } else if (rowIndex === 3) {
  205. return 'success-row';
  206. }
  207. return '';
  208. },
  209. // 多选按钮状态改变会调用
  210. handleSelectionChange(val) {
  211. this.multipleSelection = val;
  212. },
  213. // 点击查询按钮时调用
  214. onSubmit() {
  215. let _this = this;
  216. //记得返回数据啊(this.searchBrand)
  217. axios.post("ServletSelectOne", this.searchBrand)
  218. .then(resp => {
  219. if (resp.data != null) {
  220. _this.tableData = resp.data;
  221. }
  222. })
  223. },
  224. submitForm(formName) {
  225. //在前端控制台输出数据信息
  226. console.log(this.addBrand);
  227. let _this = this;
  228. //axios
  229. axios.post("ServletAddBrand", this.addBrand)
  230. .then(resp => {
  231. if (resp.data == "success") {
  232. //添加成功后关闭窗口
  233. _this.dialogVisible = false;
  234. //重新加载数据
  235. _this.reselectAll();
  236. alert("添加成功");
  237. //清空数据模型:
  238. }
  239. })
  240. },
  241. resetForm(formName) {
  242. // 关闭对话框
  243. this.dialogVisible = false;
  244. },
  245. // 每页数量发生变化调用
  246. handleSizeChange(val) {
  247. console.log(`每页 ${val} 条`);
  248. },
  249. // 页码变化调用
  250. handleCurrentChange(val) {
  251. console.log(`当前页: ${val}`);
  252. },
  253. //设置点击新增后清空数据模型
  254. addButton() {
  255. this.dialogVisible = true;
  256. this.addBrand = {};
  257. },
  258. //批量删除:
  259. deleteBrands() {
  260. let _this = this;
  261. //遍历选中的数据获取id
  262. for (let brand of this.multipleSelection) {
  263. this.idList.push(brand.id);
  264. }
  265. //请求发送id到servlet
  266. axios.post("ServletDelectBrands", this.idList)
  267. .then(resp => {
  268. if (resp.data == "success") {
  269. alert("删除成功");
  270. _this.reselectAll();
  271. }
  272. })
  273. },
  274. //清空查询框
  275. clean() {
  276. this.searchBrand = {};
  277. this.reselectAll();
  278. },
  279. //
  280. //exit
  281. userout() {
  282. }
  283. },
  284. data() {
  285. return {
  286. // 分页工具条,显示当前第几页
  287. currentPage: 2,
  288. addBrand: {
  289. id: '',
  290. brandName: '',
  291. companyName: '',
  292. ordered: '',
  293. description: '',
  294. status: "0"
  295. },
  296. rules: {
  297. brandName: [
  298. {required: true, message: '请输入品牌名称', trigger: 'blur'},
  299. {min: 1, max: 15, message: '品牌名称长度在 1 到 15 个字符', trigger: 'blur'}
  300. ],
  301. companyName: [
  302. {required: true, message: '请输入企业名称', trigger: 'blur'},
  303. {min: 2, max: 15, message: '企业名称长度在 2 到 15 个字符', trigger: 'blur'}
  304. ]
  305. },
  306. // 控制对话框是否显示true表示显示,false表示不显示
  307. dialogVisible: false,
  308. // 第一行表单绑定的数据
  309. searchBrand: {
  310. brandName: '',
  311. companyName: '',
  312. status: ''
  313. },
  314. // 保存选中的数据
  315. multipleSelection: [],
  316. tableData: [{
  317. brandName: '华为P50',
  318. companyName: '华为科技有限公司',
  319. ordered: '1',
  320. status: '1'
  321. }],
  322. //选中的id
  323. idList: []
  324. }
  325. }
  326. });
  327. </script>
  328. </body>
  329. </html>