第二章 整合Mybatis和写CRUD的基本流程以及使用代码生成器生成Mapper等相关代码

提示:本博客个为人独立博客,不是权威,仅供参考!所有思路只做交流之用!如有不足之处,望各位在评论区友善指正。


文章目录

  • 第二章 整合Mybatis和写CRUD的基本流程以及使用代码生成器生成Mapper等相关代码
  • 前言
  • 一、整合Mybatis
    • 1.创建项目的时候已经自动引入了Mybatis
    • 2.在application.yml文件中配置Mybatis
    • 3.将xml、yml、properties等文件打包到target文件夹中
  • 二、写CURD的基本流程(以用户表为例)
    • 1.在entity中创建User实体类
    • 2.在mapper中创建UserMapper接口
    • 3.对应地创建UserMapper.xml
    • 4.在UserMapper.xml中写sql语句
    • 5.在service文件夹中创建UserService类
    • 6.在项目启动入口中装配一下mapper文件夹
    • 7.在controller文件夹中创建UserController类
    • 9.在application.yml中配置log日志,可以跟踪执行过程
    • 10.测试(运行程序后,打开以下网址并对照数据库中数据变化)
  • 三、使用代码生成器生成Mapper等相关代码
    • 1.提取代码生成器
    • 2.在db.properties中修改基本配置信息
    • 2.基本原理就是按照字段生成对应的代码
    • 3.运行程序后去输出路径下找到生成的代码
    • 4.将所有实体类拷贝至entity文件夹中
    • 5.引入hibernate-validator到pom.xml
    • 6.将所有mapper接口和xml文件拷贝至mapper文件夹中
    • 7.将所有service类拷贝至service文件夹中
    • 8.引入pagehelper到pom.xml
    • 9.在application.yml中配置pagehelper的方言
    • 9.在utils文件夹中创建一个实体类(用于分页)
  • 总结

前言

这一章将介绍
1.整合Mybatis
2.写CRUD的基本流程
3.使用代码生成器生成Mapper等相关代码


一、整合Mybatis

Mybatis的官方文档:
https://mybatis.org/mybatis-3/zh/index.html

1.创建项目的时候已经自动引入了Mybatis

2.在application.yml文件中配置Mybatis

3.将xml、yml、properties等文件打包到target文件夹中

二、写CURD的基本流程(以用户表为例)

1.在entity中创建User实体类

2.在mapper中创建UserMapper接口

3.对应地创建UserMapper.xml

拷贝头部代码

4.在UserMapper.xml中写sql语句

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sisyphus.mapper.UserMapper"><resultMap type="com.sisyphus.entity.User" id="User"><id column="id" property="id"/><result column="user_name"  property="userName"/><result column="password"  property="password"/><result column="name"  property="name"/><result column="phone"  property="phone"/><result column="type"  property="type"/><result column="remark"  property="remark"/></resultMap><insert id="create" keyProperty="id" useGeneratedKeys="true" parameterType="com.sisyphus.entity.User">insert into tb_user(user_name,password,name,phone,type,remark)values(#{userName},#{password},#{name},#{phone},#{type},#{remark})</insert><select id="query" resultMap="User">select * from tb_user<include refid="UserFindCriteria"/></select><select id="count" resultType="int">select count(1) from tb_user<include refid="UserFindCriteria"/></select><select id="detail" resultMap="User">select * from tb_user where id = #{id}</select><delete id="delete">delete from tb_user where id = #{id}</delete><update id="update">update tb_user setuser_name=#{userName},password=#{password},name=#{name},phone=#{phone},type=#{type},remark=#{remark}where id = #{id}</update><update id="updateSelective">update tb_user set<if test="userName != null and userName != ''"> user_name = #{userName}</if>,<if test="password != null and password != ''"> password = #{password}</if>,<if test="name != null and name != ''"> name = #{name}</if>,<if test="phone != null and phone != ''"> phone = #{phone}</if>,<if test="type != null">type = #{type}</if>,<if test="remark != null and remark != ''"> remark = #{remark}</if>where id = #{id}</update><sql id="UserFindCriteria"><where><if test="id != null">and id = #{id}</if><if test="userName != null and userName != ''">and user_name = #{userName}</if><if test="password != null and password != ''">and password = #{password}</if><if test="name != null and name != ''">and name = #{name}</if><if test="phone != null and phone != ''">and phone = #{phone}</if><if test="type != null">and type = #{type}</if><if test="remark != null and remark != ''">and remark = #{remark}</if></where></sql></mapper>

5.在service文件夹中创建UserService类

调用了UserMapper的接口

package com.sisyphus.service;import com.sisyphus.entity.User;
import com.sisyphus.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UserService {@Autowiredprivate UserMapper userMapper;public int create(User user){return userMapper.create(user);}public int delete(Integer id){return userMapper.delete(id);}public int update(User user){return userMapper.update(user);}public List<User> query(User user){return userMapper.query(user);}public User detail(Integer id){return userMapper.detail(id);}
}

6.在项目启动入口中装配一下mapper文件夹

7.在controller文件夹中创建UserController类

package com.sisyphus.controller;import com.sisyphus.entity.User;
import com.sisyphus.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/user")
public class UserController {@Autowiredprivate UserService userService;@GetMapping("create")public void create(){User user = new User();user.setUserName("admin");user.setName("admin");user.setPassword("123456");userService.create(user);}@GetMapping("delete")public void delete(Integer id){userService.delete(id);}@GetMapping("update")public void update(){User user = new User();user.setUserName("adminxxx");user.setName("adminxxx");user.setPassword("123456xxx");user.setId(1);userService.update(user);}@GetMapping("detail")public User detail(Integer id){return userService.detail(id);}@GetMapping("query")public PageInfo<User> query(User user){return userService.query(user);}
}

9.在application.yml中配置log日志,可以跟踪执行过程

10.测试(运行程序后,打开以下网址并对照数据库中数据变化)

1.create
http://localhost:8888/dormitory/user/create
2.update
http://localhost:8888/dormitory/user/update
3.query
http://localhost:8888/dormitory/user/query
4.delete
http://localhost:8888/dormitory/user/delete?id=1

三、使用代码生成器生成Mapper等相关代码

1.提取代码生成器

链接:https://pan.baidu.com/s/1okXO2qQpMVkmlrU8o6G3PA
提取码:kwir

2.在db.properties中修改基本配置信息

2.基本原理就是按照字段生成对应的代码

3.运行程序后去输出路径下找到生成的代码

4.将所有实体类拷贝至entity文件夹中

5.引入hibernate-validator到pom.xml

Hibernate Validator是Hibernate提供的一个开源框架,使用注解方式非常方便的实现服务端的数据校验。

6.将所有mapper接口和xml文件拷贝至mapper文件夹中

7.将所有service类拷贝至service文件夹中

8.引入pagehelper到pom.xml

PageHelper是一款好用的开源免费的Mybatis第三方物理分页插件

9.在application.yml中配置pagehelper的方言

9.在utils文件夹中创建一个实体类(用于分页)


总结

这里讲一下代码生成器的部分,为什么不把controller的代码也拷贝进去。因为我们在controller里面要做的就是和前端进行交互,至于做成什么样子,需要看需求比较灵活,所以不能直接简单粗暴地套用。

难点:
1.读Mybatis的官方文档
2.熟记CRUD的基本流程
跟着我的博客依葫芦画瓢并不难,程序成功运行这是最基本的。我每一章最后都会例出几个难点,但确切地来说不能说是难点,应该说是在程序运行成功后进一步深入学习的方向。除了这几个方向之外还有很多拓展的点,比如说xml、yml、properties这些文件类型会用于什么地方?CRUD是哪四个单词的缩写?hibernate-validator的语法以及pagehelper的语法等等等等,这些都是可以去深挖的点,但是每个人的空余时间就那么多,哪些值得去深挖,哪些稍作了解就可以,就看个人的选择了。

本来说好在五一假期前出这一章的,由于我拖延症犯了拖到了今天,我决定明天一定要完成下一章!
下一章给大家介绍一下本项目将要使用的前端模板Layui以及Axios。

【高校宿舍管理系统】第二章 整合Mybatis和写CRUD的基本流程以及使用代码生成器生成Mapper等相关代码相关推荐

  1. 【高校宿舍管理系统】第三章 Layui整合Axios

    第三章 Layui整合Axios 提示:本博客个为人独立博客,不是权威,仅供参考!所有思路只做交流之用!如有不足之处,望各位在评论区友善指正. 文章目录 第三章 Layui整合Axios 前言 一.下 ...

  2. 【高校宿舍管理系统】终章 完成主页以及项目总结

    终章 完成主页以及项目总结 提示:本博客个为人独立博客,不是权威,仅供参考!所有思路只做交流之用!如有不足之处,望各位在评论区友善指正. 文章目录 终章 完成主页以及项目总结 前言 一.完成主页 1. ...

  3. 【高校宿舍管理系统】第七章 机构管理和功能菜单权限分配

    第七章 机构管理和功能菜单权限分配 提示:本博客个为人独立博客,不是权威,仅供参考!所有思路只做交流之用!如有不足之处,望各位在评论区友善指正. 文章目录 第七章 机构管理和功能菜单权限分配 前言 一 ...

  4. 【高校宿舍管理系统】第一章 建立数据库以及项目框架搭建

    第一章 建立数据库以及项目框架搭建 提示:本博客个为人独立博客,不是权威,仅供参考!所有思路只做交流之用!如有不足之处,望各位在评论区友善指正. 文章目录 第一章 建立数据库以及项目框架搭建 前言 一 ...

  5. 【计算机毕业文章】基于SSM的高校宿舍管理系统的设计与实现

    摘 要 近年来, 伴随着互联网产业的快速发展和大力推广,"数字化"."信息化"为关键字的校园建设已经成为了各大院校发展的共同课题.宿舍是在大学校园里面学习和生活 ...

  6. 【高校宿舍管理系统】第八章 学生管理和楼宇管理以及寝室管理

    第八章 学生管理和楼宇管理以及宿舍管理 提示:本博客个为人独立博客,不是权威,仅供参考!所有思路只做交流之用!如有不足之处,望各位在评论区友善指正. 文章目录 第八章 学生管理和楼宇管理以及宿舍管理 ...

  7. 高校宿舍管理系统详细需求分析说明书

    (详细)高校宿舍管理系统需求分析说明书(文末-->获取原文档)   版本状态   版本 作者 参与者 起止日期 注释 审阅者 团队 版本 日期 签名 教学管理委员会 V1.1 2019.06.1 ...

  8. 【毕业设计】大学宿舍管理系统高校宿舍管理系统

    基于Springboot的大学宿舍管理系统&&高校宿舍管理系统 绪论 课题主要内容 系统建设目标 系统采用 B/S 架构,后端基于 Java 语言和 SpringBoot 框架的方式进 ...

  9. java计算机毕业设计高校宿舍管理系统演示视频2021源码+mysql数据库+系统+lw文档+部署

    java计算机毕业设计高校宿舍管理系统演示视频2021源码+mysql数据库+系统+lw文档+部署 java计算机毕业设计高校宿舍管理系统演示视频2021源码+mysql数据库+系统+lw文档+部署 ...

最新文章

  1. Socket中listen
  2. Photoshop 融合属性 Unity Shader
  3. awk rand函数问题
  4. HoloLens开发手记 - Unity之Spatial Sounds 空间声音
  5. android学习笔记17——对话框(PopupWindow)
  6. nginx核心模块ngx_http_core_module详解
  7. mysql简单增删改查(CRUD)
  8. Unity面试题精选(6)
  9. 记录表类型 oracle,[转]关于oracle的记录类型
  10. python数据分析-如何在业余时学数据分析?
  11. 上海人工智能实验室招聘NLP研究员和工程师啦,是事业单位呦~
  12. Android广告平台
  13. 使用 Colab 训练 Pytorch-Yolov4 (WongKinYiu版)
  14. halo博客:如何加快搜索引擎收录网站速度
  15. CCS之最少拍控制器设计
  16. 编写仿supersu的权限管理工具(aosp11 root、实现aosp系统内置wifi、root管理apk)
  17. python去除重复单词_Python重复的单词
  18. 全站替换https攻略
  19. c++实现三国杀小游戏
  20. html5 strokestyle,HTML canvas strokeStyle 属性 - JavaScript 参考手册 - 自强学堂

热门文章

  1. 小酌一下:Pycharm 2019.1.3 64位版本破解
  2. Sublime Text 3 设置
  3. systemctl自定义service
  4. mysql 用户权限设置【转】
  5. 城市轨道交通运营票务管理论文_城市轨道交通运营企业的票务组织管理
  6. 关于Cortex-M3处理器内核中断异常处理机制你了解多少?
  7. 网络协议:TCP拥塞控制
  8. (85)FPGA约束有哪些-面试必问(九)(第17天)
  9. (81)FPGA复位激励(task)
  10. (01)System Verilog验证理论