F:\1ziliao\mybatis\資料\逆向工程官方下載java
在使用mybatis時須要程序員本身編寫sql語句,針對單表的sql語句量是很大的,mybatis官方提供了一種根據數據庫表生成mybatis執行代碼的工具,這個工具就是一個逆向工程。 mysql
逆向工程:數據庫單表----->>>生成代碼(mapper.java,mapper.xml,pojo)程序員
mybatis-generator-core-1.3.2.jarspring
<?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.mybatis.po" targetProject=".\src">
<!-- enableSubPackages:是否讓schema做爲包的後綴 -->
<property name="enableSubPackages" value="false" />
<!-- 從數據庫返回的值被清理先後的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="cn.itcast.mybatis.mapper" targetProject=".\src">
<!-- enableSubPackages:是否讓schema做爲包的後綴 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="cn.itcast.mybatis.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>
執行java類方法:建立 GeneratorSqlmap.java sql
import java.io.File; import java.io.IOException; 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.exception.XMLParserException; 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(); } } }
2.4生成的代碼以下:數據庫
public class ItemsMapperTest { private ApplicationContext applicationContext; private ItemsMapper itemsMapper; @Before public void setUp() throws Exception { //初始化spring的容器
applicationContext =
new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); itemsMapper= (ItemsMapper) applicationContext.getBean("itemsMapper"); } //根據主鍵刪除
@Test public void testDeleteByPrimaryKey() { //根據主鍵刪除 // itemsMapper.deleteByPrimaryKey(id); //將符合條件的記錄刪除,參考下邊的查詢自定義條件example // itemsMapper.deleteByExample(example);
} //插入商品
@Test public void testInsert() { // itemsMapper.insert(record);
} //自定義條件查詢
@Test public void testSelectByExample() { ItemsExample itemsExample = new ItemsExample(); ItemsExample.Criteria criteria = itemsExample.createCriteria(); //使用criteria自定義查詢條件
criteria.andNameEqualTo("水杯"); criteria.andIdEqualTo(1); List<Items> list = itemsMapper.selectByExample(itemsExample); System.out.println(list); } //根據主鍵查詢
@Test public void testSelectByPrimaryKey() { Items items = itemsMapper.selectByPrimaryKey(1); System.out.println(items); } //更新
@Test public void testUpdateByPrimaryKey() { //根據主鍵更新,無論更新對象的屬性是否爲空所有更新 // itemsMapper.updateByPrimaryKey(record) //根據主鍵更新,只更新不爲空的屬性,參考mapper.xml中的代碼 // itemsMapper.updateByPrimaryKeySelective(record)
}