实现步骤:
1.创建简单的java项目
2.导入jar包,创建generator.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="mysqlTables" targetRuntime="MyBatis3">
<!--数据库配置-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis"
userId="root"
password="123456">
</jdbcConnection>
<!--java类型解析-->
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--模型生成包名,.\src代表当前项目下的src下面-->
<javaModelGenerator targetPackage="com.szl.backoffice.model" targetProject=".\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--mybatis的映射.xml-->
<sqlMapGenerator targetPackage="com.szl.backoffice.mapper" targetProject=".\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--mybtis的mapper接口文件-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.szl.backoffice.mapper" targetProject=".\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!--配置数据库中的表,如果只有一个单词,它会默认将第一个字母生成为大写(相当于items的domainObjectName),可以不用写-->
<table tableName="items" domainObjectName="Items" ></table>
<table tableName="orderdetail" domainObjectName="OrderDetail" ></table>
<table tableName="orders"></table>
<table tableName="user"></table>
</context>
</generatorConfiguration>
3.使用java类来执行逆向文件
public class Main {
public static void main(String[] args) throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//默认情况下文件是从当前项目开始找
File configFile = new File("src/generator.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);
}
}
4.把生成的代码拷贝到项目中
先拷贝模型,再拷贝映射文件和接口文件
5.在正式的项目中使用逆向工程生成的代码
测试类
@Test
public void test2() {
//1.加载spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("applicaitonContext.xml");
//2.获取userMapper
UserMapper userMapper = (UserMapper) context.getBean("userMapper");
//3.调用userMapper的方法,根据主键查询
User user = userMapper.selectByPrimaryKey(1);
System.out.println(user);
}
在model中除了生成模型之外,还生成了xxxExample的文件,而自动生成的Example是用于查询
@Test
public void test2() {
//自动生成的Example 是用于查询
//1.加载spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("applicaitonContext.xml");
//2.获取userMapper
UserMapper userMapper = (UserMapper) context.getBean("userMapper");
UserExample example=new UserExample();
UserExample.Criteria criteria=example.createCriteria();//获取查询条件
//example为每个字段都生成了一些方法
//查询性别为1(男)的用户
//criteria.andSexEqualTo("1");
//查询名字中有a的用户
criteria.andUsernameLike("%a%");
List<User> list=userMapper.selectByExample(example);
for (User user:list
) {
System.out.println(user);
}
}