MyBatis - 13 - MyBatis逆向工程

文章目录

  • 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
MyBatis - 13 - MyBatis逆向工程_第1张图片
MyBatis - 13 - MyBatis逆向工程_第2张图片

t_dept
MyBatis - 13 - MyBatis逆向工程_第3张图片
将两张表建立关系
MyBatis - 13 - MyBatis逆向工程_第4张图片

添加测试数据:
MyBatis - 13 - MyBatis逆向工程_第5张图片
MyBatis - 13 - MyBatis逆向工程_第6张图片

1.2 创建Maven工程

1.2.1 在pom.xml中添加依赖和插件,更新maven


<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>

1.2.2 在src/main/resources下创建mybatis-config.xml

复制代码后注意设置类型别名的包和映射文件的路径,如下图所示
MyBatis - 13 - MyBatis逆向工程_第7张图片
MyBatis - 13 - MyBatis逆向工程_第8张图片


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>

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文件


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"报红不用管。


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>

2.执行生成的逆向工程的版本:MyBatis3

targetRuntime: 执行生成的逆向工程的版本

  • MyBatis3Simple: 生成基本的CRUD(清新简洁版)
  • MyBatis3: 生成带条件的CRUD(奢华尊享版)

执行MBG插件的generate目标
MyBatis - 13 - MyBatis逆向工程_第9张图片
操作前的文件目录:
MyBatis - 13 - MyBatis逆向工程_第10张图片
操作后的文件目录:
自动创建了pojo、mapper接口(xxxMapper) 以及接口的映射文件
MyBatis - 13 - MyBatis逆向工程_第11张图片
MyBatis - 13 - MyBatis逆向工程_第12张图片

上图来源

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


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>

3.执行生成的逆向工程的版本:MyBatis3

targetRuntime: 执行生成的逆向工程的版本

  • MyBatis3Simple: 生成基本的CRUD(清新简洁版)
  • MyBatis3: 生成带条件的CRUD(奢华尊享版)

删除之前所生成的代码,如下图框中的
MyBatis - 13 - MyBatis逆向工程_第13张图片

尝试用MyBatis3生成,如下图所示,修改targetRuntime参数为MyBatis3: 生成带条件的CRUD,
MyBatis - 13 - MyBatis逆向工程_第14张图片

自动生成的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()方法

MyBatis - 13 - MyBatis逆向工程_第15张图片

MyBatis - 13 - MyBatis逆向工程_第16张图片

为实体类增加有参和无参构造器,并重写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();
        }
    }
}

MyBatis - 13 - MyBatis逆向工程_第17张图片

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();
        }
    }
}

MyBatis - 13 - MyBatis逆向工程_第18张图片
MyBatis - 13 - MyBatis逆向工程_第19张图片

4.2.2.1 链式添加条件(多条件查询)

在上面条件查询的基础上,直接在后面通过.添加条件
MyBatis - 13 - MyBatis逆向工程_第20张图片

在这里插入图片描述

4.2.3 测试两个条件用or连接

MyBatis - 13 - MyBatis逆向工程_第21张图片

4.2.4 测试修改功能

4.2.4.1 测试直接修改

修改前的数据
MyBatis - 13 - MyBatis逆向工程_第22张图片

MyBatis - 13 - MyBatis逆向工程_第23张图片
修改后的数据
MyBatis - 13 - MyBatis逆向工程_第24张图片

4.2.4.1 测试选择性修改

修改前的数据
MyBatis - 13 - MyBatis逆向工程_第25张图片
MyBatis - 13 - MyBatis逆向工程_第26张图片
修改后的数据,sex没有变化,不作处理
MyBatis - 13 - MyBatis逆向工程_第27张图片

你可能感兴趣的:(MyBatis,mybatis,java,maven)