Mybatis(4)---encapsulate the output result of Mybatis, like query

Fourth, encapsulate the output result of Mybatis

The output refers to the Java object obtained after mybatis executes the sql statement

1.resultType

1. resultType: result type. Refers to the java object that the data is converted to after the sql statement is executed (the type of the object is arbitrary)

Processing method:
(1) Mybatis executes the sql statement, and calls the parameterless construction method of the class to create the object
(2) Mybatis assigns the value of the specified column of the ResultSet to the attribute of the same name

such as:

<select id="selectMutiParam" resultType="customDAO.Test">
        SELECT id,name FROM t_test;
</select>

Corresponding jdbc:

ResultSet rs = executeQuery("SELECT id,name FROM t_test");
while(rs.next())
{
    
    
	Test t = new Test();
	t.setId(rs.getInt("id"));
	t.setName(rs.getString("name"));
}

2. The value of resultType:
(1) The fully qualified name of the
type (2) The alias of the type (for example, the alias of java.lang.Integer is int)

3. Define the alias of the custom type
(1) Define in the mybatis main configuration file, use <typeAlias> to define the alias
(2) Use the custom alias in resultType

The first way

<typeAliases>
    <!--
        指定自定义类型的别名
        type:全限定名称
        alias:自定义的别名
    -->
    <typeAlias type="customDAO.Test" alias="Test"></typeAlias>
</typeAliases>

The second way

<!--
    第二种方式:
        使用<package>标签
        name是包名,这个包中所有类的类名就是别名
-->
<package name="customDAO"></package>

4. The return value is Map

Corresponding dao interface

package customDAO;

import org.apache.ibatis.annotations.Param;

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

//定义操作test表的接口
public interface TestDao
{
    
    
    //定义方法返回Map
    public Map<Object,Object> selectIdByMap(Integer id);
}

sql mapping file

<!--
    返回map:
        (1)列名是map的key,列值是map的value
        (2)sql查询结果最多只能有一条,如果多于一条会产生异常
-->
<select id="selectIdByMap" resultType="java.util.HashMap">
    SELECT * FROM t_test WHERE id = #{id};
</select>

2.resultMap

resultMap: result mapping, used to specify the correspondence between the column name and the attributes of the java object

(1) Assign a custom column value to an attribute
(2) When the column name is different from the attribute name, you must use resultMap

Corresponding dao interface

package customDAO;

import org.apache.ibatis.annotations.Param;

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

//定义操作test表的接口
public interface TestDao
{
    
    
    //使用resultMap定义映射关系
    public List<Test> selectAlltests();
}

sql mapping file

<!--
    使用resultMap
        (1)定义resultMap
        (2)在select标签中引用(1)中定义好的resultMap
-->
<!--
    定义resultMap
        id:自定义名称,表示你定义的这个resultMap
        type:Java类型的全限定名称
-->
<resultMap id="testMap" type="customDAO.Test">
    <!--
        指定列名和java对象属性间的关系
    -->
    <!--
        主键列使用id标签
            column:列名
            property:java类型的属性名
    -->
    <id column="id" property="id"></id>
    <!--
        非主键类使用result标签
    -->
    <result column="name" property="name"></result>
</resultMap>
<select id="selectAlltests" resultMap="testMap">
    SELECT * FROM t_test;
</select>

Test function

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

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

public class TestMybatis
{
    
    
    @Test
    public void testselectAllTests()
    {
    
    
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        TestDao testDao = sqlSession.getMapper(TestDao.class);
        List<customDAO.Test> list = testDao.selectAlltests();
        list.forEach(t-> System.out.println(t));
    }
}

The second way to solve the difference between the column name and the attribute name

Use column alias to solve, the defined category name is the attribute name of the class you defined

<select id="selectAlltests" resultMap="testMap">
    SELECT id as stuId FROM t_test;
</select>

3.like query

Two ways to use like query in mybatis

3.1 The first way

Directly pass in a string with wildcard characters

Methods of dao interface

import java.util.List;

//定义操作test表的接口
public interface TestDao
{
    
    
    //第一种模糊查询:在java代码中指定like的内容
    public List<Test> selectLikeOne(String name);
}

Statements in the sql mapping file

<!--
    第一种like查询
-->
<select id="selectLikeOne" resultType="customDAO.Test">
    SELECT * FROM t_test WHERE name LIKE #{name};
</select>

Incoming parameters

@Test
public void testLikeOne()
{
    
    
    SqlSession sqlSession = MybatisUtil.getSqlSession();
    TestDao testDao = sqlSession.getMapper(TestDao.class);
    //这里的字符串直接传入%name%,包含通配符
    List<customDAO.Test> list = testDao.selectLikeOne("%张%");
    list.forEach(t-> System.out.println(t));
}

3.2 The second way

Use string splicing

sql mapping file

<!--
    第二种like查询
        拼接字符串
-->
<select id="selectLikeTwo" resultType="customDAO.Test">
    SELECT * FROM t_test WHERE name LIKE "%" #{name} "%";
</select>

testing method

@Test
public void testLikeTwo()
{
    
    
    SqlSession sqlSession = MybatisUtil.getSqlSession();
    TestDao testDao = sqlSession.getMapper(TestDao.class);
    //这里的字符串直接传入name
    List<customDAO.Test> list = testDao.selectLikeTwo("李");
    list.forEach(t-> System.out.println(t));
}

Methods in the dao interface

import java.util.List;
//定义操作test表的接口
public interface TestDao
{
    
    
    //第二种模糊查询:在mapper文件中拼接like的内容
    public List<Test> selectLikeTwo(String name);
}

Guess you like

Origin blog.csdn.net/weixin_46841376/article/details/114640654