Mybaits笔记框架:https://blog.csdn.net/qq_43751200/article/details/128154837
Mybatis中文官方文档: https://mybatis.org/mybatis-3/zh/index.html
Mybati的逆向工程
- 1. 正向工程 VS 逆向工程
- 2. 创建逆向工程的步骤(MyBatis3Simple清新简洁版)
- 2.1 添加依赖和插件
- 2.2 创建MyBatis的核心配置文件
- 2.3 创建逆向工程的配置文件
- 2.4 项目总体结构
- 2.5 执行MBG插件的generate目标
- 2.6 项目最终结构
- 2.7 生成的实体类、接口、配置文件查看
- 3. 创建逆向工程的步骤(MyBatis3奢华尊享版)
1. 正向工程 VS 逆向工程
-
正向工程:先创建Java实体类,由框架负责根据实体类生成数据库表。Hibernate是支持正向工程的
-
逆向工程:先创建数据库表,由框架负责根据数据库表,反向生成如下资源:
- Java实体类
- Mapper接口
- Mapper映射文件
2. 创建逆向工程的步骤(MyBatis3Simple清新简洁版)
2.1 添加依赖和插件
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>mybatis_05_MBG</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
</dependencies>
<!-- 控制Maven在构建过程中相关配置 -->
<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>
<!-- MySQL驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
2.2 创建MyBatis的核心配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 引入资源文件 -->
<properties resource="jdbc.properties"/>
<!--设置Mybatis的全局配置-->
<settings>
<!--将下划线_自动映射为驼峰,emp_name : empName -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!--开启延迟加载-->
<setting name="lazyLoadingEnabled" value="true"/>
</settings>
<!-- 给实体类起别名 -->
<typeAliases>
<!--
typeAlias:设置某个具体的类型的别名
属性:
type:需要设置别名的类型的全类名
alias:设置此类型的别名,且别名不区分大小写。若不设置此属性,该类型拥有默认的别名,即类名
-->
<!--<typeAlias type="com.atguigu.mybatis.bean.User"></typeAlias>-->
<!--<typeAlias type="com.atguigu.mybatis.bean.User" alias="user">
</typeAlias>-->
<!--以包为单位,设置改包下所有的类型都拥有默认的别名,即类名且不区分大小写-->
<package name="com.atguigu.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>
<!-- 映射文件XXXMapper.xml所在的包 -->
<mappers>
<!--
以包为单位,将包下所有的映射文件引入核心配置文件
注意:
1. 此方式必须保证mapper接口和mapper映射文件必须在相同的包下
2. mapper接口要和mapper映射文件的名字一致
-->
<package name="com.atguigu.mapper"/>
</mappers>
</configuration>
2.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>
<!--
targetRuntime: 执行生成的逆向工程的版本
MyBatis3Simple: 生成基本的CRUD(清新简洁版)
MyBatis3: 生成带条件的CRUD(奢华尊享版)
-->
<context id="DB2Tables" targetRuntime="MyBatis3Simple">
<!-- 数据库的连接信息 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8"
userId="root"
password="1234">
</jdbcConnection>
<!-- javaBean的生成策略-->
<javaModelGenerator targetPackage="com.atguigu.pojo" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- SQL映射文件的生成策略 -->
<sqlMapGenerator targetPackage="com.atguigu.mapper"
targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- Mapper接口的生成策略 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.atguigu.mapper" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 逆向分析的表 -->
<!-- tableName设置为*号,可以对应所有表,此时不写domainObjectName -->
<!-- domainObjectName属性指定生成出来的实体类的类名 -->
<table tableName="t_emp" domainObjectName="Emp"/>
<table tableName="t_dept" domainObjectName="Dept"/>
</context>
</generatorConfiguration>
2.4 项目总体结构
2.5 执行MBG插件的generate目标
2.6 项目最终结构
2.7 生成的实体类、接口、配置文件查看
Dept
package com.atguigu.pojo;
public class Dept {
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_dept.did
*
* @mbggenerated Tue Dec 13 10:17:42 CST 2022
*/
private Integer did;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_dept.dept_name
*
* @mbggenerated Tue Dec 13 10:17:42 CST 2022
*/
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 Tue Dec 13 10:17:42 CST 2022
*/
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 Tue Dec 13 10:17:42 CST 2022
*/
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 Tue Dec 13 10:17:42 CST 2022
*/
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 Tue Dec 13 10:17:42 CST 2022
*/
public void setDeptName(String deptName) {
this.deptName = deptName == null ? null : deptName.trim();
}
}
Emp
package com.atguigu.pojo;
public class Emp {
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_emp.eid
*
* @mbggenerated Tue Dec 13 10:17:42 CST 2022
*/
private Integer eid;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_emp.emp_name
*
* @mbggenerated Tue Dec 13 10:17:42 CST 2022
*/
private String empName;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_emp.age
*
* @mbggenerated Tue Dec 13 10:17:42 CST 2022
*/
private Integer age;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_emp.sex
*
* @mbggenerated Tue Dec 13 10:17:42 CST 2022
*/
private String sex;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_emp.email
*
* @mbggenerated Tue Dec 13 10:17:42 CST 2022
*/
private String email;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_emp.did
*
* @mbggenerated Tue Dec 13 10:17:42 CST 2022
*/
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 Tue Dec 13 10:17:42 CST 2022
*/
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 Tue Dec 13 10:17:42 CST 2022
*/
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 Tue Dec 13 10:17:42 CST 2022
*/
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 Tue Dec 13 10:17:42 CST 2022
*/
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 Tue Dec 13 10:17:42 CST 2022
*/
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 Tue Dec 13 10:17:42 CST 2022
*/
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 Tue Dec 13 10:17:42 CST 2022
*/
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 Tue Dec 13 10:17:42 CST 2022
*/
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 Tue Dec 13 10:17:42 CST 2022
*/
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 Tue Dec 13 10:17:42 CST 2022
*/
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 Tue Dec 13 10:17:42 CST 2022
*/
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 Tue Dec 13 10:17:42 CST 2022
*/
public void setDid(Integer did) {
this.did = did;
}
}
DeptMapper: 自动生成有增删改查方法
package com.atguigu.mapper;
import com.atguigu.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 Tue Dec 13 10:17:42 CST 2022
*/
int insert(Dept record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table t_dept
*
* @mbggenerated Tue Dec 13 10:17:42 CST 2022
*/
List<Dept> selectAll();
}
EmpMapper: 自动生成有增删改查方法
package com.atguigu.mapper;
import com.atguigu.pojo.Emp;
import java.util.List;
public interface EmpMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table t_emp
*
* @mbggenerated Tue Dec 13 10:17:42 CST 2022
*/
int deleteByPrimaryKey(Integer eid);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table t_emp
*
* @mbggenerated Tue Dec 13 10:17:42 CST 2022
*/
int insert(Emp record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table t_emp
*
* @mbggenerated Tue Dec 13 10:17:42 CST 2022
*/
Emp selectByPrimaryKey(Integer eid);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table t_emp
*
* @mbggenerated Tue Dec 13 10:17:42 CST 2022
*/
List<Emp> selectAll();
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table t_emp
*
* @mbggenerated Tue Dec 13 10:17:42 CST 2022
*/
int updateByPrimaryKey(Emp record);
}
DeptMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.atguigu.mapper.DeptMapper" >
<resultMap id="BaseResultMap" type="com.atguigu.pojo.Dept" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Tue Dec 13 10:17:42 CST 2022.
-->
<result column="did" property="did" jdbcType="INTEGER" />
<result column="dept_name" property="deptName" jdbcType="VARCHAR" />
</resultMap>
<insert id="insert" parameterType="com.atguigu.pojo.Dept" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Tue Dec 13 10:17:42 CST 2022.
-->
insert into t_dept (did, dept_name)
values (#{did,jdbcType=INTEGER}, #{deptName,jdbcType=VARCHAR})
</insert>
<select id="selectAll" resultMap="BaseResultMap" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Tue Dec 13 10:17:42 CST 2022.
-->
select did, dept_name
from t_dept
</select>
</mapper>
EmpMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.atguigu.mapper.EmpMapper" >
<resultMap id="BaseResultMap" type="com.atguigu.pojo.Emp" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Tue Dec 13 10:17:42 CST 2022.
-->
<id column="eid" property="eid" jdbcType="INTEGER" />
<result column="emp_name" property="empName" jdbcType="VARCHAR" />
<result column="age" property="age" jdbcType="INTEGER" />
<result column="sex" property="sex" jdbcType="CHAR" />
<result column="email" property="email" jdbcType="VARCHAR" />
<result column="did" property="did" jdbcType="INTEGER" />
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Tue Dec 13 10:17:42 CST 2022.
-->
delete from t_emp
where eid = #{eid,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.atguigu.pojo.Emp" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Tue Dec 13 10:17:42 CST 2022.
-->
insert into t_emp (eid, emp_name, age,
sex, email, did)
values (#{eid,jdbcType=INTEGER}, #{empName,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER},
#{sex,jdbcType=CHAR}, #{email,jdbcType=VARCHAR}, #{did,jdbcType=INTEGER})
</insert>
<update id="updateByPrimaryKey" parameterType="com.atguigu.pojo.Emp" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Tue Dec 13 10:17:42 CST 2022.
-->
update t_emp
set emp_name = #{empName,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER},
sex = #{sex,jdbcType=CHAR},
email = #{email,jdbcType=VARCHAR},
did = #{did,jdbcType=INTEGER}
where eid = #{eid,jdbcType=INTEGER}
</update>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Tue Dec 13 10:17:42 CST 2022.
-->
select eid, emp_name, age, sex, email, did
from t_emp
where eid = #{eid,jdbcType=INTEGER}
</select>
<select id="selectAll" resultMap="BaseResultMap" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Tue Dec 13 10:17:42 CST 2022.
-->
select eid, emp_name, age, sex, email, did
from t_emp
</select>
</mapper>
3. 创建逆向工程的步骤(MyBatis3奢华尊享版)
只需要修改generatorConfig.xml配置文件,将targetRuntime="MyBatis3Simple"修改为targetRuntime="MyBatis3"即可
<?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>
<!--
targetRuntime: 执行生成的逆向工程的版本
MyBatis3Simple: 生成基本的CRUD(清新简洁版)
MyBatis3: 生成带条件的CRUD(奢华尊享版)
-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 数据库的连接信息 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8"
userId="root"
password="1234">
</jdbcConnection>
<!-- javaBean的生成策略-->
<javaModelGenerator targetPackage="com.atguigu.pojo" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- SQL映射文件的生成策略 -->
<sqlMapGenerator targetPackage="com.atguigu.mapper"
targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- Mapper接口的生成策略 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.atguigu.mapper" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 逆向分析的表 -->
<!-- tableName设置为*号,可以对应所有表,此时不写domainObjectName -->
<!-- domainObjectName属性指定生成出来的实体类的类名 -->
<table tableName="t_emp" domainObjectName="Emp"/>
<table tableName="t_dept" domainObjectName="Dept"/>
</context>
</generatorConfiguration>
生成的项目框架
测试方法
@Test
public void testMethod(){
try {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
// 根据条件查询
EmpExample empExample = new EmpExample();
// 标准一:name为lsm且年龄等于231
empExample.createCriteria().andEmpNameEqualTo("lsm").andAgeEqualTo(231);
// 标准二与标准一or的关系,did不为空
empExample.or().andDidIsNotNull();
List<Emp> empList = empMapper.selectByExample(empExample);
empList.forEach(emp -> System.out.println(emp));
} catch (IOException e) {
e.printStackTrace();
}
}