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);