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);
}
}
}
来源:oschina
链接:https://my.oschina.net/u/4395857/blog/3438349