MyBatis 的逆向工程
概念:
1.正向工程:先创建java 实体类,由框架负责根据实体类生成数据库表。Hibernate 是支持正向工程的
2.逆向工程:先创建数据库表,由框架负责根据数据库表,反向生成如下资源:
Java 实体类Mapper 接口Mapper 映射文件
使用步骤:
1.导入依赖Jar 和插件
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.0</version>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.8</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
2.创建MyBatis 核心配置文件
主要创建的是:
mybatis-config 的核心配置文件和jdbc.properties 及添加log4j.xml
3.创建逆向工程的配置文件
文件名必须是:generatorConfig.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="DB2Tables" targetRuntime="MyBatis3">
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis"
userId="root"
password="root">
</jdbcConnection>
<javaModelGenerator targetPackage="com.haikang.mybatis.pojo" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="com.haikang.mybatis.mapper" targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.haikang.mybatis.mapper" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="emp" domainObjectName="Emp"/>
<table tableName="dept" domainObjectName="Dept"/>
</context>
</generatorConfiguration>
4.打开maven 找到对应的项目,再找到对应的mybatis-generatorgenerate 插件
双击就可以
运行后结果:
重点注意的是:选择性添加和修改,当传入的参数为null 时,选择性添加和修改,则不对该字段进行添加或修改,如果不是选择性添加和修改,如果当传入的参数为null 时,该是会被添加为null 或修改为null 的
选择性条件使用案例:【重点】
EmpExample empExample = new EmpExample();
EmpExample.Criteria criteria = empExample.createCriteria().andEmpNameEqualTo("大理").andAgeEqualTo(23);
逆工程在开发中使用是 MyBatis3: 生成带条件的CRUD(奢华尊享版)
方法:
逆工程使用的是QBC 风格查询
public class BQCTest {
@Test
public void delete(){
SqlSession sqlSession = SqlSessionUtils.sqlSession;
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
int i = mapper.deleteByPrimaryKey(21);
System.out.println(i);
EmpExample example = new EmpExample();
EmpExample.Criteria criteria = example.createCriteria().andEmpNameLike("%a%");
int result = mapper.deleteByExample(example);
System.out.println(result);
}
@Test
public void QBCInsert(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
int result = mapper.insert(new Emp(null,"大理",23,"男","123qq.com",null));
System.out.println(result);
int result2 = mapper.insertSelective(new Emp(null,"大理",23,"男","123qq.com",null));
System.out.println(result2);
}
@Test
public void QBCSelect(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
List<Emp> emps = mapper.selectByExample(null);
emps.forEach(emp -> System.out.println(emp));
EmpExample empExample = new EmpExample();
EmpExample.Criteria criteria = empExample.createCriteria().andEmpNameEqualTo("大理").andAgeEqualTo(23);
List<Emp> empList = mapper.selectByExample(empExample);
empList.forEach(emp -> System.out.println(emp));
Emp emp = mapper.selectByPrimaryKey(1);
System.out.println(emp);
}
@Test
public void BQCUpdate(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
EmpExample empExample = new EmpExample();
EmpExample.Criteria criteria = empExample.createCriteria().andEmpNameEqualTo("大理").
andAgeEqualTo(23).andDidEqualTo(2);
int result = mapper.updateByExampleSelective(new Emp(25,"大理",23,"男","123qq.com",null),empExample);
System.out.println(result);
}
}
|