11、MyBatis的逆向工程
阿新 • • 發佈:2019-01-25
使用官方網站的Mapper自動生成工具mybatis-generator-core-1.3.2來生成類和mapper對映檔案。
連結:https://pan.baidu.com/s/1nuYaYST 密碼:ur5m
將generatorSqlmapCustom匯入工作空間。
在這裡我們使用java工程生成逆向工程:
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 GeneratorSqlmap {
public void generator() throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//指定 逆向工程配置檔案
File configFile = new File("generatorConfig.xml");
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);
}
public static void main(String[] args) throws Exception {
try {
GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}
上面這段java程式碼我們不需要去管,重點看generatorConfig.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/mybatis" userId="root"
password="mysql">
</jdbcConnection>
<!-- <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:生成PO類的位置 -->
<javaModelGenerator targetPackage="cn.itcast.ssm.po"
targetProject=".\src">
<!-- enableSubPackages:是否讓schema作為包的字尾 -->
<property name="enableSubPackages" value="false" />
<!-- 從資料庫返回的值被清理前後的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper對映檔案生成的位置 -->
<sqlMapGenerator targetPackage="cn.itcast.ssm.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否讓schema作為包的字尾 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper介面生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="cn.itcast.ssm.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否讓schema作為包的字尾 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定資料庫表 -->
<table schema="" tableName="user"></table>
<table schema="" tableName="orders"></table>
<table schema="" tableName="items"></table>
<table schema="" tableName="orderdetail"></table>
<!-- 有些表的欄位需要指定java型別
<table schema="" tableName="">
<columnOverride column="" javaType="" />
</table> -->
</context>
</generatorConfiguration>
將上面的配置檔案修改適合自己的引數,執行程式:
測試逆向工程
工程中的環境,諸如sqlmapconfig.xml、applicationContext.xml可以參考前面一篇:
10、MyBatis與Spring的整合
使用掃描包的方式配置mapper介面:
<!-- 掃描包的形式配置mapper-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 這種方法不需要寫id -->
<!-- 配置基礎包,每個mapper代理物件的id就是類名,首字母小寫 -->
<property name="basePackage" value="cn.xpu.hcp.mapper"/>
</bean>
測試插入:
@Autowired
@Qualifier("userMapper")
private UserMapper userMapper;
@Test
public void testInsert() {
User user = new User();
user.setUsername("曹操");
user.setSex("1");
user.setBirthday(new Date());
user.setAddress("成都市");
userMapper.insert(user);
}
根據id查詢:
@Test
public void testSelectById(){
User user = userMapper.selectByPrimaryKey(1);
System.out.println(user.getUsername()+"\t"+user.getSex()+"\t"+user.getBirthday());
}
測試模糊查詢:
@Test
public void testSelectByLike(){
//建立user物件的擴充套件類,設定查詢條件
UserExample userExample = new UserExample();
//我們可以進行鏈式程式設計,新增任何我們希望的條件
userExample.createCriteria().andUsernameLike("%王%");
//查詢資料
List<User> list = userMapper.selectByExample(userExample);
for (User user : list) {
System.out.println(user);
}
}
注意:
- 如果我們修改了資料庫表,那麼我們就需要重新生成逆向工程;
- 逆向工程生成的程式碼只能做單表查詢;
- 我們不能在生成的程式碼上進行擴充套件;
- 一張表生成四個檔案。