Java框架之mybatis(四)——多对一& 一对多处理、动态SQL & 缓存

多对一处理

多个对象对应一个对象

比如:你们都是我的学生 ,多个学生对应一个老师

掌握两个单词:

  • association — 联系 ,关联 多个人可以关联一个人。
  • collection — 集合 一个人有一个集合,包含多个人。
  • 发现是多对一业务情况,我们需要使用association 标签进行关联

多对一的处理方式一

使用数据库的思想处理:联表查询

  1. 定义dao接口
List<Student> getStudents();
  1. 编写查询语句
    1. 查询学生信息 id name tid , 由于我们要得到老师的信息,我们需要联表查询
    2. 查询老师的信息 id name 。
<!--遇到问题:学生类中关联老师: 多个学生对应一个老师 -->
<!--<select id="getStudents" resultType="Student">-->
    <!--select s.id,s.name,t.name from mybatis.student as s,mybatis.teacher as t-->
    <!--where s.tid = t.id-->
<!--</select>-->


<!--解决问题方式一:按查询结果嵌套处理,模拟数据库思想;
-->
<select id="getStudents" resultMap="StudentTeacher">
    select * from mybatis.student
</select>

<resultMap id="StudentTeacher" type="Student">
    <id column="id" property="id"/>
    <result column="name" property="name"/>
    <!--属性和字段对应  , 类和表对应  , 对象和记录
    关联一个字段
    需求:拿到老师这个类的属性

    association : 关联,多对一
        column : 数据库对应的列名
        property : 对应属性名
        javaType : 多对一字段对应的Java类型
        select : 关联一个语句
    -->
    <association column="tid" property="teacher" javaType="Teacher" select="getTeacher"/>
</resultMap>

<select id="getTeacher" resultType="Teacher">
    select * from mybatis.teacher where id = #{id}
</select>
  1. 测试类
@Test
    public void getStudents(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();

        StudentDao mapper = sqlSession.getMapper(StudentDao.class);

        List<Student> students = mapper.getStudents();

        for (Student student : students) {
            System.out.println("学生姓名:"+student.getName()+"\t老师姓名:"+student.getTeacher().getName());
        }

    }

多对一的处理方式二

1.编写接口

List<Student> getStudentsTwo();

2.编写处理的mapper

  1. 查询学生id,学生姓名,老师姓名,需要从学生表和老师表中查询
  2. 学生对应的类进行映射,发现老师一个对象 , 所以关联一个对象;
<!-- 解决方式二:一个resultMap解决 , 模拟面向对象的思想-->
<select id="getStudentsTwo" resultMap="StudentTeacher2">
    select s.id,s.name,t.name as tname from mybatis.student as s, mybatis.teacher as t
    where s.tid = t.id
</select>

<!--设置结果集映射ResultMap -->
<resultMap id="StudentTeacher2" type="Student">
    <id property="id" column="id"/>
    <result property="name" column="name"/>

    <!--直接关联一个老师-->
    <association property="teacher" javaType="Teacher">
        <result property="name" column="tname"/>
    </association>
</resultMap>

3.测试类

@Test
public void getStudentsTwo(){
    SqlSession sqlSession = MyBatisUtils.getSqlSession();

    StudentDao mapper = sqlSession.getMapper(StudentDao.class);

    List<Student> students = mapper.getStudentsTwo();

    for (Student student : students) {
        System.out.println("学生姓名:"+student.getName()+"\t老师姓名:"+student.getTeacher().getName());
    }

}

总结

  1. mybatis中遇到多对一的情况,要使用关联映射处理:使用association
  2. 两种处理思路:
    1. 数据库思想 : 联表查询
    2. OOP思想 :关联对象

项目实现:

项目结构:
在这里插入图片描述
数据库建立:

CREATE DATABASE /*!32312 IF NOT EXISTS*/`mybatis` /*!40100 DEFAULT CHARACTER SET utf8 */;

USE `mybatis`;

/*Table structure for table `student` */

DROP TABLE IF EXISTS `student`;

CREATE TABLE `student` (
  `id` INT(10) NOT NULL,
  `name` VARCHAR(30) DEFAULT NULL,
  `tid` INT(10) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fktid` (`tid`),
  CONSTRAINT `fktid` FOREIGN KEY (`tid`) REFERENCES `teacher` (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;

/*Data for the table `student` */

INSERT  INTO `student`(`id`,`name`,`tid`) VALUES (1,'武少伟',1),(2,'李鸿彬',1),(3,'武少伟',1),(4,'姚震',1),(5,'夏彬峰',1);

/*Table structure for table `teacher` */

DROP TABLE IF EXISTS `teacher`;

CREATE TABLE `teacher` (
  `id` INT(10) NOT NULL,
  `name` VARCHAR(30) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;

/*Data for the table `teacher` */

INSERT  INTO `teacher`(`id`,`name`) VALUES (1,'秦老师');


porm.xml文件配置:

<?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>com.kuang</groupId>
    <artifactId>ssm-mybatis-study</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <!--子项目-->
    <modules>
        <module>Mybatis01</module>
        <module>MyBatis02</module>
        <module>MyBatis03</module>
        <module>MyBatis04</module>
        <module>MyBatis05</module>
        <module>Mybatis06</module>
        <module>Mybatis07</module>
        <module>Mybatis08</module>
        <module>MyBatis09</module>
    </modules>

    <dependencies>

        <!--单元测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>

        <!--mybatis的包-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.1</version>
        </dependency>

        <!--连接数据库的驱动包-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

        <!--LOG4J日志包-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

    </dependencies>


    <build>
        <!--希望maven在导出项目的时,能够将我们的配置及资源导出-->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>

    </build>



</project>

其中build部分是必须的,否则最后资源导不出来!

pojo类编写:
student

package com.kuang.pojo;

public class Student  {
    private int id;
    private String name;
    private Teacher teacher;

    public Student() {
    }

    public Student(int id, String name, Teacher teacher) {
        this.id = id;
        this.name = name;
        this.teacher = teacher;
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", teacher=" + teacher +
                '}';
    }
}

teacher

package com.kuang.pojo;

public class Teacher {
    private int id;
    private String name;

    public Teacher() {
    }

    public Teacher(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

database.properties文件编写:

driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf-8
username = root
password = 123456

log4j.properties文件(日志实现):

### Log4j配置 ###
#定义log4j的输出级别和输出目的地(目的地可以自定义名称,和后面的对应)
#[ level ] , appenderName1 , appenderName2
log4j.rootLogger=DEBUG,console,file

#-----------------------------------#
#1 定义日志输出目的地为控制台
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.Threshold=DEBUG
####可以灵活地指定日志输出格式,下面一行是指定具体的格式 ###
#%c: 输出日志信息所属的类目,通常就是所在类的全名
#%m: 输出代码中指定的消息,产生的日志具体信息
#%n: 输出一个回车换行符,Windows平台为"/r/n",Unix平台为"/n"输出日志信息换行
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%c]-%m%n

#-----------------------------------#
#2 文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.file = org.apache.log4j.RollingFileAppender
#日志文件输出目录
log4j.appender.file.File=log/info.log
#定义文件最大大小
log4j.appender.file.MaxFileSize=10mb
###输出日志信息###
#最低级别
log4j.appender.file.Threshold=ERROR
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n

#-----------------------------------#
#3 druid
log4j.logger.druid.sql=INFO
log4j.logger.druid.sql.DataSource=info
log4j.logger.druid.sql.Connection=info
log4j.logger.druid.sql.Statement=info
log4j.logger.druid.sql.ResultSet=info

#4 mybatis 显示SQL语句部分
log4j.logger.org.mybatis=DEBUG
#log4j.logger.cn.tibet.cas.dao=DEBUG
#log4j.logger.org.mybatis.common.jdbc.SimpleDataSource=DEBUG
#log4j.logger.org.mybatis.common.jdbc.ScriptRunner=DEBUG
#log4j.logger.org.mybatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
#log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

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>

    <!--配置文件修改-->
    <properties resource="database.properties"/>

    <!--Mybatis设置-->
    <settings>
        <!--默认日志实现-->
        <!--<setting name="logImpl" value="STDOUT_LOGGING"/>-->

        <!--Log4j实现-->
        <setting name="logImpl" value="LOG4J"/>
    </settings>

    <!--配置别名-->
    <typeAliases>
        <!--<typeAlias type="com.kuang.pojo.User" alias="User"/>-->
        <package name="com.kuang.pojo"/>
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <!--class对应的是一个接口类-->
        <!--resource对应的是一个接口类的映射文件-->
        <mapper resource="com/kuang/dao/StudentMapper.xml"/>
    </mappers>

</configuration>

dao层:
StudentDao接口

package com.kuang.dao;

import com.kuang.pojo.Student;

import java.util.List;

public interface StudentDao {

    //获得全部学生的信息以及对应的老师
    List<Student> getStudents();


    //获得全部学生的信息以及对应的老师
    List<Student> getStudentsTwo();

}


对应的映射文件:StudentMapper.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">

<!--namespace不能写别名-->
<mapper namespace="com.kuang.dao.StudentDao">

    <!--遇到问题:学生类中关联老师: 多个学生对应一个老师 -->
    <!--<select id="getStudents" resultType="Student">-->
        <!--select s.id,s.name,t.name from mybatis.student as s,mybatis.teacher as t-->
        <!--where s.tid = t.id-->
    <!--</select>-->

    <!--解决问题方式一:按查询结果嵌套处理,模拟数据库思想;-->
    <select id="getStudents" resultMap="StudentTeacher">
        select * from mybatis.student
    </select>

    <resultMap id="StudentTeacher" type="Student">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <!--属性和字段对应  , 类和表对应  , 对象和记录
        关联一个字段
        需求:拿到老师这个类的属性

        association : 关联,多对一
            column : 数据库对应的列名
            property : 对应属性名
            javaType : 多对一字段对应的Java类型
            select : 关联一个语句
        -->
        <association column="tid" property="teacher" javaType="Teacher" select="getTeacher"/>
    </resultMap>

    <select id="getTeacher" resultType="Teacher">
        select * from mybatis.teacher where id = #{id}
    </select>




    <!-- 解决方式二:一个resultMap解决 , 模拟面向对象的思想-->
    <select id="getStudentsTwo" resultMap="StudentTeacher2">
        select s.id,s.name,t.id as tid,t.name as tname
        from mybatis.student as s, mybatis.teacher as t
        where s.tid = t.id
    </select>

    <!--设置结果集映射ResultMap -->
    <resultMap id="StudentTeacher2" type="Student">
        <id property="id" column="id"/>
        <result property="name" column="name"/>

        <!--直接关联一个老师-->
        <association property="teacher" javaType="Teacher">
            <id property="id" column="tid"/>
            <result property="name" column="tname"/>
        </association>
    </resultMap>


</mapper>

测试文件:StudentDaoTest

package com.kuang.dao;

import com.kuang.pojo.Student;
import com.kuang.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class StudentDaoTest {
    @Test
    public void getStudents(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();

        StudentDao mapper = sqlSession.getMapper(StudentDao.class);

        List<Student> students = mapper.getStudents();

        for (Student student : students) {
            System.out.println("学生姓名:"+student.getName()+"\t老师姓名:"+student.getTeacher().getName());
        }

    }
    StudentDao ma

    @Test
    public void getStudentsTwo(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
pper = sqlSession.getMapper(StudentDao.class);

        List<Student> students = mapper.getStudentsTwo();

        for (Student student : students) {
            System.out.println("学生姓名:"+student.getName()+"\t老师姓名:"+student.getTeacher().getName()
            +student.getTeacher().getId());
        }

    }



}



结果输出:
在这里插入图片描述

在这里插入图片描述

一对多处理

一个老师对应多个学生

一对多的业务:使用collection处理

环境搭建

一个老师对应多个学生

public class Teacher {
    private int id;
    private String name;

    //一个老师对应对个学生
    private List<Student> students;
}

编写代码

编写dao接口

package com.kuang.dao;

import com.kuang.pojo.Teacher;

public interface TeacherDao {

    //获得一个老师下的所有学生信息; 老师是包含学生的集合;
    Teacher getTeacher(int id);

    Teacher getTeacherTwo(int id);

}

对应mapper文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.kuang.dao.TeacherDao">

    <!--一对多的处理-->
    <!--面向对象方式解决-->
    <select id="getTeacher" resultMap="TeacherStudent">
        select s.name sname,s.id sid,t.id tid, t.name tname
        from mybatis.student as s,mybatis.teacher as t
        where s.tid = t.id and t.id = #{id}
    </select>
    <resultMap id="TeacherStudent" type="Teacher">
        <result property="name" column="tname"/>
        <collection property="students" ofType="Student">
            <id property="id" column="sid"/>
            <result property="name" column="sname"/>
        </collection>
    </resultMap>


    <!--数据库思想-->
    <select id="getTeacherTwo" resultMap="TeacherStudent2">
        select * from mybatis.teacher where id = #{id}
    </select>
    <resultMap id="TeacherStudent2" type="Teacher">
        <collection property="students" javaType="ArrayList" ofType="Student" column="id" select="T2"/>
    </resultMap>
    <select id="T2" resultType="Student">
        select * from mybatis.student where tid = #{id}
    </select>

</mapper>

测试类

package com.kuang.dao;

import com.kuang.pojo.Teacher;
import com.kuang.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;


public class TeacherDaoTest {
    @Test
    public void getTeacher(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        TeacherDao mapper = sqlSession.getMapper(TeacherDao.class);
        Teacher teacher = mapper.getTeacher(1);

        System.out.println(teacher.getName());
        System.out.println(teacher.getStudents());

    }

    @Test
    public void getTeacherTwo(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        TeacherDao mapper = sqlSession.getMapper(TeacherDao.class);
        Teacher teacher = mapper.getTeacherTwo(1);

        System.out.println(teacher.getName());
        System.out.println(teacher.getStudents());

    }
}

总结:

多对一:association 关联

一对多:collection 集合

两种解决方式:

  • 面对对象的思想:关联对象
  • SQL语句思想:联表查询

项目实现

项目结构:
在这里插入图片描述
porm.xml文件resources同上;

TeacherMapper.xml文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.kuang.dao.TeacherDao">

    <!--一对多的处理-->
    <!--面向对象方式解决-->
    <select id="getTeacher" resultMap="TeacherStudent">
        select s.name sname,s.id sid, t.name tname
        from mybatis.student as s,mybatis.teacher as t
        where s.tid = t.id and t.id = #{id}
    </select>
    <resultMap id="TeacherStudent" type="Teacher">
        <result property="id" column="id"/>
        <result property="name" column="tname"/>
        <collection property="students" ofType="Student">
            <id property="id" column="sid"/>
            <result property="name" column="sname"/>
        </collection>
    </resultMap>


    <!--数据库思想-->
    <select id="getTeacherTwo" resultMap="TeacherStudent2">
        select * from mybatis.teacher where id = #{id}
    </select>
    <resultMap id="TeacherStudent2" type="Teacher">
        <collection property="students" javaType="ArrayList" ofType="Student" column="id" select="T2"/>
    </resultMap>
    <select id="T2" resultType="Student">
        select * from mybatis.student where tid = #{id}
    </select>

</mapper>

结果输出:
在这里插入图片描述
在这里插入图片描述

动态SQL & 缓存

MyBatis 的强大特性之一便是它的动态 SQL。如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句的痛苦。

动态SQL就是指根据不同查询条件,生成不同的SQL语句

在 MyBatis 之前的版本中,有很多元素需要花时间了解。MyBatis 3 大大精简了元素种类,现在只需学习原来一半的元素便可。MyBatis 采用功能强大的基于 OGNL 的表达式来淘汰其它大部分元素。

if

参数test:里面的表达式如果为ture则执行,否则不执行

if(title != null)
<if test="title != null">
AND title like #{title}
</if>

choose

有时我们不想应用到所有的条件语句,而只想从中择其一项。针对这种情况,MyBatis 提供了 choose 元素,它有点像 Java 中的 switch 语句。

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG WHERE state = ‘ACTIVE’
  <choose>
    <when test="title != null">
      AND title like #{title}
    </when>
    <when test="author != null and author.name != null">
      AND author_name like #{author.name}
    </when>
    <otherwise>
      AND featured = 1
    </otherwise>
  </choose>
    
    
    switch (exp){
    	case 1:
    		break;
    	case 2:
    		break;
    }
    
</select>

trim[where,set]

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG
  <where>
    <if test="state != null">
         state = #{state}
    </if>
    <if test="title != null">
        AND title like #{title}
    </if>
    <if test="author != null and author.name != null">
        AND author_name like #{author.name}
    </if>
  </where>
</select>
<trim prefix="WHERE" prefixOverrides="AND |OR ">
  ...
</trim>
<update id="updateAuthorIfNecessary">
  update Author
    <set>
      <if test="username != null">username=#{username},</if>
      <if test="password != null">password=#{password},</if>
      <if test="email != null">email=#{email},</if>
      <if test="bio != null">bio=#{bio}</if>
    </set>
  where id=#{id}
</update>
<trim prefix="SET" suffixOverrides=",">
  ...
</trim>

foreach

<select id="selectPostIn" resultType="domain.blog.Post">
  SELECT *
  FROM POST P
  WHERE ID in
  <foreach item="item" index="index" collection="list"
      open="(" separator="," close=")">
        #{item}
  </foreach>
</select>

代码测试

测试模糊查询

接口编写

//模糊查询,可以通过自定义条件查询
List<User> getUserByLike(Map<String,Object> map);

映射文件编写

<select id="getUser" resultType="User">
    select * from mybatis.user
</select>

<select id="getUserByLike" resultType="User" parameterType="Map">
    select * from mybatis.user
    <where>
        <if test="name!=null">
            name like CONCAT('%',#{name},'%')
        </if>
        <if test="id!=null">
            and id = #{id}
        </if>
    </where>
</select>

测试类

@Test
public void getUserByLike(){
    SqlSession sqlSession = MyBatisUtils.getSqlSession();
    UserDao mapper = sqlSession.getMapper(UserDao.class);

    Map<String,Object> map = new HashMap<String,Object>();

    map.put("name","秦");
    map.put("id",1);

    List<User> users = mapper.getUserByLike(map);
    for (User user : users) {
        System.out.println(user);
    }
}

注意点:太过复杂的逻辑不建议使用动态SQL,简单的话可以直接使用动态SQL实现;

缓存

如果开启缓存,

在mapper映射文件中,添加一个标签

<!--开启缓存-->
<cache/>

如果要CRUD操作要查询结果需要缓存,可以使用usrCache;

<!--
useCache: 是否开启缓存
使用缓存可以解决问题:
    查询出来的结果暂时保存着,消耗内存资源;
    如果短时间查询同样的语句比较多,可以提高速度;
-->
<select id="getUser" resultType="User" useCache="true">
    select * from mybatis.user
</select>

项目实现:
项目结构:
在这里插入图片描述
UserDao层:

package com.kuang.dao;

import com.kuang.pojo.User;

import java.util.List;
import java.util.Map;

public interface UserDao {
    //查询全部用户
    List<User> getUser();

    //模糊查询,可以通过自定义条件查询
    List<User> getUserByLike(Map<String,Object> map);


}


对应的mapper文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.kuang.dao.UserDao">

    <!--开启缓存-->
    <cache/>

    <!--
    useCache: 是否开启缓存
    使用缓存可以解决问题:
        查询出来的结果暂时保存着,消耗内存资源;
        如果短时间查询同样的语句比较多,可以提高速度;
    -->
    <select id="getUser" resultType="User" useCache="true">
        select * from mybatis.user
    </select>

    <select id="getUserByLike" resultType="User" parameterType="Map">
        select * from mybatis.user
        <where>
            <if test="name!=null">
                name like CONCAT('%',#{name},'%')
            </if>
            <if test="id!=null">
                and id = #{id}
            </if>
        </where>
    </select>

</mapper>

测试类;

package com.kuang.dao;

import com.kuang.pojo.User;
import com.kuang.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//
public class UserDaoTest {

    @Test
    public void getUser(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        List<User> users = mapper.getUser();
        for (User user : users) {
            System.out.println(user);
        }
    }

    @Test
    public void getUserByLike(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);

        Map<String,Object> map = new HashMap<String,Object>();

        map.put("name","秦");
        map.put("id",1);

        List<User> users = mapper.getUserByLike(map);
        for (User user : users) {
            System.out.println(user);
        }
    }

}


测试结果:
在这里插入图片描述

在这里插入图片描述


版权声明:本文为yalu_123456原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
THE END
< <上一篇
下一篇>>