1.逆向工程
用来生成po类
需要注意的是逆向工程生成的文件是追加的,所以生成下一个项目的同事就必须把上一次 的给删除掉。
2.搭建步骤:
①引入jar包
②新建一个generator.xml 同时修改相应的包名 (cn.frame.pojo)
<?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="root">
</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.frame.pojo"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="cn.frame.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="cn.frame.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>
③src下新建class StartServer
package generator0608;
import java.io.File;
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.internal.DefaultShellCallback;
public class StartServer {
public void generator() throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("genarator.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 {
StartServer startServer = new StartServer();
startServer.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}
④运行startServer并且刷新项目才可以看见:
沒有运行之前包结构:
运行后的包结构:
⑤自己编写的工程:
这样就可以把生成的目录结构直接粘贴 过来就可以用了。
⑥注意:
这里的OrdersExample是各种表里的复杂的查询条件做的一个封装。
⑦把pojo和mapper粘贴过来后就可以使用;
测试UserMapperTest:
package cn.frame.test;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itheima.mapper.UserMapper;
import cn.itheima.pojo.User;
import cn.itheima.pojo.UserExample;
import cn.itheima.pojo.UserExample.Criteria;
import javassist.compiler.ast.Symbol;
public class UserMapperTest {
private ApplicationContext applicatonContext;
@Before
public void setUp() throws Exception{
String configLocation = "classpath:ApplicationContext.xml";
applicatonContext = new ClassPathXmlApplicationContext(configLocation);
}
// @Test
// public void testFindUserById() throws Exception{
// UserMapper userMapper = (UserMapper)applicatonContext.getBean("userMapper");
//
// User user = userMapper.findUserById(1);
// System.out.println(user);
// }
@Test
public void testFindUserById() throws Exception{
UserMapper userMapper = (UserMapper)applicatonContext.getBean("userMapper");
User user = userMapper.selectByPrimaryKey(1);
System.out.println(user);
}
@Test
public void testFindUserAndSex() throws Exception{
UserMapper userMapper = (UserMapper)applicatonContext.getBean("userMapper");
//创建UserExample对象
UserExample userExample = new UserExample();
//通过UserExample对象创建查询条件封装对象(Criteria中是封装的查询条件)
Criteria createCriteria = userExample.createCriteria();
//加入查询条件
createCriteria.andUsernameLike("%王%");
createCriteria.andSexEqualTo("1");
List<User> list = userMapper.selectByExample(userExample);
System.out.println(list);
}
}