点击上方蓝字“小白编码”一起学习更多
写在前边
写在前边:今天给大家讲解Mybatis中逆向工程Generator插件,可以一键生成关键的CRUD代码!
逆向工程
逆向工程通常包括由数据库的表生成 Java 代码 和 通过 Java 代码生成数据库表。而Mybatis 逆向工程是指由数据库表生成 Java 代码。根据一个配置,自动生成!
Mybaits 需要程序员自己编写 SQL 语句,但是 Mybatis 官方提供逆向工程可以针对单表自动生成 Mybaits 执行所需要的代码,包括 POJO、Mapper.java、Mapper.xml,能过让项目更加便捷的成型开发。
generator插件
依赖:
<dependency>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-coreartifactId>
<version>1.3.7version>
dependency>
mgb.xml
<?xml version="1.0" encoding="UTF-8"?>br /> PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"><generatorConfiguration>
<context id="DB2Tables" targetRuntime="MyBatis3Simple">
<commentGenerator>
<property name="suppressAllComments" value="true"/>
commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"connectionURL="jdbc:mysql://localhost:3306/eesy_mybatis?allowMultiQueries=true"userId="root"password="123">
<property name="nullCatalogMeansCurrent" value="true"/>
jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
javaTypeResolver>
<javaModelGenerator targetPackage="cn.codewhite.pojo"targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
javaModelGenerator>
<sqlMapGenerator targetPackage="cn.codewhite.dao"targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true" />
sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="cn.codewhite.dao"targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
javaClientGenerator>
<table tableName="tbl_dept" domainObjectName="Department">table>
<table tableName="tbl_employee" domainObjectName="Employee">table>
context>generatorConfiguration>
创建mgb工程代码
@Testpublic void testMbg() throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("src/main/resources/mbg.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);}
一键生成成功
测试代码
@Test
public void selectAll() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
List<Employee> employees = mapper.selectAll();
employees.forEach(System.out::println);
sqlSession.close();
}
结果
拼装SQL测试
@Testpublic void testmyBatis3() {
SqlSession sqlSession = null;
try {
sqlSession = getSqlSessionFactory().openSession();
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
//xxxExample就是封装查询条件的
//1、查询所有
//List emps = mapper.selectByExample(null);
//2、查询员工名字中有e字母的,和员工性别是1的
//封装员工查询条件的example
EmployeeExample example = new EmployeeExample();
//创建一个Criteria,这个Criteria就是拼装查询条件
//select id, last_name, email, gender, d_id from tbl_employee
//WHERE ( last_name like ? and gender = ? ) or email like "%e%"
EmployeeExample.Criteria criteria = example.createCriteria();
criteria.andLastNameLike("%e%");
criteria.andGenderEqualTo("1");
List<Employee> employees = mapper.selectByExample(example);
employees.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
} finally {
assert sqlSession != null;
sqlSession.close();
}
结果
扫码学习更多作者:JUNSHI,微信:codewihte077。