idea 實現逆向工程(反向工程)


pom.xml 配置  在build里添加

 

 1 <!--逆向工程-->
 2     <plugins>
 3       <plugin>
 4         <groupId>org.mybatis.generator</groupId>
 5         <artifactId>mybatis-generator-maven-plugin</artifactId>
 6         <version>1.3.2</version>
 7         <dependencies>
 8           <dependency>
 9             <groupId>mysql</groupId>
10             <artifactId>mysql-connector-java</artifactId>
11             <version>5.1.22</version>
12           </dependency>
13         </dependencies>
14         <configuration>
15           <!--配置文件的路徑-->
16           <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
17           <overwrite>true</overwrite>
18         </configuration>
19       </plugin>
20     </plugins>

 

 

 

在resources添加文件 generatorConfig.xml 

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE generatorConfiguration
 3         PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
 4         "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
 5 <generatorConfiguration>
 6     <context id="test" targetRuntime="MyBatis3">
 7         <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>
 8         <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
 9         <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>
10         <commentGenerator>
11             <!-- 這個元素用來去除指定生成的注釋中是否包含生成的日期 false:表示保護 -->
12             <!-- 如果生成日期,會造成即使修改一個字段,整個實體類所有屬性都會發生變化,不利於版本控制,所以設置為true -->
13             <property name="suppressDate" value="true" />
14             <!-- 是否去除自動生成的注釋 true:是 : false:否 -->
15             <property name="suppressAllComments" value="true" />
16         </commentGenerator>
17         <!--數據庫鏈接URL,用戶名、密碼 -->
18         <jdbcConnection driverClass="com.mysql.jdbc.Driver"
19                         connectionURL="jdbc:mysql://localhost:3306/shy" userId="root"
20                         password="123456">
21         </jdbcConnection>
22         <javaTypeResolver>
23             <!-- This property is used to specify whether MyBatis Generator should
24                 force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, -->
25             <property name="forceBigDecimals" value="false" />
26         </javaTypeResolver>
27         <!-- 生成模型的包名和位置 -->
28         <javaModelGenerator targetPackage="com.etc.entity"
29                             targetProject="target">
30             <property name="enableSubPackages" value="true" />
31             <property name="trimStrings" value="true" />
32         </javaModelGenerator>
33         <!-- 生成映射文件的包名和位置 -->
34         <sqlMapGenerator targetPackage="com.etc.dao"
35                          targetProject="target">
36             <property name="enableSubPackages" value="true" />
37         </sqlMapGenerator>
38         <!-- 生成DAO的包名和位置 -->
39         <javaClientGenerator type="XMLMAPPER"
40                              targetPackage="com.etc.dao"
41                              targetProject="target">
42             <property name="enableSubPackages" value="true" />
43         </javaClientGenerator>
44 
45         <!-- 要生成哪些表 -->
46         <table tableName="t_cart" domainObjectName="Cart"></table>
47         <table tableName="t_furniture" domainObjectName="Furniture"></table>
48         <table tableName="t_order" domainObjectName="Order"></table>
49         <table tableName="t_type" domainObjectName="Type"></table>
50         <table tableName="t_user" domainObjectName="User"></table>
51 
52     </context>
53 </generatorConfiguration>

 

 

 

待idea 下載完成后 點擊最右邊的Mavenprojects

 

 在這會出現 mybatis-generator 運行下面的插件,只有能連上數據庫就ok了

生成的dao和entity在這

 

移動到對應的包下后測試:

package com.etc.dao;


import com.etc.entity.User;
import com.etc.entity.UserExample;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.IOException;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class UserDAOTest {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void test() throws IOException {

        //通過criteria構造查詢條件
        UserExample userExample = new UserExample();
        userExample.setOrderByClause("u_name asc"); //asc升序,desc降序排列
        userExample.setDistinct(false); //去除重復,true是選擇不重復記錄,false反之
        UserExample.Criteria criteria = userExample.createCriteria(); //構造自定義查詢條件
        criteria.andUNameEqualTo("qlt");

        //自定義查詢條件可能返回多條記錄,使用List接收
        List<User> list = userMapper.selectByExample(userExample);

        for(User u : list){
            System.out.println(u);
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM