MyBatis——封装MyBatis的输出结果(resultType、resultMap)、使用like进行模糊查询的两种方式

简介: MyBatis——封装MyBatis的输出结果(resultType、resultMap)、使用like进行模糊查询的两种方式

文章目录:


1.封装MyBatis的输出结果

1.1 第一种方式——使用resultType

1.2 resultType返回简单类型的数据

1.3 resultType返回对象类型的数据

1.4 resultType返回Map类型的数据

1.5 resultType默认规则(同名的列赋值给同名的属性)

1.6 第二种方式——使用resultMap

1.7 列名和属性不相同的解决方式

1.7.1 使用resultMap(自定义列名和属性名之间的对应关系)

1.7.2 自定义列别名(使用列别名,使得列别名和Java对象属性名一样) 

1.8 多表查询的一个小实例

1.8.1 Navicat中创建两个表provincecity

1.8.2 创建一个ProvinceCity实体类

1.8.3 dao包下新建一个接口和该接口的mapper文件

1.8.4 mybatis主配置文件中添加相应的mapper标签内容

1.8.5 编写测试方法

1.8.6 运行结果(与Navicat中测试结果一样) 

2.MyBatis使用like进行模糊查询

2.1 方式一(推荐使用!!!)

2.2 方式二(不推荐。。。)

1.封装MyBatis的输出结果


1.1 第一种方式——使用resultType

resultType属性:在执行 select 时使用,作为 <select>标签的属性出现。

resultType表示结果类型,mysql执行sql语句,得到Java对象的类型,它的值有两种:

1.    Java类型的全限定名称

2.    使用别名(不推荐)

例如,下面这个例子:👇👇👇

接口中的方法为:   Student selectById(Integer id);
对应的mapper文件为:
<!--
    resultType: 1.java类型全限定名称  2.别名
    resultType: 表示mybatis执行sql语句后得到java对象类型
-->
<select id="selectById" resultType="com.bjpowernode.entity.Student">
    select id,name,email,age from student where id=#{studentId}
</select>
@Test
public void testSelectById() {
    SqlSession session = MyBatisUtil.getSqlSession();
    StudentDao studentDao=session.getMapper(StudentDao.class);
    Student student=studentDao.selectById(1003);
    System.out.println("student === " + student);
    session.close();
}

此时,resultType使用了 Java类型的全限定名称。表示的意思是 mybatis 执行sql语句,把ResultSet中的数据转为 Student 类型的对象。mybatis会执行以下操作:

1.    调用com.bjpowernode.entity.Student 的无参构造方法,创建对象。( Student student=new Student(); )使用反射创建对象。

2.    同名的列赋值给同名的属性。例如:student.setId(rs.getInt("id"))          student.setName(rs.getString("name"))

3.    得到相应的Java对象(根据接口中方法的返回值类型而定,如果是List,则mybatisStudent对象放入List集合)。

所以在测试方法中 Student student=studentDao.selectById(1003); 执行这行代码就会得到数据库中 id=1003 这行数据。

这行数据的列值赋给了student对象的属性,我们能得到student对象,就相当于是得到了 id=1003 这行数据。


1.2 resultType返回简单类型的数据


package com.bjpowernode.dao;
import com.bjpowernode.entity.Student;
/**
 *
 */
public interface StudentDao {
    long countStudent();
}
<!--
    执行sql语句,得到一个值(一行一列)
-->
<select id="countStudent" resultType="java.lang.Long">
    select count(*) from student
</select>
    @Test
    public void testCountStudent() {
        SqlSession session = MyBatisUtil.getSqlSession();
        StudentDao studentDao=session.getMapper(StudentDao.class);
        long nums=studentDao.countStudent();
        System.out.println("nums = " + nums);
        session.close();
    }

1.3 resultType返回对象类型的数据

package com.bjpowernode.dao;
import com.bjpowernode.entity.Student;
/**
 *
 */
public interface StudentDao {
    Student selectById(Integer id);
}
<!--
    resultType: 1.java类型全限定名称  2.别名
    resultType: 表示mybatis执行sql语句后得到java对象类型
-->
<select id="selectById" resultType="com.bjpowernode.entity.Student">
    select id,name,email,age from student where id=#{studentId}
</select>
    @Test
    public void testSelectById() {
        SqlSession session = MyBatisUtil.getSqlSession();
        StudentDao studentDao=session.getMapper(StudentDao.class);
        Student student=studentDao.selectById(1003);
        System.out.println("student === " + student);
        session.close();
    }

1.4 resultType返回Map类型的数据

package com.bjpowernode.dao;
import com.bjpowernode.entity.Student;
import org.apache.ibatis.annotations.Param;
import java.util.Map;
/**
 *
 */
public interface StudentDao {
    Map<Object,Object> selectMap(@Param("stuid") Integer id);
}
<!--
    执行sql语句,得到一个Map结构的数据
    sql语句执行结果中,列名为map的key,列值为map的value
    sql语句执行得到的是一行记录,此时转为map结构是正确的
    (如果得到的是一个map结构的数据,则sql语句最多能获取一行记录,超过一行就会报错)
-->
<select id="selectMap" resultType="java.util.Map">
    select  id,name from student where id!=#{stuid}
</select>
    @Test
    public void testSelectMap() {
        SqlSession session = MyBatisUtil.getSqlSession();
        StudentDao studentDao=session.getMapper(StudentDao.class);
        Map<Object,Object> map=studentDao.selectMap(1005);
        System.out.println("map === " + map);
        System.out.println("name === " + map.get("name"));
        System.out.println("id === " + map.get("id"));
        session.close();
    }

1.5 resultType默认规则(同名的列赋值给同名的属性)

1.6 第二种方式——使用resultMap

resultMap:结果映射,自定义列名和Java对象属性的对应关系,常用在列名和属性名不同的情况下。

package com.bjpowernode.dao;
import com.bjpowernode.entity.Student;
import com.bjpowernode.vo.CustomObject;
import org.apache.ibatis.annotations.Param;
/**
 *
 */
public interface StudentDao {
    CustomObject selectById2(@Param("cid") Integer id);
}
<!--
    定义resultMap
    id:给resultMap的映射关系起个名称,唯一值
    type:java类型的全限定名称
-->
<resultMap id="customMap" type="com.bjpowernode.vo.CustomObject">
    <!-- 定义列名和属性名的对应关系 -->
    <!-- 主键类型的列使用id标签 -->
    <id column="id" property="cid" />
    <!-- 非主键类型的列使用result标签 -->
    <result column="name" property="cname" />
    <!--列名和属性名相同的可以不用定义-->
    <result column="email" property="email" />
    <result column="age" property="age" />
</resultMap>
<!--
    使用resultMap属性,指定映射关系的id
    resultType和resultMap不能同时使用(二选一)
-->
<select id="selectById2" resultMap="customMap">
    select id,name,email,age from student where id=#{cid}
</select>
    @Test
    public void testSelectById2() {
        SqlSession session = MyBatisUtil.getSqlSession();
        StudentDao studentDao=session.getMapper(StudentDao.class);
        CustomObject customObject=studentDao.selectById2(1001);
        System.out.println("customObject = " + customObject);
        session.close();
    }

1.7 列名和属性不相同的解决方式

1.7.1 使用resultMap(自定义列名和属性名之间的对应关系)


1.7.2 自定义列别名(使用列别名,使得列别名和Java对象属性名一样) 

package com.bjpowernode.dao;
import com.bjpowernode.entity.Student;
import com.bjpowernode.vo.CustomObject;
import org.apache.ibatis.annotations.Param;
/**
 *
 */
public interface StudentDao {
    CustomObject selectById3(@Param("cid") Integer id);
}
<!--
    使用列别名,解决列名和属性名不同的问题
    在sql语句中将不相同的列名修改为与属性名相同即可
    select id cid,name cname
-->
<select id="selectById3" resultType="com.bjpowernode.vo.CustomObject">
    select id cid,name cname,email,age from student where id=#{cid}
</select>
    @Test
    public void testSelectById3() {
        SqlSession session = MyBatisUtil.getSqlSession();
        StudentDao studentDao=session.getMapper(StudentDao.class);
        CustomObject customObject=studentDao.selectById3(1001);
        System.out.println("customObject = " + customObject);
        session.close();
    }

1.8 多表查询的一个小实例

1.8.1 Navicat中创建两个表provincecity

每个属性的数据类型分别为:intvarcharvarcharvarchar id为主键

每个属性的数据类型分别为:intvarcharint  id为主键

1.8.2 创建一个ProvinceCity实体类

package com.bjpowernode.vo;
/**
 *
 */
public class ProvinceCity {
    private Integer pid;
    private String pname;
    private Integer cid;
    private String cname;
    public Integer getPid() {
        return pid;
    }
    public void setPid(Integer pid) {
        this.pid = pid;
    }
    public String getPname() {
        return pname;
    }
    public void setPname(String pname) {
        this.pname = pname;
    }
    public Integer getCid() {
        return cid;
    }
    public void setCid(Integer cid) {
        this.cid = cid;
    }
    public String getCname() {
        return cname;
    }
    public void setCname(String cname) {
        this.cname = cname;
    }
    @Override
    public String toString() {
        return "ProvinceCity{" +
                "pid=" + pid +
                ", pname='" + pname + '\'' +
                ", cid=" + cid +
                ", cname='" + cname + '\'' +
                '}';
    }
}



1.8.3 dao包下新建一个接口和该接口的mapper文件

package com.bjpowernode.dao;
import com.bjpowernode.vo.ProvinceCity;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
 *
 */
public interface ProvinceDao {
    List<ProvinceCity> selectProvinceCity(@Param("pid") Integer pid);
}


<?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.bjpowernode.dao.ProvinceDao">
    <!-- 使用insert、update、delete、select标签写sql -->
        <select id="selectProvinceCity" resultType="com.bjpowernode.vo.ProvinceCity">
                SELECT province.id pid,province.name pname,city.id cid,city.name cname
                from province
                join city on province.id=city.provinceid
                where province.id=#{pid}
        </select>
</mapper>

1.8.4 mybatis主配置文件中添加相应的mapper标签内容

<mappers>
    <mapper resource="com/bjpowernode/dao/ProvinceDao.xml"/>
</mappers>

1.8.5 编写测试方法

    @Test
    public void testSelectProvinceCity() {
        SqlSession session=MyBatisUtil.getSqlSession();
        ProvinceDao provinceDao=session.getMapper(ProvinceDao.class);
        List<ProvinceCity> provinceCities=provinceDao.selectProvinceCity(1);
        provinceCities.forEach( p-> System.out.println(p));
        session.close();
    }

1.8.6 运行结果(与Navicat中测试结果一样) 


2.MyBatis使用like进行模糊查询


2.1 方式一(推荐使用!!!)

Java程序中,把 like 的内容组装好,直接把这个内容传入到 sql 语句中。 

package com.bjpowernode.dao;
import com.bjpowernode.entity.Student;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
 *
 */
public interface StudentDao {
    List<Student> selectLikeOne(@Param("name") String name);
}
<!-- like的第一种方式 -->
<select id="selectLikeOne" resultType="com.bjpowernode.entity.Student">
    select * from student where name like #{name}
</select>
    @Test
    public void testLikeOne() {
        SqlSession session = MyBatisUtil.getSqlSession();
        StudentDao studentDao=session.getMapper(StudentDao.class);
        List<Student> students=studentDao.selectLikeOne("%张%");
        students.forEach( stu-> System.out.println(stu));
        session.close();
    }

这里使用like进行模糊查询:查询student表中,名字中包含 "" 字的所有人都信息。 

2.2 方式二(不推荐。。。)

sql语句中,组织 like 的内容。

package com.bjpowernode.dao;
import com.bjpowernode.entity.Student;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
 *
 */
public interface StudentDao {
    List<Student> selectLikeTwo(@Param("name") String name);
}
<!-- like的第二种方式 -->
<select id="selectLikeTwo" resultType="com.bjpowernode.entity.Student">
    select * from student where name like "%" #{name} "%"
</select>
    @Test
    public void testLikeTwo() {
        SqlSession session = MyBatisUtil.getSqlSession();
        StudentDao studentDao=session.getMapper(StudentDao.class);
        List<Student> students=studentDao.selectLikeTwo("张");
        students.forEach( stu-> System.out.println(stu));
        session.close();
    }

这里同样是使用like进行模糊查询:查询student表中,名字中包含 "" 字的所有人都信息。 

但是可以看到两种方式的mapper文件中,第二种方式的写法更为复杂,第一种方式比较符合大家对 sql 语句的理解和使用。


相关文章
|
2月前
|
SQL Java 数据库连接
|
5月前
|
SQL Java 数据库连接
Mybatis【Map传参与模糊查询】
Mybatis【Map传参与模糊查询】
|
16天前
|
API
Mybatis-Plus实现Service封装
Mybatis-Plus实现Service封装
16 1
|
1月前
|
XML Java 数据库连接
调用mybatisplus的封装CURD方法出现报错
调用mybatisplus的封装CURD方法出现报错
12 0
|
2月前
|
XML Java 数据库连接
mybatis的resultMap完美解析
mybatis的resultMap完美解析
25 0
|
4月前
|
Java 关系型数据库 MySQL
结合springboot+mybatis-plus+lombok,自定义Page封装类
结合springboot+mybatis-plus+lombok,自定义Page封装类
57 0
|
4月前
|
SQL 存储 Java
Mybatis之自定义映射resultMap
【1月更文挑战第3天】 一、resultMap处理字段和属性的映射关系 二、多对一映射处理 1、级联方式处理映射关系 2、使用association处理映射关系 3、分步查询 1. 查询员工信息 2. 查询部门信息 三、一对多映射处理 1、collection 2、分步查询 3. 查询部门信息 4. 根据部门id查询部门中的所有员工 四、延迟加载
156 2
Mybatis之自定义映射resultMap
|
4月前
|
SQL XML Java
mybatis元素类型为 "resultMap" 的内容必须匹配 "(constructor?,id *,result*,association报错解决
mybatis元素类型为 "resultMap" 的内容必须匹配 "(constructor?,id *,result*,association报错解决
55 0
|
5月前
|
Java 数据库连接 mybatis
Mybatis中模糊查询like语句的使用方法
Mybatis中模糊查询like语句的使用方法
36 0