java逆向工程删改查_基于maven+ssm的增删改查之使用mybatis逆向工程生成相关文件...

接上一节。

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数据表。相关表结构如下:

f8518b86be8a4ffb1fd979ff1cbdaec2.png

4c0c2616ccf69283207e17f96c84b089.png

tbl_emp中d_id以外键关联tbl_dept中的dept_id字段:

759caa22a7640d706d7fa58b59140aff.png

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();

}

}

}

可当模板,只需要指定逆向工程配置文件路径即可。

运行之后:会在指定目录下生成相关文件

39a8de67cc287fb5589fa1985bd8100a.png

里面的一些方法我们可以直接用,另外的需求需要我们自己进行更改。

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,"测试部"));

}

}

运行之后:

dc9bcf8e95ac2971a87e210b64a38eb2.png

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,这里就不继续测试了:

c7cce37d78126e3bce885e3fe09b529d.png

至此,逆向工程就完成了,下一节进行ssm框架的测试。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值