文章目录
- 1.准备工作
- 1.1 建表
- 1.2 创建Maven工程
- 1.2.1 在pom.xml中添加依赖和插件,更新maven
- 1.2.2 在src/main/resources下创建mybatis-config.xml
- 1.2.3 在src/main/resources下创建jdbc.properties
- 1.2.4 在src/main/resources下创建log4j.xml文件
- 1.2.5 在src/main/resources下创建逆向工程的配置文件generatorConfig.xml
- 2.执行生成的逆向工程的版本:MyBatis3
- 2.1 观察自动生成的文件
- 2.1.1 pojo中自动生成属性和get/set方法
- 2.1.2 Mapper接口中自动生成基础增删改查功能
- 2.1.3 Mapper映射文件中自动生成相对应方法的配置信息
- 3.执行生成的逆向工程的版本:MyBatis3
- 4.测试
- 4.1 测试前的准备
- 4.1.1 为实体类增加有参和无参构造器,并重写toString()方法
- 4.2 开始测试
- 4.2.1 测试查询所有数据
- 4.2.2 测试根据条件查询
- 4.2.2.1 链式添加条件(多条件查询)
- 4.2.3 测试两个条件用or连接
- 4.2.4 测试修改功能
- 4.2.4.1 测试直接修改
- 4.2.4.1 测试选择性修改
正向工程:先创建Java实体类,由框架负责根据实体类生成数据库表。Hibernate是支持正向工程的。
逆向工程:先创建数据库表,由框架负责根据数据库表,反向生成如下资源:
- Java实体类
- Mapper接口
- Mapper映射文件
1.准备工作
1.1 建表
t_emp
t_dept
将两张表建立关系
添加测试数据:
1.2 创建Maven工程
1.2.1 在pom.xml中添加依赖和插件,更新maven
<?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_MBG</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<!-- 依赖MyBatis核心包 -->
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!-- junit测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- MySQL驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.3</version>
</dependency>
<!-- log4j日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</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>5.1.8</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
1.2.2 在src/main/resources下创建mybatis-config.xml
复制代码后注意设置类型别名的包和映射文件的路径,如下图所示
<?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>
<!-- MyBatis核心配置文件,标签的顺序
properties?,settings?,typeAliases?,typeHandlers?,
objectFactory?,objectWrapperFactory?,reflectorFactory?,
plugins?,environments?,databaseIdProvider?,mappers?-->
<properties resource="jdbc.properties"></properties>
<!--设置类型别名,大小写不敏感。
如果不设置alias,则默认为类名(大小写不敏感)-->
<typeAliases>
<!--
typeAlias: 设置某个类型的别名
属性:
type 设置需要设置别名的类型
alias 设置某个类型的别名,如果不设置该属性,那么该类型拥有默认的类名,且不区分大小写
-->
<!--<typeAlias type="com.rqs.mybatis.pojo.User" alias="User"></typeAlias>-->
<!--推荐以包为单位,将包下所有的类型设置默认的类型别名且不区分大小写-->
<package name="类型别名所在的包,例如com.rqs.mybatis.pojo"/>
</typeAliases>
<!--设置连接数据库的环境-->
<!--每一个environment都是具体连接数据库的环境-->
<!--
一个项目中只会用一个环境,default用于使用默认使用的环境:
id:表示连接数据库的环境的唯一标识 不能重复
-->
<environments default="development">
<!--
transactionmanager:设置事务管理方式
属性:
type="JDBC/MANAGED"
JDBC: 在当前环境中,执行sql时,使用的时jdbc原声的事务管理方式,需要手动的提交和回滚事务
MANAGED:被管理,例如Spring
-->
<environment id="development">
<transactionManager type="JDBC"/>
<!-- dataSource:配置数据源
属性"
type:设置数据源的类型
type=""
POOLED:表示使用数据库连接池缓存数据库连接
UNPOOLED:表示不使用数据库连接池
JNDI:表示使用上下文中的数据源
-->
<dataSource type="POOLED">
<!--设置连接数据库的驱动-->
<property name="driver" value="${jdbc.driver}"/>
<!--设置连接地址-->
<property name="url" value="${jdbc.url}"/>
<!--注意:如果在建sql表单的时候选了字符集(如utf8),
这里的value要改成:value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8"-->
<!--用户名和密码-->
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!--引入映射文件-->
<mappers>
<!--
推荐以包为单位引入映射文件,要求:
1。 mapper接口所在的包要和映射文件所在的包一致
2。 mapper接口要和映射文件的名字一致-->
<!-- com.rqs.mybatis.mapper创建包时要用/分隔,这样才是目录,否则这整一个就只是文件夹名字而已-->
<package name="映射文件路径,例如com.rqs.mybatis.mapper"/>
</mappers>
</configuration>
1.2.3 在src/main/resources下创建jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8
jdbc.username=root
jdbc.password=root
1.2.4 在src/main/resources下创建log4j.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!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>
1.2.5 在src/main/resources下创建逆向工程的配置文件generatorConfig.xml
文件名必须是:generatorConfig.xml
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"报红不用管。
<?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.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis"
userId="root"
password="root">
</jdbcConnection>
<!-- javaBean的生成策略-->
<javaModelGenerator targetPackage="com.rqs.mybatis.pojo"
targetProject="./src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- SQL映射文件的生成策略 -->
<sqlMapGenerator targetPackage="com.rqs.mybatis.mapper"
targetProject="./src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- Mapper接口的生成策略 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.rqs.mybatis.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.执行生成的逆向工程的版本:MyBatis3
targetRuntime: 执行生成的逆向工程的版本
MyBatis3Simple: 生成基本的CRUD(清新简洁版)
- MyBatis3: 生成带条件的CRUD(奢华尊享版)
执行MBG插件的generate目标
操作前的文件目录:
操作后的文件目录:
自动创建了pojo、mapper接口(xxxMapper) 以及接口的映射文件
上图来源
2.1 观察自动生成的文件
2.1.1 pojo中自动生成属性和get/set方法
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();
}
}
2.1.2 Mapper接口中自动生成基础增删改查功能
此时逆向工程的配置文件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);
}
2.1.3 Mapper映射文件中自动生成相对应方法的配置信息
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.rqs.mybatis.mapper.DeptMapper" >
<resultMap id="BaseResultMap" type="com.rqs.mybatis.pojo.Dept" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sat Mar 04 15:44:42 CST 2023.
-->
<id column="did" property="did" jdbcType="INTEGER" />
<result column="dept_name" property="deptName" jdbcType="VARCHAR" />
</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 Sat Mar 04 15:44:42 CST 2023.
-->
delete from t_dept
where did = #{did,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.rqs.mybatis.pojo.Dept" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sat Mar 04 15:44:42 CST 2023.
-->
insert into t_dept (did, dept_name)
values (#{did,jdbcType=INTEGER}, #{deptName,jdbcType=VARCHAR})
</insert>
<update id="updateByPrimaryKey" parameterType="com.rqs.mybatis.pojo.Dept" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sat Mar 04 15:44:42 CST 2023.
-->
update t_dept
set dept_name = #{deptName,jdbcType=VARCHAR}
where did = #{did,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 Sat Mar 04 15:44:42 CST 2023.
-->
select did, dept_name
from t_dept
where did = #{did,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 Sat Mar 04 15:44:42 CST 2023.
-->
select did, dept_name
from t_dept
</select>
</mapper>
3.执行生成的逆向工程的版本:MyBatis3
targetRuntime: 执行生成的逆向工程的版本
- MyBatis3Simple: 生成基本的CRUD(清新简洁版)
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);
}
4.测试
4.1 测试前的准备
4.1.1 为实体类增加有参和无参构造器,并重写toString()方法
为实体类增加有参和无参构造器,并重写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 + '\'' +
'}';
}
}
4.2 开始测试
4.2.1 测试查询所有数据
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();
}
}
}
4.2.2 测试根据条件查询
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();
}
}
}
4.2.2.1 链式添加条件(多条件查询)
在上面条件查询的基础上,直接在后面通过.
添加条件
4.2.3 测试两个条件用or连接
4.2.4 测试修改功能
4.2.4.1 测试直接修改
修改前的数据
修改后的数据
4.2.4.1 测试选择性修改
修改前的数据
修改后的数据,sex没有变化,不作处理