Mabatis逆向工程生成pojo和mapper

Mabatis逆向工程生成pojo和mapper

1. 导入逆向工程

逆向工程简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件,接口,以及bean类。支持基本的增删改查,以及QBC风格的条件查询。但是表连接、存储过程等这些复杂sqI的定义需要我们手工编写

在这里插入图片描述

2. 修改配置文件

在generatorConfig.xml中配置Mapper生成的详细信息,如下图:
在这里插入图片描述
注意修改内容主要以下几点:

  1. 修改数据库连接的信息

    	<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
    	<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
    		connectionURL="jdbc:mysql://localhost:3306/mimissm?useSSL=false&amp;serverTimezone=UTC&amp;allowPublicKeyRetrieval=true" 
    		userId="root"
    		password="root">
    	</jdbcConnection>
    
  2. 指定数据库表 (生成那些数据库表对应的文件) – admin,product_info,product_type

     	<!-- 指定数据库表 -->
     	<table schema="" tableName="admin"></table>
     	<table schema="" tableName="product_info"></table>
     	<table schema="" tableName="product_type"></table>
    
  3. 生成PO类的位置 – com.yanyu.pojo

    <!-- targetProject:生成PO类的位置 -->
    	<javaModelGenerator targetPackage="com.yanyu.pojo"
    		targetProject=".\src">
    		<!-- enableSubPackages:是否让schema作为包的后缀 -->
    		<property name="enableSubPackages" value="false" />
    		<!-- 从数据库返回的值被清理前后的空格 -->
    		<property name="trimStrings" value="true" />
    	</javaModelGenerator>
    
  4. mapper映射文件生成的位置 – com.yanyu.mapper

     <!-- targetProject:mapper映射文件生成的位置 -->
    	<sqlMapGenerator targetPackage="com.yanyu.mapper"
    		targetProject=".\src">
    		<!-- enableSubPackages:是否让schema作为包的后缀 -->
    		<property name="enableSubPackages" value="false" />
    	</sqlMapGenerator>
    
  5. mapper接口生成的位置

    <!-- targetPackage:mapper接口生成的位置 -->
     	<javaClientGenerator type="XMLMAPPER"
     		targetPackage="com.yanyu.mapper"
     		targetProject=".\src">
     		<!-- enableSubPackages:是否让schema作为包的后缀 -->
     		<property name="enableSubPackages" value="false" />
     	</javaClientGenerator>
    

3. 运行GeneratorSqlmap,生成pojo和mapper文件

在这里插入图片描述

运行前删除src下同名目录,防止文件重叠

运行程序后,在设置的目录下生成对应文件
在这里插入图片描述

4. MyBatis逆向工程中的Mapper接口以及Example的实例函数及详解

转载自:https://www.cnblogs.com/cl-rr/p/9668645.html

4.1 mapper接口中的方法解析

mapper接口中的函数及方法

方法功能说明
int countByExample(UserExample example)按条件计数
int deleteByPrimaryKey(Integer id)按主键删除
int deleteByExample(UserExample example)按条件查询
String/Integer insert(User record)插入数据(返回值为ID)
int insertSelective(Admin record)条件插入
User selectByPrimaryKey(Integer id)按主键查询
List selectByExample(UserExample example)按条件查询
int updateByPrimaryKey(User record)按主键更新
int updateByPrimaryKeySelective(User record)按主键更新值不为null的字段(更新值为null的字段该字段还是以前的值)
int updateByExample(User record, UserExample example)按条件更新
int updateByExampleSelective(User record, UserExample example)按条件更新值不为null的字段

4.2 example实例解析

mybatis的逆向工程中会生成实例及实例对应的example,example用于添加条件,相当where后面的部分
xxxExample example = new xxxExample();
Criteria criteria = new Example().createCriteria();

方法说明
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之间条件

4.3 应用举例

注:在iBator逆向工程生成的文件XxxExample.java中包含一个static的内部类Criteria,Criteria中的方法是定义SQL 语句where后的查询条件。

1.查询

① selectByPrimaryKey()
//相当于select * from user where id = 100

 User user = XxxMapper.selectByPrimaryKey(100); 

**② selectByExample() **
//相当于:select * from user where username = ‘wyw’ and username is null order by username asc,email desc

 UserExample example = new UserExample();
 Criteria criteria = example.createCriteria();
 criteria.andUsernameEqualTo("wyw");
 criteria.andUsernameIsNull();
 example.setOrderByClause("username asc,email desc");
 List<?>list = XxxMapper.selectByExample(example);
2. 插入数据

①insert()
//相当于:insert into user(ID,username,password,email) values (‘dsfgsdfgdsfgds’,‘admin’,‘admin’,‘wyw@126.com’);

 User user = new User();
 user.setId("dsfgsdfgdsfgds");
 user.setUsername("admin");
 user.setPassword("admin")
 user.setEmail("wyw@163.com");
 XxxMapper.insert(user);
3. 更新数据

①updateByPrimaryKey()
//相当于:update user set username=‘wyw’, password=‘wyw’, email=‘wyw@163.com’ where id=‘dsfgsdfgdsfgds’

User user =new User();
user.setId("dsfgsdfgdsfgds");
user.setUsername("wyw");
user.setPassword("wyw");
user.setEmail("wyw@163.com");
XxxMapper.updateByPrimaryKey(user);

②updateByPrimaryKeySelective()
//相当于:update user set password=‘wyw’ where id=‘dsfgsdfgdsfgds’

 User user = new User();
 user.setId("dsfgsdfgdsfgds");
 user.setPassword("wyw");
 XxxMapper.updateByPrimaryKey(user);

③ updateByExample() 和 updateByExampleSelective()

updateByExample()更新所有的字段,包括字段为null的也更新,建议使用 updateByExampleSelective()更新想更新的字段

//相当于:update user set password=‘wyw’ where username=‘admin’

UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("admin");
User user = new User();
user.setPassword("wyw");
XxxMapper.updateByPrimaryKeySelective(user,example);

4.删除数据

①deleteByPrimaryKey()
//相当于:delete from user where id=1

 1 XxxMapper.deleteByPrimaryKey(1); 

②deleteByExample()
//相当于:delete from user where username=‘admin’

 UserExample example = new UserExample();
 Criteria criteria = example.createCriteria();
 criteria.andUsernameEqualTo("admin");
 XxxMapper.deleteByExample(example);

5.查询数据数量

①countByExample()
//相当于:select count(*) from user where username=‘wyw’

 UserExample example = new UserExample();
 Criteria criteria = example.createCriteria();
 criteria.andUsernameEqualTo("wyw");
 int count = XxxMapper.countByExample(example);

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis逆向工程是一个非常实用的工具,可以根据数据库表结构自动生成Java Bean对象和Mapper映射接口代码,提高开发效率。而生成的代码默认使用XML文件配置SQL语句,但是随着Java EE技术的不断发展,注解已经逐渐成为了一个非常流行和便捷的编程方式。 因此,很多开发者开始使用MyBatis逆向工程生成注解的代码。下面是一个基本的工程配置步骤: 1. 添加相关依赖 在Maven工程中,需要添加MyBatis、MyBatis Generator和数据库驱动的依赖。例如: ``` <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.4.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> </dependencies> ``` 2. 编写generatorConfig.xml 该配置文件位于src/main/resources/目录下,可以使用MyBatis Generator提供的GUI工具进行可视化编辑,也可以手动编写该文件。编写该文件时需要注意以下几点: - database、jdbcConnection、table等元素的配置与普通的XML配置类似。 - 需要添加context元素,并在其中配置JavaModelGenerator、SqlMapGenerator和JavaClientGenerator等元素,分别用来生成Java Bean、Mapper映射接口和Mapper XML文件。 - 在context元素中添加targetRuntime="MyBatis3Simple"属性,表示使用简单的MyBatis3运行时,可以生成注解的代码。 例如: ```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="MyBatis3Simple"> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" userId="root" password="root"> </jdbcConnection> <javaModelGenerator targetPackage="com.example.demo.pojo" targetProject="src/main/java"> </javaModelGenerator> <sqlMapGenerator targetPackage="com.example.demo.mapper" targetProject="src/main/java"> </sqlMapGenerator> <javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="com.example.demo.mapper" targetProject="src/main/java"> </javaClientGenerator> <table tableName="student"></table> </context> </generatorConfiguration> ``` 3. 运行Generator 使用Maven插件或者命令行运行MyBatis Generator,生成自动化代码到指定目录下。例如: ```shell mvn mybatis-generator:generate ``` 4. 代码使用 生成的 Java Bean、Mapper 接口和注解 SQL 语句已经全部生成了,可以直接使用。例如: ```java public interface StudentMapper { @Select({ "select", "id, name, age, sex", "from student", "where id = #{id,jdbcType=INTEGER}" }) @Results({ @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true), @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR), @Result(column="age", property="age", jdbcType=JdbcType.INTEGER), @Result(column="sex", property="sex", jdbcType=JdbcType.VARCHAR) }) Student selectByPrimaryKey(Integer id); } ``` 以上是MyBatis逆向工程生成注解的代码的工程配置过程,也是一种传统的方式。近年来,大部分使用MyBatis的开发者会选择使用习惯上更加统一的Lombok注解类库,必要的情况下也会对产生的代码进行瘦身优化,使代码更加简洁。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值