• MyBatis是什么?使用它作为持久层框架有什么优点?



    一、前言

    在阅读本篇文章之前,我先提问一下:

    • mybatis映射文件、核心配置文件、properties配置文件、日志文件到底在这个框架中各自的功能是什么?它们在文件夹中的位置该如何设定?
    • resultMap、resultType有什么不同?什么时候要使用它们?
    • 遇到像模糊查询时,我们该如何正确操作?
    • {} 、${} 之间的区别是什么?各自使用的情景是什么?

    • mybatis缓存是什么?
    • 分步查询会不会?
    • ...

    废话不多说,满满的干货,赶快来看看吧~

    二、基本介绍

    一句话总结一下,MyBatis是一个基于Java的持久层框架,是一个半自动的ORM框架。那么可爱的它具有哪些很好的特性呢?

    • 支持定制化 SQL、存储过程以及高级映射
    • 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集的操作
    • 可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(实体类)映射成数据库中的记录

    那么它有哪些优点呢?

    • 轻量级,性能出色
    • SQL 和 Java 编码分开,功能边界清晰。Java代码专注业务、SQL语句专注数据

    好了,了解这些就差不多了,接下来进入Mybatis的知识世界!

    三、搭建开发环境

    首先要做的就是引入依赖,具体的pom.xml如下:

    1. <packaging>jarpackaging>
    2. <dependencies>
    3. <dependency>
    4. <groupId>org.mybatisgroupId>
    5. <artifactId>mybatisartifactId>
    6. <version>3.5.7version>
    7. dependency>
    8. <dependency>
    9. <groupId>junitgroupId>
    10. <artifactId>junitartifactId>
    11. <version>4.12version>
    12. <scope>testscope>
    13. dependency>
    14. <dependency>
    15. <groupId>mysqlgroupId>
    16. <artifactId>mysql-connector-javaartifactId>
    17. <version>8.0.28version>
    18. dependency>
    19. <dependency>
    20. <groupId>org.projectlombokgroupId>
    21. <artifactId>lombokartifactId>
    22. <version>1.18.10version>
    23. dependency>
    24. <dependency>
    25. <groupId>log4jgroupId>
    26. <artifactId>log4jartifactId>
    27. <version>1.2.17version>
    28. dependency>
    29. dependencies>

    这里需要注意的就是MySQL的驱动依赖了,我直接跟着视频走,使用的是版本5.7,结果可想而知,报错了,呜呜呜~

    接着就是创建实体类、mapper接口、核心配置文件、映射文件,我给大家依次演示一下如何使用:

    实体类:与数据库字段相关联,这个数据库在我的石墨文档第一个位置,大家可以自己运行一下SQL代码试一试

    1. @Data
    2. @AllArgsConstructor
    3. @NoArgsConstructor
    4. public class User {
    5. private Integer id;
    6. private String username;
    7. private String password;
    8. private Integer age;
    9. private String sex;
    10. private String email;
    11. }

    mapper接口:可以从官方文档中找案例复制下来——Mybatis官方文档

    1. mapper
    2. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    4. <mapper namespace="com.cabbage.mappers.UserMapper">
    5. mapper>

    核心配置文件:跟mapper接口一样,也可以从官方中复制下来

    1. configuration
    2. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    3. "http://mybatis.org/dtd/mybatis-3-config.dtd">
    4. <configuration>
    5. <properties resource="jdbc.properties"/>
    6. <typeAliases>
    7. <package name="com.cabbage.pojo"/>
    8. typeAliases>
    9. <environments default="development">
    10. <environment id="development">
    11. <transactionManager type="JDBC"/>
    12. <dataSource type="POOLED">
    13. <property name="driver" value="${jdbc.driver}"/>
    14. <property name="url" value="${jdbc.url}"/>
    15. <property name="username" value="${jdbc.username}"/>
    16. <property name="password" value="${jdbc.password}"/>
    17. dataSource>
    18. environment>
    19. environments>
    20. <mappers>
    21. <package name="com.cabbage.mappers"/>
    22. mappers>
    23. configuration>

    最后再来一个日记文件记录信息以及jdbc.properties配置文件

    日志文件:可以当做是一种模板

    1. log4j:configuration SYSTEM "log4j.dtd">
    2. <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    3. <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
    4. <param name="Encoding" value="UTF-8"/>
    5. <layout class="org.apache.log4j.PatternLayout">
    6. <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n"/>
    7. layout>
    8. appender>
    9. <logger name="java.sql">
    10. <level value="debug"/>
    11. logger>
    12. <logger name="org.apache.ibatis">
    13. <level value="info"/>
    14. logger>
    15. <root>
    16. <level value="debug"/>
    17. <appender-ref ref="STDOUT"/>
    18. root>
    19. log4j:configuration>

    jdbc.properties配置文件:配置数据库的连接信息——用户名、密码等等

    1. jdbc.driver=com.mysql.cj.jdbc.Driver
    2. jdbc.url=jdbc:mysql://localhost:3306/mybatis
    3. jdbc.username=
    4. jdbc.password=

    说了这么多配置文件,大家估计都懵了,这些文件各自放在什么目录下呢?大家可以参考一下我的位置

    好了,各位小伙伴们,基本的环境搭建已经完成了,熟悉mybatis的小伙伴们看到这些文件中的属性,也知道各自是什么功能;不知道的小伙伴也不要着急,从案例中慢慢就会解密他们的各自功能以及使用方法。

    四、核心配置文件

    让我们回过头来看看刚才搭建的核心配置文件,如何使用?

    • 1、

    • 2、

    • 3、

      需要特别注意的是:在resources文件夹下建包时,格式应该是下图的样式

    五、mapper映射文件

    先看一下映射文件的命名规则:

    • 表所对应的实体类的类名+Mapper.xml

    例如:表t_user,映射的实体类为User,所对应的映射文件为UserMapper.xml 因此一个映射文件对应一个实体类,对应一张表的操作

    • MyBatis映射文件用于编写SQL,访问以及操作表中的数据
    • MyBatis映射文件存放的位置是src/main/resources/mappers目录

    此外,MyBatis中可以面向接口操作数据,要保证两个一致:

    • mapper接口的全类名和映射文件的命名空间(namespace)保持一致
    • mapper接口中方法的方法名和映射文件中编写SQL的标签的id属性保持一致

    写一个代码演示一下:查询表中id=3的user

    1. <select id="selectById" resultType="User">
    2. SELECT * FROM t_user WHERE id = 3
    3. select>
    1. public interface UserMapper {
    2. User selectById();
    3. }

    运行结果完全正确:

    需要注意的是:

    • 查询的标签select必须设置属性resultTyperesultMap,用于设置实体类和数据库表的映射
    • resultType:自动映射,用于属性名和表中字段名一致的情况
    • resultMap:自定义映射,用于一对多或多对一或字段名和属性名不一致的情况,这个后面就会讲到
    • 当查询的数据为多条时,不能使用实体类作为返回值,只能使用集合,否则会抛出异常TooManyResultsException;但是若查询的数据只有一条,可以使用实体类或集合作为返回值

    六、参数值的两种方式(重点)

    • MyBatis获取参数值的两种方式:${}和#{}

    • ${}的本质就是字符串拼接,#{}的本质就是占位符赋值

    • ${}使用字符串拼接的方式拼接sql,若为字符串类型或日期类型的字段进行赋值时,需要手动加单引号;但是#{}使用占位符赋值的方式拼接sql,此时为字符串类型或日期类型的字段进行赋值时,可以自动添加单引号

    在学习中,原本把参数值分为五种情况,最后介绍了注解@Param的使用,最后就把这五种情况归纳为两种情况,那就来介绍这两种情况吧!

    • 1、实体类类型参数
    • 2、使用@Param标识参数,使用该注解时,以@Param注解的值为键,以参数为值,或者以param1、param2为键,以参数为值

    下面找一些具有代表性的代码,方便以后的回忆,小伙伴们也可以看看这些代码是否还知道是什么意思呀,测试代码就不写了

    七、select的重点介绍

    1. 查询单个数据,这个很简单了
    2. <select id="getCount" resultType="integer">
    3. select count(*) from t_user
    4. select>
    5. 查询一条数据为map集合,会把查询到的一个对象封装在map中
    6. <select id="getUserByIdToMap" resultType="map">
    7. select * from t_user where id = #{id}
    8. select>
    9. 查询所有数据放入到map集合中
    10. //方式一: List> getAllUsersToMap();
    11. @MapKey("id"),
    12. Map getAllUsersToMap();

    查询所有数据放入到map集合中这种方式,进行代码测试一下:

    1. @Test
    2. public void test4() throws IOException {
    3. SqlSession sqlSession = GetSqlSession.getSqlSession();
    4. UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    5. // List<Map<String, Object>> mapList = mapper.getAllUsersToMap();
    6. Map<String, Object> map = mapper.getAllUsersToMap();
    7. // System.out.println(mapList);
    8. System.out.println(map);
    9. }

    看一下查询的部分结果:

    八、特殊SQL的查询

    • 1、模糊查询
    1. <select id="getUserByLike" resultType="User">
    2. select * from t_user where username like "%"#{username}"%"
    3. select>
    • 2、批量删除
    1. <delete id="deleteMore">
    2. delete from t_user where id in (${ids})
    3. delete>

    代码演示一下:

    1. @Test
    2. public void test2() throws IOException {
    3. SqlSession sqlSession = GetSqlSession.getSqlSession();
    4. SQLMapper mapper = sqlSession.getMapper(SQLMapper.class);
    5. Integer result = mapper.deleteMore("7,8,9");
    6. System.out.println(result);
    7. }
    8. 复制代码

    运行结果: [图片上传失败...(image-4c6888-1659663334477)]

    • 3、动态设置表名
    1. <select id="getAllUserByTableName" resultType="User">
    2. select * from ${tableName}
    3. select>
    • 4、添加功能获取自增的主键
    1. <insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
    2. insert into t_user values (null,#{username},#{password},#{age},#{sex},#{email})
    3. insert>

    代码演示一下:

    1. @Test
    2. public void test4() throws IOException {
    3. SqlSession sqlSession = GetSqlSession.getSqlSession();
    4. SQLMapper mapper = sqlSession.getMapper(SQLMapper.class);
    5. User user = new User(null, "cabbage8"
    6. , "123454", 23, "男"
    7. , "cabbage8@qq.com");
    8. int result = mapper.insertUser(user);
    9. System.out.println(result);
    10. System.out.println(user.getId());
    11. }

    运行结果:

    九、自定义resultMap

    • 1、resultMap处理字段和属性的映射关系,若字段名和实体类中的属性名不一致,则可以通过resultMap设置自定义映射
    1. <resultMap id="mapEmp" type="Emp">
    2. <id property="eid" column="eid"/>
    3. <result property="empName" column="emp_name"/>
    4. resultMap>
    5. <select id="getAllEmp" resultMap="mapEmp">
    6. select eid,emp_name,age,sex,email,did from t_emp
    7. --或者select eid,emp_name empName,age,sex,email,did from t_emp
    8. select>

    resultMap:设置自定义映射属性:

    • id:表示自定义映射的唯一标识
    • type:查询的数据要映射的实体类的类型子标签:
    • id:设置主键的映射关系

    result:设置普通字段的映射关系

    • property:设置映射关系中实体类中的属性名
    • column:设置映射关系中表中的字段名

    当然,若字段名和实体类中的属性名不一致,但是字段名符合数据库的规则(使用),实体类中的属性名符合Java的规则(使用驼峰)时,可以在MyBatis的核心配置文件中设置一个全局配置信息mapUnderscoreToCamelCase,在查询表中数据时,自动将类型的字段名转换为驼峰 例如:字段名user_name,设置了mapUnderscoreToCamelCase,此时字段名就会转换为userName

    • 2、多对一映射处理,这是重点!

    我们需要修改一下核心配置文件的内容,添加一些配置(这些配置在官方文档中都有介绍):

    1. <settings>
    2. <setting name="mapUnderscoreToCamelCase" value="true"/>
    3. <setting name="lazyLoadingEnabled" value="true"/>
    4. settings>

    三种方式的介绍:

    1. <resultMap id="getEmpAndDeptMap1" type="Emp">
    2. <id property="eid" column="eid"/>
    3. <result property="empName" column="emp_name"/>
    4. <result property="dept.did" column="did"/>
    5. <result property="dept.deptName" column="dept_name"/>
    6. resultMap>
    7. <resultMap id="getEmpAndDeptMap2" type="Emp">
    8. <id property="eid" column="eid"/>
    9. <result property="empName" column="emp_name"/>
    10. <association property="dept" javaType="Dept">
    11. <result property="did" column="did"/>
    12. <result property="deptName" column="dept_name"/>
    13. association>
    14. resultMap>
    15. <resultMap id="getEmpAndDeptByStepMap" type="Emp">
    16. <id property="eid" column="eid"/>
    17. <result property="empName" column="emp_name"/>
    18. <association property="dept"
    19. select="com.cabbage.mappers.DeptMapper.getEmpAndDeptByStepTwo"
    20. column="did"
    21. fetchType="eager">
    22. association>
    23. resultMap>
    24. <select id="getEmpAndDeptByStepOne" resultMap="getEmpAndDeptByStepMap">
    25. select * from t_emp where eid = #{eid}
    26. select>

    方式三中DeptMapper.xml对应的分步查询的第二步:

    1. <select id="getEmpAndDeptByStepTwo" resultType="Dept">
    2. select * from t_dept where did = #{did}
    3. select>

    老师说,方式三在实际开发中使用的最多,方式三是一种懒加载,那咱们就测试一下方式三:

    1. @Test
    2. public void test3() throws IOException {
    3. SqlSession sqlSession = SqlSessionUtil.getSqlSession();
    4. EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
    5. Emp emp = mapper.getEmpAndDeptByStepOne(1);
    6. System.out.println(emp.getEmpName());
    7. System.out.println("----------------");
    8. System.out.println(emp.getDept());
    9. }

    运行结果:

    我们把eager改成lazy后的运行结果:

    • 3、一对多映射处理,这是重点!
    1. <resultMap id="getDeptAndEmpMap" type="Dept">
    2. <result property="did" column="did">result>
    3. <result property="deptName" column="dept_name">result>
    4. <collection property="emps" ofType="Emp">
    5. <result property="eid" column="eid">result>
    6. <result property="empName" column="emp_name">result>
    7. <result property="age" column="age">result>
    8. <result property="email" column="email">result>
    9. <result property="sex" column="sex">result>
    10. <result property="did" column="did">result>
    11. collection>
    12. resultMap>
    13. <select id="getDeptAndEmp" resultMap="getDeptAndEmpMap">
    14. select * from t_dept t1 left join t_emp t2 on t1.did = t2.did where t1.did = #{did}
    15. select>
    16. <resultMap id="getDeptAndEmpOneMap" type="Dept">
    17. <id property="did" column="did">id>
    18. <result property="deptName" column="dept_name">result>
    19. <collection property="emps"
    20. select="com.cabbage.mappers.EmpMapper.getDeptAndEmpByStepTwo"
    21. column="did">
    22. collection>
    23. resultMap>
    24. <select id="getDeptAndEmpOne" resultMap="getDeptAndEmpOneMap">
    25. select * from t_dept where did = #{did}
    26. select>

    方式二中EmpMapper.xml对应的分步查询的第二步:

    1. <select id="getDeptAndEmpByStepTwo" resultType="Emp">
    2. select * from t_emp where did= #{eid}
    3. select>

    写个测试代码:

    1. @Test
    2. public void test4() throws IOException {
    3. SqlSession sqlSession = SqlSessionUtil.getSqlSession();
    4. DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
    5. Dept dept = mapper.getDeptAndEmpOne(1);
    6. System.out.println(dept);
    7. }

    十、动态SQL

    Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它存在的意义是为了解决拼接SQL语句字符串时的痛点问题。接下来就为大家逐一介绍他们的使用代码

    • 1、if的使用:if标签可通过test属性的表达式进行判断,若表达式的结果为true,则标签中的内容会执行;反之标签中的内容不会执行
    1. <select id="getUserByCondition" resultType="User">
    2. select * from t_user where 1=1
    3. <if test="username !=null and username != ''">
    4. and username = #{username}
    5. if>
    6. <if test="password != null and password != ''">
    7. and password = #{password}
    8. if>
    9. select>\

    这个语句是什么意思呢?其实就是我们传入一个实体对象,对象的username、password属性不为空或者不为null,就查询。对应的SQL语句是:select * from t_user where 1=1 and username = ? and password = ?

    • 2、where的使用
    1. <select id="getUserByWhere" resultType="User">
    2. select * from t_user
    3. <where>
    4. <if test="username != null and username != ''">
    5. and username = #{username}
    6. if>
    7. <if test="password != null and password != ''">
    8. and password = #{password}
    9. if>
    10. where>
    11. select>
    12. \

    where和if一般结合使用: 1、若where标签中的if条件都不满足,则where标签没有任何功能,即不会添加where关键字 2、若where标签中的if条件满足,则where标签会自动添加where关键字,并将条件最前方多余的 and去掉 3、注意:where标签不能去掉条件最后多余的and

    • 3、trim的使用
    1. <select id="getUserByTrim" resultType="User">
    2. select * from t_user
    3. <trim prefix="where" suffixOverrides="and">
    4. <if test="username != null and username != ''">
    5. username = #{username} and
    6. if>
    7. <if test="password != null and password != ''">
    8. password = #{password} and
    9. if>
    10. trim>
    11. select>
    12. \\\

    trim用于去掉或添加标签中的内容,常用属性: 1、prefix:在trim标签中的内容的前面添加某些内容 2、prefixOverrides:在trim标签中的内容的前面去掉某些内容 3、suffix:在trim标签中的内容的后面添加某些内容 4、suffixOverrides:在trim标签中的内容的后面去掉某些内容

    • 4、choose、when、otherwise的使用,相当于if...else if..else,满足一个if条件就不会执行下一个条件了
    1. <select id="getUserByIfElse" resultType="User">
    2. select * from t_user
    3. <where>
    4. <choose>
    5. <when test="username != null and username != ''">
    6. username = #{username}
    7. when>
    8. <when test="password != null and password != ''">
    9. password = #{password}
    10. when>
    11. <otherwise>
    12. id = 3
    13. otherwise>
    14. choose>
    15. where>
    16. select>
    • 5、foreach的使用
    1. <insert id="insertByForeach">
    2. insert into t_user values
    3. <foreach collection="users" item="item" separator=",">
    4. (null,#{item.username},#{item.password},#{item.age},#{item.sex},#{item.email})
    5. foreach>
    6. insert>
    7. <delete id="deleteByForeach">
    8. delete from t_user where id in
    9. <foreach collection="id" open="(" close=")" separator="," item="item">
    10. #{item}
    11. foreach>
    12. delete>

    一起来看看其中的属性: 1、collection:设置要循环的数组或集合 2、item:表示集合或数组中的每一个数据 3、separator:设置循环体之间的分隔符 4、open:设置foreach标签中的内容的开始符 5、close:设置foreach标签中的内容的结束符

    • 6、SQL片段的使用,可以记录一段公共sql片段,在使用的地方通过include标签进行引入
    1. <sql id="selectAll">
    2. id,username,password,age,sex,email
    3. sql>
    4. select <include refid="selectAll">include> from t_user

    十一、MyBatis的缓存

    对于这一部分的讲解,我不打算举例代码,因为相信学过mybatis的小伙伴们,看看对缓存的文字介绍,就可以回想起缓存所具有的特点,那么咱们就先从一级缓存开始吧!

    一级缓存是SqlSession级别的,通过同一个SqlSession查询的数据会被缓存,下次查询相同的数据,就会从缓存中直接获取,不会从数据库重新访问。把握两点:一是同一个sqlsession,二是查询相同的数据。缓存影响着我们的查询速度,但不影响我的查询数据!

    使一级缓存失效的四种情况:

    • 不同的SqlSession对应不同的一级缓存
    • 同一个SqlSession但是查询条件不同
    • 同一个SqlSession两次查询期间执行了任何一次增删改操作(注意一下)
    • 同一个SqlSession查询期间手动清空了缓存(sqlSession.clearCache();)

    还是演示一下手动清空缓存的情况吧:

    1. @Test
    2. public void test1() throws IOException {
    3. InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
    4. SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
    5. SqlSessionFactory factory = factoryBuilder.build(inputStream);
    6. SqlSession sqlSession = factory.openSession(true);
    7. UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    8. UserMapper mapper1 = sqlSession.getMapper(UserMapper.class);
    9. User user = mapper.selectById(1);
    10. sqlSession.clearCache();
    11. User user1 = mapper1.selectById(1);
    12. System.out.println(user);
    13. System.out.println(user1);
    14. }

    运行结果:

    二级缓存是SqlSessionFactory级别,通过同一个SqlSessionFactory创建的SqlSession查询的结果会被缓存;此后若再次执行相同的查询语句,结果就会从缓存中获取

    二级缓存开启的条件:

    • 在核心配置文件中,设置全局配置属性cacheEnabled="true",默认为true,不需要设置
    • 在映射文件中设置标签
    • 二级缓存必须在SqlSession关闭或提交之后有效(不要忘记了哦)
    • 查询的数据所转换的实体类类型必须实现序列化的接口(implements Serializable)

    使二级缓存失效的情况:

    • 两次查询之间执行了任意的增删改,会使一级和二级缓存同时失效

    演示一下二级缓存:

    1. @Test
    2. public void test2() throws IOException {
    3. InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
    4. SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
    5. SqlSessionFactory factory = factoryBuilder.build(inputStream);
    6. SqlSession sqlSession = factory.openSession(true);
    7. UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    8. User user1 = mapper.selectById(1);
    9. System.out.println(user1);
    10. sqlSession.close();//关闭一个后,放入了二级缓存中
    11. SqlSession sqlSession1 = factory.openSession(true);
    12. UserMapper mapper1 = sqlSession1.getMapper(UserMapper.class);
    13. User user = mapper1.selectById(1);
    14. System.out.println(user);
    15. sqlSession1.close();
    16. }

    运行结果:

    二级缓存的相关配置,这一部分老师没有细讲,摘录下来,了解一下。在mapper配置文件中添加的cache标签可以设置一些属性:

    • eviction属性:缓存回收策略 LRU(Least Recently Used) – 最近最少使用的:移除最长时间不被使用的对象。 FIFO(First in First out) – 先进先出:按对象进入缓存的顺序来移除它们。 SOFT – 软引用:移除基于垃圾回收器状态和软引用规则的对象。 WEAK – 弱引用:更积极地移除基于垃圾收集器状态和弱引用规则的对象。 默认的是 LRU。
    • flushInterval属性:刷新间隔,单位毫秒,默认情况是不设置,也就是没有刷新间隔,缓存仅仅调用语句时刷新
    • size属性:引用数目,正整数代表缓存最多可以存储多少个对象,太大容易导致内存溢出
    • readOnly属性:只读,true/false true:只读缓存;会给所有调用者返回缓存对象的相同实例。因此这些对象不能被修改。这提供了很重要的性能优势。 false:读写缓存;会返回缓存对象的拷贝(通过序列化)。这会慢一些,但是安全,因此默认是false。

    最后来讲一讲MyBatis缓存查询的顺序:

    • 先查询二级缓存,因为二级缓存中可能会有其他程序已经查出来的数据,可以拿来直接使用。
    • 如果二级缓存没有命中,再查询一级缓存
    • 如果一级缓存也没有命中,则查询数据库SqlSession关闭之后,一级缓存中的数据会写入二级缓存

    十二、创建逆向工程

    什么是逆向工程呢?就是先创建数据库表,由框架负责根据数据库表,反向生成如下资源:

    • Java实体类、mapper接口、mapper映射文件

    那么就开始根据步骤创建逆向工程吧!

    首先需要添加依赖和插件,这里有些依赖使用已经在前面使用过了,大家可以注意一下哦

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.mybatisgroupId>
    4. <artifactId>mybatisartifactId>
    5. <version>3.5.7version>
    6. dependency>
    7. <dependency>
    8. <groupId>org.projectlombokgroupId>
    9. <artifactId>lombokartifactId>
    10. <version>1.18.10version>
    11. dependency>
    12. <dependency>
    13. <groupId>mysqlgroupId>
    14. <artifactId>mysql-connector-javaartifactId>
    15. <version>8.0.28version>
    16. dependency>
    17. <dependency>
    18. <groupId>junitgroupId>
    19. <artifactId>junitartifactId>
    20. <version>4.12version>
    21. <scope>testscope>
    22. dependency>
    23. <dependency>
    24. <groupId>log4jgroupId>
    25. <artifactId>log4jartifactId>
    26. <version>1.2.17version>
    27. dependency>
    28. dependencies>
    29. <build>
    30. <plugins>
    31. <plugin>
    32. <groupId>org.mybatis.generatorgroupId>
    33. <artifactId>mybatis-generator-maven-pluginartifactId>
    34. <version>1.3.0version>
    35. <dependencies>
    36. <dependency>
    37. <groupId>org.mybatis.generatorgroupId>
    38. <artifactId>mybatis-generator-coreartifactId>
    39. <version>1.3.2version>
    40. dependency>
    41. <dependency>
    42. <groupId>com.mchangegroupId>
    43. <artifactId>c3p0artifactId>
    44. <version>0.9.2version>
    45. dependency>
    46. <dependency>
    47. <groupId>mysqlgroupId>
    48. <artifactId>mysql-connector-javaartifactId>
    49. <version>8.0.28version>
    50. dependency>
    51. dependencies>
    52. plugin>
    53. plugins>
    54. build>

    接着创建MyBatis的核心配置文件以及逆向工程的配置文件,第一个配置文件已经讲过了,来看看逆向工程配置文件如何创建(使用时,发现实体类属性值补全,后来完善了一下视频中的笔记,加了一个配置):

    1. generatorConfiguration
    2. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
    3. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
    4. <generatorConfiguration>
    5. <context id="DB2Tables" targetRuntime="MyBatis3">
    6. <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis"
    7. userId="root" password="">
    8. <property name="nullCatalogMeansCurrent" value="true"/>
    9. jdbcConnection>
    10. <javaModelGenerator targetPackage="com.cabbage.pojo" targetProject=".\src\main\java">
    11. <property name="enableSubPackages" value="true"/>
    12. <property name="trimStrings" value="true"/>
    13. javaModelGenerator>
    14. <sqlMapGenerator targetPackage="com.cabbage.mappers" targetProject=".\src\main\resources">
    15. <property name="enableSubPackages" value="true"/>
    16. sqlMapGenerator>
    17. <javaClientGenerator type="XMLMAPPER" targetPackage="com.cabbage.mappers"
    18. targetProject=".\src\main\java">
    19. <property name="enableSubPackages" value="true"/>
    20. javaClientGenerator>
    21. <table tableName="t_user" domainObjectName="User"/>
    22. context>
    23. generatorConfiguration>

    需要注意的是:逆向工程配置文件的数据库相关的信息还是需要自己修改配置的

    最后执行插件,如图所示:

    使用逆向工程生成的目录给大家看一下,可以看出跟我们自己手动创建的一模一样,是不是很简便呢?

    好了,本文对MyBatis的知识总结就到这里了,在复习的过程中,对使用逆向工程后的方法没有具体举例介绍,因为方法实在是太多了;还有一个知识点就是分页插件的使用也没有在本文中介绍,原因也是提供的方法很多,不方便介绍,自己认为最好的办法就是找一个小项目,在实际开发中慢慢熟练使用这些框架给的方法!

  • 相关阅读:
    从一道面试题开始学习C++标准库提供的并发编程工具
    owt-server源码剖析(六)--集群分布及任务调度
    76-Java的泛型深入、自定义泛型、泛型通配符、上下限
    2023最新SSM计算机毕业设计选题大全(附源码+LW)之java智慧社区管理系统jby69
    23年7/8月前端面试题总结
    SAP MM 事务代码VL04为STO创建外向交货单
    【Linux基础】Linux的基本指令使用(超详细解析,小白必看系列)
    mysql添加普通索引(简单使用)
    哈希加盐算法
    【Vue.js】使用Element搭建首页导航&左侧菜单
  • 原文地址:https://blog.csdn.net/LBWNB_Java/article/details/126171737