接上一节。
1、mybatis逆向工程相关文件配置
generatorConfig.xml(8条)
(1)使用classPathEntry指定Mysql驱动的位置。
(2)去掉生成文件中的注释
(3)数据库连接配置
(4)类型解析
(5)javabean生成的位置、mapper接口的位置、mapper.xml文件的位置
(6)指定数据库中的表以及映射成的javabean的名称
/p>
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
2、相关数据库表
现有ssm_curd数据库,连接数据库的用户名为root,密码为123456,数据库中有tbl_emp何tbl_dept数据表。相关表结构如下:
tbl_emp中d_id以外键关联tbl_dept中的dept_id字段:
3、生成相关文件
接下来我们在conm.gong.curd.text新建一个GeneratorSqlmap.java用于生成相关文件
GeneratorSqlmap.java
packagecom.gong.curd.test;importjava.io.File;importjava.util.ArrayList;importjava.util.List;importorg.mybatis.generator.api.MyBatisGenerator;importorg.mybatis.generator.config.Configuration;importorg.mybatis.generator.config.xml.ConfigurationParser;importorg.mybatis.generator.internal.DefaultShellCallback;public classGeneratorSqlmap {public void generator() throwsException{
List warnings = new ArrayList();boolean overwrite = true;//指定 逆向工程配置文件
File configFile = new File("generatorConfig.xml");
ConfigurationParser cp= newConfigurationParser(warnings);
Configuration config=cp.parseConfiguration(configFile);
DefaultShellCallback callback= newDefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator= newMyBatisGenerator(config,
callback, warnings);
myBatisGenerator.generate(null);
}public static void main(String[] args) throwsException {try{
GeneratorSqlmap generatorSqlmap= newGeneratorSqlmap();
generatorSqlmap.generator();
}catch(Exception e) {
e.printStackTrace();
}
}
}
可当模板,只需要指定逆向工程配置文件路径即可。
运行之后:会在指定目录下生成相关文件
里面的一些方法我们可以直接用,另外的需求需要我们自己进行更改。
4、修改相关文件
(1) 修改Employee.java
引用Department:向里面加入private Departmen dept;
向里面加入有参构造器和无参构造器用于测试:
publicEmployee() {super();//TODO Auto-generated constructor stub
}publicEmployee(Integer empId, String empName, String gender, String email, Integer dId) {super();this.empId =empId;this.empName =empName;this.gender =gender;this.email =email;this.dId =dId;
}
(2)修改Department.java(向里面加入有参构造器和无参构造器)
publicDepartment() {super();
}publicDepartment(Integer deptId, String deptName) {super();this.deptId =deptId;this.deptName =deptName;
}
(3)修改EmployeMapper.java(由于我们关联了Department,因此原来的查询不能满足实际的需求,我们要重新定义两个方法,一个是根据条件查询出员工和部门,一个是根据主键查询员工和部门)
ListselectByExampleWithDept(EmployeeExample example);
Employee selectByPrimaryKeyWithDept(Integer empId);
(4)修改EmployeeMapper.xml
e.emp_id, e.emp_name, e.gender, e.email, e.d_id,d.dept_id,d.dept_name
selectdistinct
from tbl_emp e
left join tbl_dept d on e.`d_id`=d.`dept_id`
order by ${orderByClause}
selectfrom tbl_emp
left join tbl_dept d on e.`d_id`=d.`dept_id`
where e.emp_id = #{empId,jdbcType=INTEGER}
5、在com.gong.curd.test下建立DepartmentMapperTest.java和EmployeeMapperTest.java测试相关sql是否可用:
DepartmentMapperTest.java
测试加入两个部门:(注意我们将测试环境转换成spring测试环境,使用Runwith注解标识,同时使用ContextConfiguration指定spring配置文件)
packagecom.gong.curd.test;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.test.context.ContextConfiguration;importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;importcom.gong.curd.bean.Department;importcom.gong.curd.dao.DepartmentMapper;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:applicationContext.xml"})public classDepartmentMapperTest {
@AutowiredpublicDepartmentMapper departmentMapper;
@Testpublic voidtestCURD() {
departmentMapper.insertSelective(new Department(1,"开发部"));
departmentMapper.insertSelective(new Department(2,"测试部"));
}
}
运行之后:
EmployeeMapperTest.java(同样的转换成spring测试环境)
packagecom.gong.curd.test;importjava.util.UUID;importorg.apache.ibatis.session.SqlSession;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.test.context.ContextConfiguration;importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;importcom.gong.curd.bean.Employee;importcom.gong.curd.dao.EmployeeMapper;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")public classEmployeeMapperTest {
@Autowired
EmployeeMapper employeeMapper;
@Autowired
SqlSession sqlSession;
@Testpublic voidtestCURD() {
employeeMapper.insertSelective(new Employee(1,"tom","M","tom@qq.com",1));for(int i=2;i<50;i++) {
String uid= UUID.randomUUID().toString().substring(0, 5) +i;
employeeMapper.insertSelective(new Employee(i,uid,"M",uid+"@qq.com",1));
}
System.out.println("批量完成");
}
}
运行之后:我们批量插入了49条数据,当然还可以继续测试其它的sql,这里就不继续测试了:
至此,逆向工程就完成了,下一节进行ssm框架的测试。