Mybatis逆向工程生成的Example类的使用

本章节主要讲解如何使用Mybatis逆向工程生成的Example类如何使用

Mybatis的逆向工程可以参考官方文档的QuickStar:http://www.mybatis.org/generator/quickstart.html

Mybatis的逆向工程生成的Example类主要进行实体类的复杂查询

如何使用

随便点开一个Example实体类:

//升序还是降序:字段+空格+asc(desc)
protected String orderByClause;

//去除重复:true去重,false则反之
protected boolean distinct;

//自定义查询条件
protected List<Criteria> oredCriteria;

一、mapper接口中的方法解析

mapper接口中的函数及方法

方法功能说明
int countByExample(UserExample example) thorws SQLException按条件计数
int deleteByPrimaryKey(Integer id) thorws SQLException按主键删除
int deleteByExample(UserExample example) thorws SQLException按条件查询
String/Integer insert(User record) thorws SQLException插入数据(返回值为ID)
User selectByPrimaryKey(Integer id) thorws SQLException按主键查询
ListselectByExample(UserExample example) thorws SQLException按条件查询
ListselectByExampleWithBLOGs(UserExample example) thorws SQLException按条件查询(包括BLOB字段)。只有当数据表中的字段类型有为二进制的才会产生。
int updateByPrimaryKey(User record) thorws SQLException按主键更新
int updateByPrimaryKeySelective(User record) thorws SQLException按主键更新值不为null的字段
int updateByExample(User record, UserExample example) thorws SQLException按条件更新
int updateByExampleSelective(User record, UserExample example) thorws SQLException按条件更新值不为null的字段

二、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之间条件

三、应用举例

1.查询

① selectByPrimaryKey()

User user = XxxMapper.selectByPrimaryKey(100);
//相当于select * from user where id = 100 

② selectByExample() 和 selectByExampleWithBLOGs()

UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("wyw");
criteria.andUsernameIsNull();
example.setOrderByClause("username asc,email desc");
List<?>list = XxxMapper.selectByExample(example);
//相当于:select * from user where username = 'wyw' and  username is null order by username asc,email desc

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

 

2.插入数据

①insert()

User user = new User();
user.setId("dsfgsdfgdsfgds");
user.setUsername("admin");
user.setPassword("admin")
user.setEmail("wyw@163.com");
XxxMapper.insert(user);
//相当于:insert into user(ID,username,password,email) values ('dsfgsdfgdsfgds','admin','admin','wyw@126.com');

 

3.更新数据

①updateByPrimaryKey()

User user =new User();
user.setId("dsfgsdfgdsfgds");
user.setUsername("wyw");
user.setPassword("wyw");
user.setEmail("wyw@163.com");
XxxMapper.updateByPrimaryKey(user);
//相当于:update user set username='wyw', password='wyw', email='wyw@163.com' where id='dsfgsdfgdsfgds'

 

②updateByPrimaryKeySelective()

User user = new User();
user.setId("dsfgsdfgdsfgds");
user.setPassword("wyw");
XxxMapper.updateByPrimaryKey(user);
//相当于:update user set password='wyw' where id='dsfgsdfgdsfgds'

 

③ updateByExample() 和 updateByExampleSelective()

UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("admin");
User user = new User();
user.setPassword("wyw");
XxxMapper.updateByPrimaryKeySelective(user,example);
//相当于:update user set password='wyw' where username='admin'

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

 

4.删除数据

①deleteByPrimaryKey()

XxxMapper.deleteByPrimaryKey(1); 
//相当于:delete from user where id=1 

②deleteByExample()

UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("admin");
XxxMapper.deleteByExample(example);
//相当于:delete from user where username='admin'

5.查询数据数量

①countByExample()

UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("wyw");
int count = XxxMapper.countByExample(example);
//相当于:select count(*) from user where username='wyw'

 

 

 

MyBatis逆向工程是一个非常实用的工具,可以根据数据库表结构自动生成Java Bean对象和Mapper映射接口代码,提高开发效率。而生成的代码默认使用XML文件配置SQL语句,但是随着Java EE技术的不断发展,注解已经逐渐成为了一个非常流行和便捷的编程方式。 因此,很多开发者开始使用MyBatis逆向工程生成注解的代码。下面是一个基本的工程配置步骤: 1. 添加相关依赖 在Maven工程中,需要添加MyBatisMyBatis 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、付费专栏及课程。

余额充值