Mybatis-Generator学习总结

Mybatis-Generator学习总结

一、导入插件依赖

	<!--mybatis 数据管理层自动生成插件  -->
<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.4</version>
    <configuration>
        <!--配置文件路径-->			
        <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
        <verbose>true</verbose>
        <overwrite>true</overwrite>
    </configuration>
    <!-- 插件运行所需要的的依赖-->
    <dependencies>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.4</version>
        </dependency>
        <!-- 运行插件时需要配置驱动和数据源,具体配置在generatorConfig.xml中配置 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
    </dependencies>
</plugin>

二、创建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>
    <!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
    <!--<classPathEntry  location="E:\developer\mybatis-generator-core-1.3.2\lib\mysql-connector-java-5.1.25-bin.jar"/>-->
    <context id="DB2Tables"  targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://xxx:3306/xxx"
                        userId="username" password="password">
        </jdbcConnection>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 生成模型的包名和位置-->
        <javaModelGenerator targetPackage="com.xxx.pojo" targetProject="src/main/java/">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成映射文件的包名和位置-->
        <sqlMapGenerator targetPackage="com.xxx.mapper" targetProject="src/main/resources/">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.xxx.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 你需要生成的表  表名  生成实体类名-->
        <!--<table tableName="t_test" domainObjectName="TestInfo"
                enableCountByExample="true" 
				enableUpdateByExample="true"
               	enableDeleteByExample="true" 
				enableSelectByExample="true"
              	selectByExampleQueryId="true"  >
        </table>-->
        
    </context>
</generatorConfiguration>

三、运行插件生成文件

在这里插入图片描述

生成的文件包含以下内容

1、pojo实体类,根据数据库中的字段生成对应的属性

2、XXXExample文件,和pojo在同一个目录下,主要用于查询条件构造

3、XxxMapper接口文件

4、XxxMapper接口文件对应的XxxMapper.xml文件

四、Example的使用

下面是插件帮我们生成的一个XXXMapper接口

public interface ModuleDetailInfoMapper {
    //依据某个条件算出数量,条件由XxxExample中设置
    long countByExample(ModuleDetailInfoExample example);
	//依据某个条件删除
    int deleteByExample(ModuleDetailInfoExample example);
	//根据主键删除
    int deleteByPrimaryKey(Integer id);
	//插入一条数据
    int insert(ModuleDetailInfo record);
	//插入一条数据,非空数据
    int insertSelective(ModuleDetailInfo record);
	//根据条件查询数据,只有一条时用get(0)获取
    List<ModuleDetailInfo> selectByExample(ModuleDetailInfoExample example);
	//根据主键查询
    ModuleDetailInfo selectByPrimaryKey(Integer id);
	//根据条件更新(非空)
    int updateByExampleSelective(@Param("record") ModuleDetailInfo record, @Param("example") ModuleDetailInfoExample example);
	//根据条件更新
    int updateByExample(@Param("record") ModuleDetailInfo record, @Param("example") ModuleDetailInfoExample example);
	//根据主键查询
    int updateByPrimaryKeySelective(ModuleDetailInfo record);
	//根据主键更新
    int updateByPrimaryKey(ModuleDetailInfo record);
}
example.setOrderByClause(“字段名 ASC”);添加升序排列条件,DESC为降序
example.setDistinct(false)去除重复,boolean型,true为选择不重复的记录
criteria.andXxxIsNull添加字段xxx为null的条件
criteria.andXxxIsNotNull添加字段xxx不为null的条件
criteria.andXxxEqualTo(value)添加xxx字段等于value条件
criteria.andXxxNotEqualTo(value)添加xxx字段不等于value条件
criteria.andXxxGreaterThan(value)添加xxx字段大于value条件
criteria.andXxxGreaterThanOrEqualTo(value)添加xxx字段大于等于value条件
criteria.andXxxLessThan(value)添加xxx字段小于value条件
criteria.andXxxLessThanOrEqualTo(value)添加xxx字段小于等于value条件
criteria.andXxxIn(List<?>)添加xxx字段值在List<?>条件
criteria.andXxxNotIn(List<?>)添加xxx字段值不在List<?>条件
criteria.andXxxLike(“%”+value+”%”)添加xxx字段值为value的模糊查询条件
criteria.andXxxNotLike(“%”+value+”%”)添加xxx字段值不为value的模糊查询条件
criteria.andXxxBetween(value1,value2)添加xxx字段值在value1和value2之间条件
criteria.andXxxNotBetween(value1,value2)添加xxx字段值不在value1和value2之间条件

第一次写博客,不知道写什么,正好最近新入职一家公司用的这个mybatis-generator插件,学习记录一下
上面的表中总结的方法是觉得这个博主写挺不错(写博客好累,不想写了)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值