MyBatis插件:逆向工程MyBatis Generator的配置和应用

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

简单来说就是帮助我们生成实体类,mapper(映射器).xml接口

使用

1.引入依赖

<!-- mybatis-generator-core 反向生成java代码-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>

2.创建生成mapper的配置文件generatorConfig.xml

主要修改一下标签,需要生成mapper对应的表

<?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">
<commentGenerator>
<!-- 是否去除自动生成的注释 -->
<property name="suppressAllComments" value="false"/>
</commentGenerator>
<!-- Mysql数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/szqy08"
userId="root"
password="123456">
</jdbcConnection>
<!-- Oracle数据库
<jdbcConnection driverClass="oracle.jdbc.OracleDriver"
connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg"
userId="yycg"
password="yycg">
</jdbcConnection>
-->
<!-- 默认为false,把JDBC DECIMAL 和NUMERIC类型解析为Integer,为true时
把JDBC DECIMAL 和NUMERIC类型解析为java.math.BigDecimal -->
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成POJO类的位置 -->
<javaModelGenerator targetPackage="com.wgz.entity" targetProject="src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.wgz.dao" targetProject="src/main/resources">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetProject:mapper接口生成的的位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.wgz.dao"
targetProject="src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据表 -->
<table schema="" tableName="user"></table>
<!-- 有些表的字段需要指定java类型
<table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" >
<property name="useActualColumnNames" value="true"/>
<generatedKey column="ID" sqlStatement="DB2" identity="true" />
<columnOverride column="DATE_FIELD" property="startDate" />
<ignoreColumn column="FRED" />
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
</table> -->
</context>
</generatorConfiguration>

3.生成mapper

public class MybatisGeneratorUtil {
public static void main(String[] args) throws IOException, XMLParserException,
InvalidConfigurationException, SQLException, InterruptedException {
//如果这里出现空指针,直接写绝对路径即可。
String genCfg = "C:\\Users\\Admin\\Desktop\\假期
\\mybatis\\mybatis04\\mybatis04\\src\\main\\resources\\generatorConfig.xml";
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
// 指定配置文件
File configFile = new File(genCfg);
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback,
warnings);
myBatisGenerator.generate(null);
}
}

4.使用

  1. 简单使用
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.selectByPrimaryKey(1);
System.out.println("user:"+user);
  1. 高级用法

可以使用 UserExample完成一些定制化的查询

UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
UserExample userExample = new UserExample();
// 设置去重
userExample.setDistinct(true);
// 设置按照名称排序
userExample.setOrderByClause("username");
List<User> users = userMapper.selectByExample(userExample);
for (User user:users){
System.out.println("user:"+user.getUsername());
}

  1. 通过UserExample.Criteria 生成&的条件查询

相当于 select * from demo WHERE a = ? and b = ?

UserExample userExample = new UserExample();
UserExample.Criteria criteria = userExample.createCriteria();
// 两者为&的条件
criteria.andUsernameLike("%user%").andSexEqualTo("男");
List<User> users = userMapper.selectByExample(userExample);
for (User user:users){
System.out.println("user:"+user.getUsername());
}

  1. 可以使用多个UserExample.Criteria 完成|的查询

相当于 select count(*) from demo WHERE ( a = ? and b = ? ) or ( a = ? and c = ? )

UserExample userExample = new UserExample();
UserExample.Criteria criteria = userExample.createCriteria();
// 两者为&的条件
criteria.andUsernameLike("%user%").andSexEqualTo("男");
// 或者关系 or
UserExample.Criteria criteria2 = userExample.createCriteria();
criteria2.andSexLike("%F%");
// 将多个Criteria 组合在一起
userExample.or(criteria2);
List<User> users = userMapper.selectByExample(userExample);


for (User user:users){
System.out.println("user:"+user.getUsername());
}

  1. updateByPrimaryKeySelective和updateByPrimaryKey的用法区别?

updateByPrimaryKeySelective(Object obj)
updateByPrimaryKeySelective 接收的参数为对应于数据库的实体类对象,利用字段的自动匹配进行更新表的
操作,如果传入obj对象中的某个属性值为null,则不进行数据库对应字段的更新。
updateByPrimaryKey(Object obj)
与updateByPrimaryKeySelective的区别在于,如果传入的obj对象中某个属性值为null,会将对应的数据库字
段赋值为null。

测试

User user = new User();
user.setId(1);
user.setUsername("xiaomingaaa");
// 此时示例中的null 空值不会 数据库中对应以有的字段覆盖
userMapper.updateByPrimaryKeySelective(user);
// 此时示例中的null 空值会把 数据库中对应以有的字段覆盖
//userMapper.updateByPrimaryKey(user);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值