• mybatis各阶段的详解


    1 本阶段的需要注意的几个点

    1,首先是在核心配置文件里面的内容:

    配置的顺序,不配则不用管,配则必须按顺序来!!!!
    properties?,
    settings?,
    typeAliases?,
    typeHandlers?,
    objectFactory?,
    objectWrapperFactory?,
    reflectorFactory?,
    plugins?,environments?,
    databaseIdProvider?,
    mappers?.-->

    比如我们在引入了jdbc的配置文件使用了properties标签,引入jdbc有什么好处?,可以在配置文件中统一管理
    内容而不是在很多个文件改来改去,而且在核心配置文件中把数据库连接相关的写死,显然是硬编码的所以我们用配置文件代替 nice!!!

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/mybatis_db
    jdbc.username=root
    jdbc.password=kobedu
    #为了解决重名的问题和风险 使用前缀jdbc. 见名示意
    

    你可能注意到了上面的写法 用前缀jdbc. 可以很好地将他们与其他的变量区分开,(可以从名字很容易看出是jdbc相关的数据,不至于和同名变量搞混因为username这种可能
    不止会出现在数据库的连接)

    1,在核心配置文件中引入jdbc的配置文件

        <properties resource="jdbc.properties"/>
             <typeAliases>
                  <package name="com.kobedu.com.kobedu.mybatis.pojo"/>
             </typeAliases>
    
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/> 
                <dataSource type="POOLED">
                    <property name="driver" value="${jdbc.driver}"/>
                    <property name="url"
                              value="${jdbc.url}"/>
                    <property name="username" value="${jdbc.username}"/>
                    <property name="password" value="${jdbc.password}"/>
                </dataSource>
            </environment>
        </environments>
    
    

    上面的代码中引入配置文件的部分为
    可以从上面看到写法:

    2,在核心的配置文件中设置类的别名

    下面这段就是用来设置类的别名:

    
    <typeAliases>
        <package name="com.kobedu.com.kobedu.mybatis.pojo"/>
    </typeAliases> 
    

    那么问题来了,为什么要有类的别名这种操作??

    因为在映射文件中每次都要写全类名显然有点麻烦比如下面这样:

    
    <mapper namespace="com.kobedu.com.kobedu.com.kobedu.mybatis.mapper.UserMapper">
        <insert id="insertUser">
            insert into t_user values (null,"旺财","20")
        </insert>
    </mapper>
    

    一个项目是会有很多个映射文件的为了方便,所以类别名就出现了。可以在核心配置文件写接口类和对应的别名

    
    <typeAliases>
        <typeAlias type="com.kobedu.com.kobedu.com.kobedu.mybatis.pojo.User" alias="User"/>
    </typeAliases>
    

    这样就可以在映射文件的命名空间里可以直接写User(对大小没有要求也可以是user; 其实可以比这更加简单,也是我们在实际开发中常用的写法
    就是将整个包写成别名的形式,如果不写alias属性默认为类名(不区分大小写),这样就容易多了,我们只需一行代码,便可以在所有的映射文件命名空间
    中直接写对应的类名

    
    <typeAliases>
        <typeAlias pack="com.kobedu.com.kobedu.mybatis.pojo"/>
    </typeAliases>
    

    3、在核心配置中引入映射文件

    引入核心的配置文件

    <mappers>
        <mapper resource="mappers/UserMapper.xml"></mapper>
    </mappers>
    

    首先需要思考的这里是映射文件的引入,我们正常的一个项目的数据库是有很多个表组成的那么每一张表对应一个mapper接口,每个接口对应一个映射文件,那么就需要导入大量的映射文件,还容易漏掉-->

    
    <mappers>
        <package name="com.kobedu.com.kobedu.mybatis.mapper"/>
    </mappers>
    

    上面这种以包的形式的导入非常方便,不用每次新建一个接口就要导入它的映射文件,但是上面这种写法需要注意一些问题:

    1. 在当前的resource目录下建包 (新建文件 然后是文件名 com/kobedu/com.kobedu.mybatis/mapper -->enter)
    2. 用上面方式建的包必须要和mapper接口所在的包同名
    3. 在包里的映射文件必须和接口类同名

    4、查询时需要返回类型

    如果你在映射文件中编写查询语句的sql,但是粗心的你忘记了设置返回类型会在控制台抛异常且会看到这样的说明:
    It's likely that neither a Result Type nor a Result Map was specified.

    下面只是指定返回类型的一种方式:resultType,还有 resultMap
    它们的区别:

    1. resultType : 设置默认的映射关系(字段名和属性一致的情况)
    2. resultMap : 设置自定义的映射关系(字段名和属性名不一致或一对多或多对一)

    查询的标签必须指定resultType或resultMap

    
    <select id="getUserList" resultType="com.kobedu.com.kobedu.com.kobedu.mybatis.pojo.User">
        select * from t_user
    </select>
    

    2、mybtis获取参数的两种方式(重点)

    com.kobedu.mybatis 获取参数的两种方式:${} 和 #{}

    1. ${}:本质字符串的拼接(JDBC原生中的字符串拼接)
    2. 和 #{}:本质占位符
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//com.kobedu.mybatis.org//DTD Mapper 3.0//EN"
            "http://com.kobedu.mybatis.org/dtd/com.kobedu.mybatis-3-mapper.dtd">
    <mapper namespace="com.kobedu.com.kobedu.mybatis.mapper.ParameterMapper">
    
        <select id="getUserById" resultType="User">
            select * from t_user where userName=#{username}
        </select>
    
    </mapper>
    

    上面是使用了 #{}写法相当于原生jdbc的占位符,这个前面已经提到过了所以不多赘述,需要注意的是#{}里面的变量名可以是任意的username规范显然很好,但是aaaa也没错因为只是用来占位的;
    还有就是在使用${}时注意''单引号问题,因为${}是字符拼接的方式,所以需要注意!!

    传输参数时有多个参数时

        <select id="getUserByUsernameAndPassword" resultType="User">
           select * from t_user where userName=#{username} and password=#{password}
       </select>
    

    在测试代码里通过传入两个参数分别为 username和password 但是在上面代码的(映射文件里的部分代码)执行失败,(sql语句未能解析)
    报错:
    Cause: org.apache.ibatis.binding.BindingException: Parameter 'username' not found. Available parameters are [arg1, arg0, param1, param2]

    可以从错误提示的信息不难发现我们的参数在映射文件里未能真正地接受到,可以用[arg1, arg0, param1, param2] 的方式获取,mybatis将参数放到map容器可以通过建arg0,agr1..的方式
    获取参数(也可以是param1,param2..)
    将上面的代码改动:

         <select id="getUserByUsernameAndPassword" resultType="User">
            select * from t_user where userName=#{arg0} and password=#{arg1}
        </select>
    

    需要注意的是:使用${}时需要手动添加''才能正常访问,因为他的处理方式是字符串的拼接

    class test{
        @Test
        public void selectUser(){
            ParameterMapper parameterMapper = SqlSessionUtils.getSqlSession().getMapper(ParameterMapper.class);
            User userById = parameterMapper.getUserByUsernameAndPassword("旺财","cwlz");
            System.out.println(userById);
        }
    }
    

    做了改动之后结果很感人!!
    User{id=6, userName='旺财', age=20, password='cwlz'}

    若mapper接口的参数有多个时可以手动添加到map集合

    可以直接通过键访问相对应的值(通过自己的方式访问到数据,上面的形式是mybatis默认提供的map和mybatis默认的提取指的方式 arg0,arg2...)
    当需要传多个参数时将他们放到一个map容器,然后将map传给对应的方法(模拟mybatis的做法,就可以在sql语句中直接通过键访问到值)代码如下:

    class Test{ 
    @Test
        public void longinTest(){
            ParameterMapper parameterMapper = SqlSessionUtils.getSqlSession().getMapper(ParameterMapper.class);
            Map<String,Object> userInfo = new HashMap<>();
            userInfo.put("username","旺财");
            userInfo.put("password","cwlz");
            User userById = parameterMapper.login(userInfo);
            System.out.println(userById);
            if (userById!=null) {
                System.out.println("登录成功!");
            }else{
                System.out.println("用户名或密码错误");
            }
        }
    }
    

    映射文件中的部分代码 :

        <select id="login" resultType="User">
            select * from t_user where userName=#{username} and password=#{password}
        </select>
    

    通过键直接获取值,注意:使用${}时不要忘了单引号!!!!

    当参数以实体对象的形式传参时如何解决?
    只需要通过#{}以属性名的方式访问!

    class Test{ 
    @Test
        public void longinTest2(){
            ParameterMapper parameterMapper = SqlSessionUtils.getSqlSession().getMapper(ParameterMapper.class);
            User userById = parameterMapper.loginByUser(new User(null,"旺财",21,'cwlz'));
            if (userById!=null) {
                System.out.println("登录成功!");
                System.out.println(userById);
            }else{
                System.out.println("用户名或密码错误");
            }
        }
    }
    
    小提醒: 在接受以实体类对象的参数时在sql语句中的属性名一定要和实体类定义时属性的名一样!!
        比如在实体类的定义时用户名是这样定义的 private String userName ;但是在映射文件中#{username} 就会报错:
         Error querying database.  Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'username' in 'class com.kobedu.com.kobedu.mybatis.pojo.User'
    

    所以代码的编写一定要规范,才能减少这种错误!!!

    使用注解的方式传递参数:(以后开发就用它)

    在声明方法时在参数前用注解 @Param("参数名")

    public interface ParameterMapper {
        //用于测试使用注解的传参方式
        User testZhuJie(@Param("z_username") String username, @Param("z_password") String password);
    }
    

    映射文件中的部分代码:(取参数时参数名和方法声明时的注解的中的参数名一一对应)

        <select id="testZhuJie" resultType="User">
            select * from t_user where userName=#{z_username} and password=#{z_password}
        </select>
    
    小提示:如果映射文件中的参数的名如果和注解中的参数名不一一对应则会报错:
        Cause: org.apache.ibatis.binding.BindingException: Parameter 'username' not found. Available parameters are [z_username, param1, z_password, param2]
        可以从提示看出是参数找不到(参数名写错)!!!
    

    一定要和注解中的参数名一一对应!!!


    各种查询功能

    查询功能
        1. 若查询出的数据只有一条,可以通过实体类对象接收或者集合类接收
        2. 若查询出的数据有多条可以通过List集合接收,一定不能通过实体类对象接收,此时会抛异常TooManyResultsException:(就是预期的结果是一个或者null,但是发现多个就会抛出此类异常)
    

    如果查询的结果只有一个,也可以通过Map集合接收,字段名为键字段的值为值:{password=0000, id=3, userName=图区, age=20}

    MyBatis 有默认的类型别名,可以在官方文档中查看!

    java.lang.Ingeger --> int ,Integer

    int --> _int,_Integer

    Map --> map

    String --> string

    以Map集合接收多条数据

    1. 首先我们测试了 Map<String,Object> getAllUser(); 像这样接收多个数据会报错TooManyResultsException
    2. 然后可以用List接收多条数据:List<Map<String,Object>> getAllUser();结果如下:
    [{password=1234, id=1, userName=锁哥, age=21}, {password=3211, id=2, userName=仁王, age=21}, {password=0000, id=3, userName=图区, age=20}, {password=kkkk, id=4, userName=旦罗, age=22}, {password=kobedu, id=5, userName=kobe, age=21}, {password=cwlz, id=6, userName=旺财, age=20}, {password=cwlz, id=9, userName=miss, age=20}, {password=cwlz, id=10, userName=1024, age=20}]
    
    1. 以map的形式接收多条数据(重点)
    @MapKey("id") 
    Map<String,Object> getAllUser();
    

    注意:

    小提示:
      将多条数据用Map接收可以使用@MapKey 将一个字段(在表中独一无二(通常为主键))设为map的键将每一条数据作为值,用Map集合接收!  
    

    特殊sql的执行

    • 1#{} 底层为占位符原理
    • ${} 字符串拼接原理

    1,模糊查询

    • '%${username}'
    • concat('%',#{username},'%')
    • "%"#{username}"%" (这个最常用)

    2,批量删除

    #{} 是自动会添加''
    

    所以在批量删除的案例:需要注意的是不能使用#{} 因为它是会自动添加'' 所以在批量删除的语句中我们要使用${}

     <delete id="deleteMore">
        delete from t_user where id in (${ids})
    </delete>
    

    3,动态设置表名

    select * from ${tableName} 
        不能使用#{} 会有错
    

    4,添加功能获取自增的主键

    用的场景:
    
    useGeneratedKeys:设置当前的标签中的sql使用了自增主键
    keyProperty:将自增主键的值赋值给传输到映射文件中参数的某个属性
    

    自定义映射

    1,resultMap处理字段和属性的映射关系

    若字段和属性名不一致,则可以通过resultMap设置自定义映射

    • 为字段起别名,保持和属性名一致
    • 设置全局配置将 _ 自动映射为驼峰
    • 使用自定义映射 resultMap
    1,为字段起别名
    2,设置全局配置

    在mybatis的核心配置文件用下面的代码将 数据库中命名的规范 (user_name) 转换为 java中的命名规范 (userName)

        <settings>
            <setting name="mapUnderscoreToCamelCase" value="true"/>
        </settings>
    
    3,通过resultMap设置自定义映射关系

    就是手动设置属性与字段的映射关系:
    如果设置了手动的设置属性和字段的映射关系,注意主键使用 id 标签,普通字段使用 result标签,就算属性和字段名一一对应,只要
    用了这种方式就必须要写全!!!

    <!--type 映射关系中的实体类-->
    <resultMap id="user_table_map" type="User">
    <!--        主键字段-->
            <id property="id" column="id"></id>
    <!--        普通字段 
                property 映射关系中的属性名,column 字段名,当前sql查询出来的字段名
    -->
            <result property="userName" column="user_name"></result>
            <result property="age" column="age"></result>
            <result property="password" column="password"></result>
        </resultMap>
        <select id="getAllUser" resultMap="user_table_map">
            select * from t_user
        </select>
    

    映射

    表和表之间是有映射的,那么表所对应的实体类之间也是要有映射的,处理多对一的映射关系

    • 级联属性赋值
    • association
    • 分步查询

    一对多的查询:

    案例:按照部门的编号查询部门需要返回该部门的员工列表
    
    <resultMap id="getDeptAndUserByDidMap" type="Dept">
            <id property="id" column="id"></id>
            <result property="deptName" column="dept_name"></result>
            <collection property="users" ofType="User">
                <id property="id" column="id"></id>
                <result property="userName" column="user_name"></result>
                <result property="age" column="age"></result>
                <result property="password" column="password"></result>
            </collection>
        </resultMap>
    
        <select id="getDeptAndUserByDid" resultMap="getDeptAndUserByDidMap">
            select * from t_dept left join t_user on t_dept.id = t_user.did where t_dept.id = #{did};
        </select>
    

    通过分步查询实现:


    mybatis 中的动态查询

    😎
    多条件的查询

    if

    if 根据标签中test的属性所对应的表达式决定标签中的内容是否拼接到sql语句中

      <select id="getUserByCondition" resultType="User">
            select * from t_user where 'cwlz'='cwlz'
    
            <if test="userName != null and userName != '' ">
                and user_name = #{userName}
            </if>
            <if test="age != null and age != '' ">
                and age = #{age}
            </if>
    
    
        </select>
    

    上面的where后面的 1=1 是细节,因为当where后面的条件都为空时就成了 select * from t_user where
    显然这种sql语句是有问题的,还有一种情况就是当userName为null时语句就成了 select * from t_user where and age=#{age}
    这也是错的,所以在where后加一个恒成立的条件不仅不会影响查询结果,而且没有会在特定情况时sql语句是会报错的所以很有必要

    where

    where 当where标签中有内容时,会自动生成where关键字,并且将内容前多余的and 或者or去掉
    当where中没有内容时,此时where标签没有任何效果 就是不会生成关键字注意:在写条件时不能在后面加and or 这个在下一条语句无效时mybatis不会帮你去掉!

    trim

    • prifix|suffix : 将trim标签中在内容前面或后面添加指定内容
    • suffixOverrides|prefixOverrides: 将trim标签中内容前面或后面去掉指定内容
        <select id="getUserByCondition" resultType="User">
            select * from t_user
            <trim prefix="where" suffix="" prefixOverrides="" suffixOverrides="and|or" >
                <if test="userName != null and userName != '' ">
                    user_name = #{userName} and
                </if>
                <if test="age != null and age != '' ">
                    age = #{age} and
                </if>
                <if test="password != null and password !=''">
                   password = #{password}
                </if>
    
            </trim>
        </select>
    

    choose when otherwise

    相当于 if else

    • choose:用于包when otherwise
    • when: 相当于if
    • otherwise: else
        <select id="getUserByCondition" resultType="User">
            select * from t_user
           <where>
                <choose>
                    <when test="userName != null and userName != '' ">
                        user_name = #{userName}
                    </when>
                    <when test="age != null and age != '' ">
                        age = #{age}
                    </when>
                    <when test="password != null and password != '' ">
                        password = #{password}
                    </when>
    
                    <otherwise>
                        did = 1
                    </otherwise>
    
                </choose>
            </where>
        </select>
    

    foreach

    一个案例 -->就是当我们需要批量删除一些东西时(参数以数组的形式传入)

      <delete id="deletesByIds" >
           delete from t_user where id in
            (
                <foreach collection="ids" item="id" separator=",">
                    #{id}
                </foreach>
                )
       </delete>
    
    • collection 是需要遍历的集合
    • item 代表当前的
    • separator 分隔
    • open 以什么开始,在上面案例中的括号可以 open="(" 这样写
    • close 以什么结束,在上面案例中的括号可以 open=")" 这样写
      <delete id="deletesByIds" >
           delete from t_user where id in
                <foreach collection="ids" item="id" separator="or" open="(" close=")">
                    id = #{id}
                </foreach>
       </delete>
    

    sql

    sql 片段: 在我们的查询语句不能在实际开发中也一直写 *;因为我们要按需查找,不必将不需要的也查询出来,我们可以将我们平常查询次数较多的字段
    放在sql片段内,可以在需要查询时直接进行引用!

     <sql id="sqldept"> id,dept_name</sql>
       <select id="getDeptAndById" resultMap="m2">
           select <include refid="sqldept"></include> from t_dept where id = #{did}
       </select>
    

    缓存

    缓存,这个术语我们听过很多次,在web阶段时访问网页时有缓存机制!
    现在sql的查询时也有缓存机制,有一级缓存,一级缓存是默认开启的,一级缓存的范围时sqlSession,将我们查询到的数据先进行缓存,若下次有相同的查询时不用重新
    访问数据库,可以直接从缓存中取出!!!!

    使一级缓存失效的情况

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

    手动清空缓存 sqlSession.clearCache();

    一级缓存是默认使用的,而二级缓存需要手动开启!

    二级缓存

    二级缓存的相关配置

    在mapper配置文件中添加cache标签可以设置一些属性:

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

    谈缓存

    一级缓存的作用域是一个sqlsession内;
    二级缓存的作用域是针对mapper进行缓存
    

    MyBatis缓存查询的顺序

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

    整合EHCache

    <!-- Mybatis EHCache整合包 -->
    <dependency>
        <groupId>org.mybatis.caches</groupId>
        <artifactId>mybatis-ehcache</artifactId>
        <version>1.2.1</version>
    </dependency>
    <!-- slf4j日志门面的一个具体实现 -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.3</version>
    </dependency>
    

    MyBatis的逆向工程

    • 正向工程: 先创建java实体类,由框架负责根据实体类生成数据库表。 Hibernate是支持正向工程的。
    • 逆向工程: 先创建数据库表,有框架负责根据数据库表,反向生成如下资源:
      • java实体类
      • Mapper接口
      • Mapper映射文件

    MyBatis逆向工程

    逆向工程就是不难理解,我们之前都是由实体类到数据库,而逆向类就是通过数据库表生成实体类,

    • 正向工程:先创建java实体类,有框架负责根据实体类生成数据库表。
    • 逆向工程:先创建数据库表,有框架负责根据数据库表,反向生成java实体类
      • java实体类
        • Mapper接口
        • Mapper映射文件

    创建逆向工程的步骤

    添加依赖和插件

    <dependencies>
    	<!-- MyBatis核心依赖包 -->
    	<dependency>
    		<groupId>org.mybatis</groupId>
    		<artifactId>mybatis</artifactId>
    		<version>3.5.9</version>
    	</dependency>
    	<!-- junit测试 -->
    	<dependency>
    		<groupId>junit</groupId>
    		<artifactId>junit</artifactId>
    		<version>4.13.2</version>
    		<scope>test</scope>
    	</dependency>
    	<!-- MySQL驱动 -->
    	<dependency>
    		<groupId>mysql</groupId>
    		<artifactId>mysql-connector-java</artifactId>
    		<version>8.0.27</version>
    	</dependency>
    	<!-- log4j日志 -->
    	<dependency>
    		<groupId>log4j</groupId>
    		<artifactId>log4j</artifactId>
    		<version>1.2.17</version>
    	</dependency>
    </dependencies>
    <!-- 控制Maven在构建过程中相关配置 -->
    <build>
    	<!-- 构建过程中用到的插件 -->
    	<plugins>
    		<!-- 具体插件,逆向工程的操作是以构建过程中插件形式出现的 -->
    		<plugin>
    			<groupId>org.mybatis.generator</groupId>
    			<artifactId>mybatis-generator-maven-plugin</artifactId>
    			<version>1.3.0</version>
    			<!-- 插件的依赖 -->
    			<dependencies>
    				<!-- 逆向工程的核心依赖 -->
    				<dependency>
    					<groupId>org.mybatis.generator</groupId>
    					<artifactId>mybatis-generator-core</artifactId>
    					<version>1.3.2</version>
    				</dependency>
    				<!-- 数据库连接池 -->
    				<dependency>
    					<groupId>com.mchange</groupId>
    					<artifactId>c3p0</artifactId>
    					<version>0.9.2</version>
    				</dependency>
    				<!-- MySQL驱动 -->
    				<dependency>
    					<groupId>mysql</groupId>
    					<artifactId>mysql-connector-java</artifactId>
    					<version>8.0.27</version>
    				</dependency>
    			</dependencies>
    		</plugin>
    	</plugins>
    </build>
    

    创建MyBatis的核心配置文件

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <properties resource="jdbc.properties"/>
        <typeAliases>
            <package name=""/>
        </typeAliases>
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="${jdbc.driver}"/>
                    <property name="url" value="${jdbc.url}"/>
                    <property name="username" value="${jdbc.username}"/>
                    <property name="password" value="${jdbc.password}"/>
                </dataSource>
            </environment>
        </environments>
        <mappers>
            <package name=""/>
        </mappers>
    </configuration>
    

    创建逆向工程的配置文件

    • 文件名必须是:generatorConfig.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE generatorConfiguration
            PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
            "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
    <generatorConfiguration>
        <!--
        targetRuntime: 执行生成的逆向工程的版本
        MyBatis3Simple: 生成基本的CRUD(清新简洁版)
        MyBatis3: 生成带条件的CRUD(奢华尊享版)
        -->
        <context id="DB2Tables" targetRuntime="MyBatis3">
            <!-- 数据库的连接信息 -->
            <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                            connectionURL="jdbc:mysql://localhost:3306/mybatis_db"
                            userId="root"
                            password="123456">
            </jdbcConnection>
            <!-- javaBean的生成策略-->
            <javaModelGenerator targetPackage="com.atguigu.mybatis.pojo" targetProject=".\src\main\java">
                <property name="enableSubPackages" value="true" />
                <property name="trimStrings" value="true" />
            </javaModelGenerator>
            <!-- SQL映射文件的生成策略 -->
            <sqlMapGenerator targetPackage="com.atguigu.mybatis.mapper"
                             targetProject=".\src\main\resources">
                <property name="enableSubPackages" value="true" />
            </sqlMapGenerator>
            <!-- Mapper接口的生成策略 -->
            <javaClientGenerator type="XMLMAPPER"
                                 targetPackage="com.atguigu.mybatis.mapper" targetProject=".\src\main\java">
                <property name="enableSubPackages" value="true" />
            </javaClientGenerator>
            <!-- 逆向分析的表 -->
            <!-- tableName设置为*号,可以对应所有表,此时不写domainObjectName -->
            <!-- domainObjectName属性指定生成出来的实体类的类名 -->
            <table tableName="t_emp" domainObjectName="Emp"/>
            <table tableName="t_dept" domainObjectName="Dept"/>
        </context>
    </generatorConfiguration> 
    

    执行插件的generate目标

    在maven中找到插件下的generate并执行它你就会发现他自动帮你生成了java的实体类!

    QBC

    逆向工程有两个版本简单版 和 加强版
    实际开发中使用的大都是加强版,可以按条件进行增删改查!

    对逆向工厂的操作的相关回顾!

    分页插件

    引入依赖

    <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
    <dependency>
    	<groupId>com.github.pagehelper</groupId>
    	<artifactId>pagehelper</artifactId>
    	<version>5.2.0</version>
    </dependency>
    

    配置分页插件(在核心配置文件中)

    <plugins>
    	<!--设置分页插件-->
    	<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
    </plugins>
    
  • 相关阅读:
    【Objective-C】Objective-C汇总
    Java核心——面向对象编程(下)接口
    如何利用pynlpir进行中文分词并保留段落信息
    实现DM VXN 的各阶段
    lvresize与lvextend扩容逻辑卷的区别
    装在笔记本里的私有云环境:K8s 集群准备
    Ebsynth——利用图像处理和计算机视觉的视频风格转换技术
    多线程(一)线程与进程
    java springboot VUE粮食经销系统开发mysql数据库web结构java编程计算机网页源码maven项目
    单目视频估计人体姿态+形状【注意力机制--连续性+相关性】含6数据集及补充材料
  • 原文地址:https://www.cnblogs.com/kobedu/p/16252029.html