Mybatis逆向工程:搭建一个基于
mybatis-generator-core
的工程,从功能上说叫逆向工程
mybatis-generator-core:Mybatis官方提供的一个代码生成器,可以根据数据库的单体表结构,自动生成对应的代码
相关环境
- IDE:Eclipse
- 构建工具:Maven
- JDK:1.8
- 数据库:Mysql,库名:testdb,用户:root,密码:123456,表名:tb_brand
工程构建和代码生成
- 创建一个maven工程,添加依赖
Artifact Id:generatorSql
Packaging:jar
pom.xml:
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.28</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.3</version>
</dependency>
- 创建配置文件和Java类
config.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="testTables" targetRuntime="MyBatis3" >
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/testdb" userId="root"
password="123456">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="test.pojo"
targetProject=".\src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="mapper"
targetProject=".\src\main\resources">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="test.mapper"
targetProject=".\src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<table schema="" tableName="tb_brand"></table>
</context>
</generatorConfiguration>
GeneratorSql.java
package generatorSql;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
public class GeneratorSql {
public void generator() {
try {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("config.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,callback, warnings);
System.out.println("代码开始生成");
myBatisGenerator.generate(null);
System.out.println("代码开始结束");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
try {
GeneratorSql generatorSql = new GeneratorSql();
generatorSql.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行:GeneratorSql.java 即可生成代码
查看及使用生成的代码
-
生成的代码
-
通过TbBrandMapper.java文件,查看具备的功能
public interface TbBrandMapper { //增 //普通的增加一条记录 int insert(TbBrand record); //增加的时候会验证属性值是否为null,如果是null,增加的记录里就不包含该属性 int insertSelective(TbBrand record); //删 //根据主键的值来匹配删除 int deleteByPrimaryKey(Long id); //根据特定的条件来匹配删除 int deleteByExample(TbBrandExample example); //改 //根据主键的值来匹配修改 int updateByPrimaryKey(TbBrand record); //根据主键的值来匹配修改+属性是否为null验证 int updateByPrimaryKeySelective(TbBrand record); //根据特定的条件来匹配修改 int updateByExample(@Param("record") TbBrand record, @Param("example") TbBrandExample example); //根据特定的条件来匹配修改+属性是否为null验证 int updateByExampleSelective(@Param("record") TbBrand record, @Param("example") TbBrandExample example); //查 //根据主键的值来匹配查询 TbBrand selectByPrimaryKey(Long id); //根据特定的条件来匹配查询 List<TbBrand> selectByExample(TbBrandExample example); //根据特定的条件来匹配查询满足条件的记录数 long countByExample(TbBrandExample example); }
-
关于TbBrandExample.java实体类
在原有实体类TbBrand.java的基础上,封装了的对属性值的各种查询条件的方法。
比如属性值是否为空,属性值的取值范围,属性值的模糊匹配等
TbBrandExample的使用://创建类实例example TbBrandExample example=new TbBrandExample(); //通过该实例获取Criteria对象 Criteria criteria = example.createCriteria(); //通过Criteria对象,设置各种查询条件 criteria.andNameLike("%"+brand.getName()+"%"); criteria.andFirstCharEqualTo(brand.getFirstChar()); //最后调用相应接口方法,传入实例example brandMapper.selectByExample(example);