天天看點

【MyBatis】的逆向工程

1.什麼是逆向工程

Mybatis提供來一個逆向工程工具,通過逆向工程,可以幫助程式員根據單表來生成po類、mapper映射檔案、mapper接口。

2.下載下傳逆向工程

https://github.com/mybatis/generator/releases/tag/mybatis-generator-1.3.2

【MyBatis】的逆向工程

3.建立逆向工程

【MyBatis】的逆向工程
1.建立Generator.java
@Test
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);
} 

2.添加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/taotao" userId="java"
            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="com.taotao.pojo"
            targetProject=".\src">
            <!-- enableSubPackages:是否讓schema作為包的字尾 -->
            <property name="enableSubPackages" value="false" />
            <!-- 從資料庫傳回的值被清理前後的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- targetProject:mapper映射檔案生成的位置 -->
        <sqlMapGenerator targetPackage="com.taotao.mapper" 
            targetProject=".\src">
            <!-- enableSubPackages:是否讓schema作為包的字尾 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!-- targetPackage:mapper接口生成的位置 -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.taotao.mapper" 
            targetProject=".\src">
            <!-- enableSubPackages:是否讓schema作為包的字尾 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>
        <!-- 指定資料庫表 -->
        <table schema="" tableName="tb_content"></table>
        <table schema="" tableName="tb_content_category"></table>
        <table schema="" tableName="tb_item"></table>
        <table schema="" tableName="tb_item_cat"></table>
        <table schema="" tableName="tb_item_desc"></table>
        <table schema="" tableName="tb_item_param"></table>
        <table schema="" tableName="tb_item_param_item"></table>
        <table schema="" tableName="tb_order"></table>
        <table schema="" tableName="tb_order_item"></table>
        <table schema="" tableName="tb_order_shipping"></table>
        <table schema="" tableName="tb_user"></table>
    </context>
</generatorConfiguration>

3.将逆向工程生成的代碼拷貝到指定項目中

4.使用逆向工程生成的代碼
@Autowired
private TbItemMapper itemMapper;    
@Override
public TbItem queryItemById(Long itemId) throws Exception {
//  TbItem item = itemMapper.selectByPrimaryKey(itemId);

    TbItemExample example = new TbItemExample();
    //建立查詢條件
    Criteria criteria = example.createCriteria();
    criteria.andIdEqualTo(itemId);
    List<TbItem> list = itemMapper.selectByExample(example);
    TbItem item = null;
    if(list != null && list.size() > 0 ){
        item = list.get(0);
    }
    return item;
}
           

4.注意事項

Mapper.xml檔案已經存在時,如果進行重新生成則mapper.xml檔案時,内容不被覆寫而是進行内容追加,結果導緻mybatis解析失敗。

解決方法:删除原來已經生成的mapper xml檔案再進行生成。

Mybatis自動生成的pojo及mapper.java檔案不是内容而是直接覆寫沒有此問題。

繼續閱讀