什么是逆向工程
mybatis提供了一个逆向工程工具,通过逆向工程,可以帮助程序员根据单表来生成po类、mapper映射文件、mapper接口。
下载逆向工程
https://github.com/mybatis/generator/releases/tag/mybatis-generator-1.3.2
创建逆向工程
由于是从数据库中读表,jar包肯定少不了数据库驱动包~
添加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/shopping" 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="true" />
</javaTypeResolver>
<!-- targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="com.scx.po"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.scx.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.scx.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>
</context>
</generatorConfiguration>
创建Generator.java
public class Generator {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("src/generatorConfig.xml");
System.out.println(configFile);
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);
}
}
将逆向工程生成的代码拷贝到指定项目中
将逆向工程生成的po,maper接口,mapper映射文件全部copy到我们的项目中就可以进行使用了
使用生成的代码进行测试
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.scx.dao.ItemsMapper;
import com.scx.domain.Items;
import com.scx.domain.ItemsExample;
import com.scx.domain.ItemsExample.Criteria;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Test {
@Resource
ItemsMapper itemsMapper;
//根据主键查询商品
@org.junit.Test
public void find() {
Items item = itemsMapper.selectByPrimaryKey(1);
System.out.println(item);
}
//添加商品
@org.junit.Test
public void add(){
Items item=new Items();
item.setName("汽车");
item.setPrice(66666f);
item.setCreatetime(new Date());
itemsMapper.insert(item);
}
//修改商品
@org.junit.Test
public void update(){
Items item=itemsMapper.selectByPrimaryKey(4);
item.setPrice(77777f);
itemsMapper.updateByPrimaryKey(item);
}
//删除商品
@org.junit.Test
public void delete(){
itemsMapper.deleteByPrimaryKey(4);
}
/*
* mybatis反向工程会为我们自动生成example类
* example类与实体类对应,用作动态查询
*/
@org.junit.Test
public void myFind(){
ItemsExample example=new ItemsExample();
Criteria criteria = example.createCriteria();
criteria.andNameLike("汽车");
List<Items> items = itemsMapper.selectByExample(example);
for (Items item : items) {
System.out.println(item);
}
}
}
注意事项
Mapper.xml文件已经存在时,如果进行重新生成则mapper.xml文件时,内容不被覆盖而是进行内容追加,结果导致mybatis解析失败。
解决方法:删除原来已经生成的mapper xml文件再进行生成。
Mybatis自动生成的po及mapper.java文件不是内容而是直接覆盖没有此问题。