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

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

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



前言

这一章将介绍
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 set
			user_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 {

    @Autowired
    private 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 {

    @Autowired
    private 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。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

313YPHU3

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值