mybatis逆向工程

mybatis逆向工程介绍

优点:mybatis需要程序员自己编写sql语句,并且mybatis官方提供逆向工程,可以针对单表自动生成mybatis执行所需要的代码(mapper.java、mapper.xml、pojo、sql…),可以让程序员将更多的精力放在繁杂的业务逻辑上。常用的逆向工程方式:由数据库的表生成java代码。
缺点:Mybatis逆向工程生成的Mapper所进行的操作都是针对单表的。但是在大型项目中,很少有复杂的多表关联查询,所以作用还是很大的。

mybatis逆向工程实现

  1. 实现配置文件
  2. 运行配置文件

以下是需要配置的mybatis-generator.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>
    <context id="DB2Tables" targetRuntime="MyBatis3">

        <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin>
		<!-- 数据库连接 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/${具体的数据库名}?characterEncoding=utf8"
                        userId="root"
                        password="123">
        </jdbcConnection>

		<!-- 设置一些配置信息 -->
		<!-- 是否去除自动生成的注释,不写默认自动生成注解 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        
		<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer;
		为true时把JDBC DECIMAL和NUMERIC类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

		<!-- 生成model包和位置 -->
        <javaModelGenerator targetPackage="life.majiang.community.community.model" targetProject="src/main/java">
        	<!-- 是否按schema命名包名 -->
            <property name="enableSubPackages" value="true" />
            <!-- 清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        
		<!-- 生成Mapper映射XML文件位置,在resources中 -->
        <sqlMapGenerator targetPackage="mapper"  targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

		<!-- 生成Mapper接口文件位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="life.majiang.community.community.mapper"  targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

		<!-- 具体要生成的表名  和  生成后的实例名 -->
        <table tableName="user" domainObjectName="User" ></table>
        <table tableName="question" domainObjectName="Question" ></table>
        <table tableName="comment" domainObjectName="Comment" ></table>
        <table tableName="..." domainObjectName="..." ></table>
    </context>
</generatorConfiguration>

执行maven语句,自动执行配置文件

mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate

该配置文件是以重写的方式执行,所以可以多次生成不用担心重复添加。
经上述mybatis-generator.xml生成

  • model包含有xxx和xxxExample
  • mapper接口包含有xxxMapper和xxxExtMapper
  • mapper配置文件包含有xxxMapper.xml和xxxExtMapper.xml

逆向工程的使用(以查询为例)

  • 返回特定列的查询(selectByExample)
    通过特定限制条件查询信息,example用于生成一个Criteria对象来设置查询条件
	UserExample userExample = new UserExample();
    userExample.createCriteria().andAccountIdEqualTo(user.getAccountId());
    List<User> users = userMapper.selectByExample(userExample);

criteria有许多限制条件,都是以and开头的方法

  • 又比如将Criteria对象拿出来,对其进行复杂的约束设置
Criteria criteria = userExample.createCriteria()
List<Long> ids = new ArrayList<>();
ids.add((long)20);
ids.add((long)40);
ids.add((long)60);
criteria.andItemIdIn(ids);	// 设置条件:user的属性itemId等于20或40或60
criteria.andCreatedIsNotNull(); // 设置条件:user的属性created不为空
  • 返回值是所有列的查询(selectByExampleWithBLOBs)

  • 通过主键查询(selectByPrimaryKey)

Question question = questionMapper.selectByPrimaryKey(comment.getParentId());
  • 也可以自定义查询条件
    将自定义的sql写在mapper配置文件里,再注册到对应的Extmapper接口
questionExtMapper.incCommentCount(question);//让问题数自增1

另:mybatisgenerator自带的排序操作可以简化代码
为example额外添加排序的约束:

commentExample.setOrderByClause("gmt_create desc");//根据gmt_create进行降序排列

还有插入、删除、更新、计数等操作不再演示

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值