mybatis學習筆記(18)-mybatis逆向工程
标簽: mybatis
- mybatis學習筆記18-mybatis逆向工程
- 下載下傳逆向工程
- 使用方法
- 運作逆向工程
- 生成代碼配置檔案
- 執行生成程式
- 使用生成的代碼
mybaits需要程式員自己編寫sql語句,mybatis官方提供逆向工程,可以針對單表自動生成mybatis執行所需要的代碼(mapper.Java,mapper.xml、po..)
企業實際開發中,常用的逆向工程方式:由資料庫的表生成java代碼。
先附上官網連結:
- MyBatis Generator
- A code generator for MyBatis and iBATIS. - GitHub
下載下傳逆向工程
這裡其實可以添加Maven依賴的,因為跟着視訊做的,是以我就建了個普通工程,直接添加了個lib檔案夾,把要用的jar包直接copy進來了。
maven中央倉庫 MyBatis-Generator
下載下傳位址:【MyBatis Generator Core】
使用方法
運作逆向工程
根據官網說的(Running MyBatis Generator):
Running MyBatis Generator
MyBatis Generator (MBG) can be run in the following ways:
- From the command prompt with an XML configuration
- As an Ant task with an XML configuration
- As a Maven Plugin
- From another Java program with an XML configuration
- From another Java program with a Java based configuration
還可以通過eclipse的插件生成代碼
建議使用java程式方式,不依賴開發工具。
生成代碼配置檔案
<?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://120.25.162.238:3306/mybatis001?characterEncoding=utf-8"
userId="root"
password="123">
</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="com.iot.ssm.po"
targetProject=".\src">
<!-- enableSubPackages:是否讓schema作為包的字尾 -->
<property name="enableSubPackages" value="false" />
<!-- 從資料庫傳回的值被清理前後的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射檔案生成的位置 -->
<sqlMapGenerator targetPackage="com.iot.ssm.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否讓schema作為包的字尾 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.iot.ssm.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否讓schema作為包的字尾 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定資料庫表 -->
<table tableName="items"></table>
<table tableName="orders"></table>
<table tableName="orderdetail"></table>
<table tableName="user"></table>
<!-- <table schema="" tableName="sys_user"></table>
<table schema="" tableName="sys_role"></table>
<table schema="" tableName="sys_permission"></table>
<table schema="" tableName="sys_user_role"></table>
<table schema="" tableName="sys_role_permission"></table> -->
<!-- 有些表的字段需要指定java類型
<table schema="" tableName="">
<columnOverride column="" javaType="" />
</table> -->
</context>
</generatorConfiguration>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
需要注意的位置:
-
,生成PO類的位置javaModelGenerator
-
,mapper映射檔案生成的位置sqlMapGenerator
-
,mapper接口生成的位置javaClientGenerator
-
,指定資料庫表table
執行生成程式
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);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
日志輸出:
-- ::, [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Retrieving column information for table "items"
-- ::, [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "id", data type , in table "mybatis001..items"
-- ::, [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "name", data type , in table "mybatis001..items"
-- ::, [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "price", data type , in table "mybatis001..items"
-- ::, [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "detail", data type -, in table "mybatis001..items"
-- ::, [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "pic", data type , in table "mybatis001..items"
-- ::, [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "createtime", data type , in table "mybatis001..items"
-- ::, [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Retrieving column information for table "orders"
-- ::, [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "id", data type , in table "mybatis001..orders"
-- ::, [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "user_id", data type , in table "mybatis001..orders"
-- ::, [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "number", data type , in table "mybatis001..orders"
-- ::, [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "createtime", data type , in table "mybatis001..orders"
-- ::, [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "note", data type , in table "mybatis001..orders"
-- ::, [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Retrieving column information for table "orderdetail"
-- ::, [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "id", data type , in table "mybatis001..orderdetail"
-- ::, [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "orders_id", data type , in table "mybatis001..orderdetail"
-- ::, [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "items_id", data type , in table "mybatis001..orderdetail"
-- ::, [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "items_num", data type , in table "mybatis001..orderdetail"
-- ::, [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Retrieving column information for table "user"
-- ::, [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "id", data type , in table "mybatis001..user"
-- ::, [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "username", data type , in table "mybatis001..user"
-- ::, [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "birthday", data type , in table "mybatis001..user"
-- ::, [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "sex", data type , in table "mybatis001..user"
-- ::, [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "address", data type , in table "mybatis001..user"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
生成後的代碼:

使用生成的代碼
需要将生成工程中所生成的代碼拷貝到自己的工程中。
測試ItemsMapper中的方法
package com.iot.ssm.mapper;
import static org.junit.Assert.*;
import java.util.Date;
import java.util.List;
import com.iot.ssm.po.Items;
import com.iot.ssm.po.ItemsExample;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ItemsMapperTest {
private ApplicationContext applicationContext;
private ItemsMapper itemsMapper;
//在setUp這個方法得到spring容器
@Before
public void setUp() throws Exception {
applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
itemsMapper = (ItemsMapper) applicationContext.getBean("itemsMapper");
}
//根據主鍵删除
@Test
public void testDeleteByPrimaryKey() {
}
//插入
@Test
public void testInsert() {
//構造 items對象
Items items = new Items();
items.setName("手機");
items.setPrice(f);
items.setCreatetime(new Date());
itemsMapper.insert(items);
}
//自定義條件查詢
@Test
public void testSelectByExample() {
ItemsExample itemsExample = new ItemsExample();
//通過criteria構造查詢條件
ItemsExample.Criteria criteria = itemsExample.createCriteria();
criteria.andNameEqualTo("筆記本");
//可能傳回多條記錄
List<Items> list = itemsMapper.selectByExample(itemsExample);
System.out.println(list);
}
//根據主鍵查詢
@Test
public void testSelectByPrimaryKey() {
Items items = itemsMapper.selectByPrimaryKey();
System.out.println(items);
}
//更新資料
@Test
public void testUpdateByPrimaryKey() {
//對所有字段進行更新,需要先查詢出來再更新
Items items = itemsMapper.selectByPrimaryKey();
items.setName("手機");
itemsMapper.updateByPrimaryKey(items);
//如果傳入字段不空為才更新,在批量更新中使用此方法,不需要先查詢再更新
//itemsMapper.updateByPrimaryKeySelective(record);
}
}