正向工程:先创建Java实体类,由框架负责根据实体类生成数据库表。Hibernate是支持正向工程的。
逆向工程:先创建数据库表,由框架负责根据数据库表,反向生成如下资源:
- Java实体类
- Mapper接口
- Mapper映射文件
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>MyBatis_MBGartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.7version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.3version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>org.junit.jupitergroupId>
<artifactId>junit-jupiterartifactId>
<version>RELEASEversion>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-maven-pluginartifactId>
<version>1.3.0version>
<dependencies>
<dependency>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-coreartifactId>
<version>1.3.2version>
dependency>
<dependency>
<groupId>com.mchangegroupId>
<artifactId>c3p0artifactId>
<version>0.9.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.8version>
dependency>
dependencies>
plugin>
plugins>
build>
project>
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties">properties>
<typeAliases>
<package name="类型别名所在的包,例如com.rqs.mybatis.pojo"/>
typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
dataSource>
environment>
environments>
<mappers>
<package name="映射文件路径,例如com.rqs.mybatis.mapper"/>
mappers>
configuration>
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8
jdbc.username=root
jdbc.password=root
DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jakarta.apache.org/log4j/ ">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<param name="Encoding" value="UTF-8"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS}
%m (%F:%L) \n"/>
layout>
appender>
<logger name="java.sql">
<level value="debug"/>
logger>
<logger name="org.apache.ibatis">
<level value="info"/>
logger>
<root>
<level value="debug"/>
<appender-ref ref="STDOUT"/>
root>
log4j:configuration>
文件名必须是:generatorConfig.xml
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"报红不用管。
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="MyBatis3Simple">
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis"
userId="root"
password="root">
jdbcConnection>
<javaModelGenerator targetPackage="com.rqs.mybatis.pojo"
targetProject="./src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
javaModelGenerator>
<sqlMapGenerator targetPackage="com.rqs.mybatis.mapper"
targetProject="./src/main/resources">
<property name="enableSubPackages" value="true"/>
sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.rqs.mybatis.mapper"
targetProject="./src/main/java">
<property name="enableSubPackages" value="true"/>
javaClientGenerator>
<table tableName="t_emp" domainObjectName="Emp"/>
<table tableName="t_dept" domainObjectName="Dept"/>
context>
generatorConfiguration>
targetRuntime: 执行生成的逆向工程的版本
MyBatis3Simple: 生成基本的CRUD(清新简洁版)
执行MBG插件的generate目标
操作前的文件目录:
操作后的文件目录:
自动创建了pojo、mapper接口(xxxMapper) 以及接口的映射文件
上图来源
Dept类:
package com.rqs.mybatis.pojo;
public class Dept {
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_dept.did
*
* @mbggenerated Sat Mar 04 15:44:42 CST 2023
*/
private Integer did;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_dept.dept_name
*
* @mbggenerated Sat Mar 04 15:44:42 CST 2023
*/
private String deptName;
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column t_dept.did
*
* @return the value of t_dept.did
*
* @mbggenerated Sat Mar 04 15:44:42 CST 2023
*/
public Integer getDid() {
return did;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column t_dept.did
*
* @param did the value for t_dept.did
*
* @mbggenerated Sat Mar 04 15:44:42 CST 2023
*/
public void setDid(Integer did) {
this.did = did;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column t_dept.dept_name
*
* @return the value of t_dept.dept_name
*
* @mbggenerated Sat Mar 04 15:44:42 CST 2023
*/
public String getDeptName() {
return deptName;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column t_dept.dept_name
*
* @param deptName the value for t_dept.dept_name
*
* @mbggenerated Sat Mar 04 15:44:42 CST 2023
*/
public void setDeptName(String deptName) {
this.deptName = deptName == null ? null : deptName.trim();
}
}
此时逆向工程的配置文件generatorConfig.xml中:targetRuntime=“MyBatis3Simple"
DeptMapper接口
package com.rqs.mybatis.mapper;
import com.rqs.mybatis.pojo.Dept;
import java.util.List;
public interface DeptMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table t_dept
*
* @mbggenerated Sat Mar 04 15:44:42 CST 2023
*/
int deleteByPrimaryKey(Integer did);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table t_dept
*
* @mbggenerated Sat Mar 04 15:44:42 CST 2023
*/
int insert(Dept record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table t_dept
*
* @mbggenerated Sat Mar 04 15:44:42 CST 2023
*/
Dept selectByPrimaryKey(Integer did);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table t_dept
*
* @mbggenerated Sat Mar 04 15:44:42 CST 2023
*/
List<Dept> selectAll();
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table t_dept
*
* @mbggenerated Sat Mar 04 15:44:42 CST 2023
*/
int updateByPrimaryKey(Dept record);
}
DeptMapper.xml
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.rqs.mybatis.mapper.DeptMapper" >
<resultMap id="BaseResultMap" type="com.rqs.mybatis.pojo.Dept" >
<id column="did" property="did" jdbcType="INTEGER" />
<result column="dept_name" property="deptName" jdbcType="VARCHAR" />
resultMap>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from t_dept
where did = #{did,jdbcType=INTEGER}
delete>
<insert id="insert" parameterType="com.rqs.mybatis.pojo.Dept" >
insert into t_dept (did, dept_name)
values (#{did,jdbcType=INTEGER}, #{deptName,jdbcType=VARCHAR})
insert>
<update id="updateByPrimaryKey" parameterType="com.rqs.mybatis.pojo.Dept" >
update t_dept
set dept_name = #{deptName,jdbcType=VARCHAR}
where did = #{did,jdbcType=INTEGER}
update>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select did, dept_name
from t_dept
where did = #{did,jdbcType=INTEGER}
select>
<select id="selectAll" resultMap="BaseResultMap" >
select did, dept_name
from t_dept
select>
mapper>
targetRuntime: 执行生成的逆向工程的版本
MyBatis3: 生成带条件的CRUD(奢华尊享版)
尝试用MyBatis3生成,如下图所示,修改targetRuntime参数为MyBatis3: 生成带条件的CRUD,
自动生成的Mapper接口中的方法
public interface EmpMapper {
// 根据条件计数
int countByExample(EmpExample example);
//根据条件删除
int deleteByExample(EmpExample example);
//根据主键删除
int deleteByPrimaryKey(Integer eid);
//普通插入
int insert(Emp record);
//选择性插入:没写的就是null
int insertSelective(Emp record);
//根据条件查询
List<Emp> selectByExample(EmpExample example);
//根据主键查询
Emp selectByPrimaryKey(Integer eid);
//根据条件选择性修改:
int updateByExampleSelective(@Param("record") Emp record, @Param("example") EmpExample example);
//根据条件修改
int updateByExample(@Param("record") Emp record, @Param("example") EmpExample example);
//根据主键选择性修改
int updateByPrimaryKeySelective(Emp record);
//根据主键修改
int updateByPrimaryKey(Emp record);
}
为实体类增加有参和无参构造器,并重写toString()方法后的完整代码:
Emp实体类:
package com.rqs.mybatis.pojo;
public class Emp {
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_emp.eid
*
* @mbggenerated Sat Mar 04 16:24:19 CST 2023
*/
private Integer eid;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_emp.emp_name
*
* @mbggenerated Sat Mar 04 16:24:19 CST 2023
*/
private String empName;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_emp.age
*
* @mbggenerated Sat Mar 04 16:24:19 CST 2023
*/
private Integer age;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_emp.sex
*
* @mbggenerated Sat Mar 04 16:24:19 CST 2023
*/
private String sex;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_emp.email
*
* @mbggenerated Sat Mar 04 16:24:19 CST 2023
*/
private String email;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_emp.did
*
* @mbggenerated Sat Mar 04 16:24:19 CST 2023
*/
private Integer did;
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column t_emp.eid
*
* @return the value of t_emp.eid
*
* @mbggenerated Sat Mar 04 16:24:19 CST 2023
*/
public Integer getEid() {
return eid;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column t_emp.eid
*
* @param eid the value for t_emp.eid
*
* @mbggenerated Sat Mar 04 16:24:19 CST 2023
*/
public void setEid(Integer eid) {
this.eid = eid;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column t_emp.emp_name
*
* @return the value of t_emp.emp_name
*
* @mbggenerated Sat Mar 04 16:24:19 CST 2023
*/
public String getEmpName() {
return empName;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column t_emp.emp_name
*
* @param empName the value for t_emp.emp_name
*
* @mbggenerated Sat Mar 04 16:24:19 CST 2023
*/
public void setEmpName(String empName) {
this.empName = empName == null ? null : empName.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column t_emp.age
*
* @return the value of t_emp.age
*
* @mbggenerated Sat Mar 04 16:24:19 CST 2023
*/
public Integer getAge() {
return age;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column t_emp.age
*
* @param age the value for t_emp.age
*
* @mbggenerated Sat Mar 04 16:24:19 CST 2023
*/
public void setAge(Integer age) {
this.age = age;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column t_emp.sex
*
* @return the value of t_emp.sex
*
* @mbggenerated Sat Mar 04 16:24:19 CST 2023
*/
public String getSex() {
return sex;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column t_emp.sex
*
* @param sex the value for t_emp.sex
*
* @mbggenerated Sat Mar 04 16:24:19 CST 2023
*/
public void setSex(String sex) {
this.sex = sex == null ? null : sex.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column t_emp.email
*
* @return the value of t_emp.email
*
* @mbggenerated Sat Mar 04 16:24:19 CST 2023
*/
public String getEmail() {
return email;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column t_emp.email
*
* @param email the value for t_emp.email
*
* @mbggenerated Sat Mar 04 16:24:19 CST 2023
*/
public void setEmail(String email) {
this.email = email == null ? null : email.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column t_emp.did
*
* @return the value of t_emp.did
*
* @mbggenerated Sat Mar 04 16:24:19 CST 2023
*/
public Integer getDid() {
return did;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column t_emp.did
*
* @param did the value for t_emp.did
*
* @mbggenerated Sat Mar 04 16:24:19 CST 2023
*/
public void setDid(Integer did) {
this.did = did;
}
public Emp() {
}
public Emp(Integer eid, String empName, Integer age, String sex, String email, Integer did) {
this.eid = eid;
this.empName = empName;
this.age = age;
this.sex = sex;
this.email = email;
this.did = did;
}
@Override
public String toString() {
return "Emp{" +
"eid=" + eid +
", empName='" + empName + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
", email='" + email + '\'' +
", did=" + did +
'}';
}
}
Dept实体类:
package com.rqs.mybatis.pojo;
public class Dept {
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_dept.did
*
* @mbggenerated Sat Mar 04 16:24:19 CST 2023
*/
private Integer did;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_dept.dept_name
*
* @mbggenerated Sat Mar 04 16:24:19 CST 2023
*/
private String deptName;
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column t_dept.did
*
* @return the value of t_dept.did
*
* @mbggenerated Sat Mar 04 16:24:19 CST 2023
*/
public Integer getDid() {
return did;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column t_dept.did
*
* @param did the value for t_dept.did
*
* @mbggenerated Sat Mar 04 16:24:19 CST 2023
*/
public void setDid(Integer did) {
this.did = did;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column t_dept.dept_name
*
* @return the value of t_dept.dept_name
*
* @mbggenerated Sat Mar 04 16:24:19 CST 2023
*/
public String getDeptName() {
return deptName;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column t_dept.dept_name
*
* @param deptName the value for t_dept.dept_name
*
* @mbggenerated Sat Mar 04 16:24:19 CST 2023
*/
public void setDeptName(String deptName) {
this.deptName = deptName == null ? null : deptName.trim();
}
public Dept() {
}
public Dept(Integer did, String deptName) {
this.did = did;
this.deptName = deptName;
}
@Override
public String toString() {
return "Dept{" +
"did=" + did +
", deptName='" + deptName + '\'' +
'}';
}
}
import com.rqs.mybatis.mapper.EmpMapper;
import com.rqs.mybatis.pojo.Emp;
import com.rqs.mybatis.pojo.EmpExample;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MBGTest {
@Test
public void testMBG(){
try {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
//查询所有数据(条件写null,就是没有条件,就会查询所有数据)
List<Emp> list = mapper.selectByExample(null);
list.forEach(emp -> System.out.println(emp));
} catch (IOException e) {
e.printStackTrace();
}
}
}
import com.rqs.mybatis.mapper.EmpMapper;
import com.rqs.mybatis.pojo.Emp;
import com.rqs.mybatis.pojo.EmpExample;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MBGTest {
@Test
public void testMBG(){
try {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
//根据条件查询
//要求创建EmpExample对象
EmpExample example = new EmpExample();
//createCriteria()创建条件 createCriteria().后面所有的方法都是add开头
//查询名字为“rqs”的Emp
example.createCriteria().andEmpNameEqualTo("rqs");
List<Emp> list = mapper.selectByExample(example);
list.forEach(emp -> System.out.println(emp));
} catch (IOException e) {
e.printStackTrace();
}
}
}