Mybatis framework----->(5) Deeply understand the results of Mybatis package output and fuzzy query like

One, in-depth understanding of the results of Mybatis package output

1、resultType

  • Refers to the type of ResultSet conversion obtained by executing the SQL statement. The conversion type used here can be:
    (1) Fully qualified name: com.hcz.entity.Student
    (2) Type alias: for example, java.lang.Integer alias is int
    (3) Custom alias: defined in the mybatis main configuration file, use <typeAlias>Define alias, then you can use custom alias in resultType

Two ways to define aliases are as follows:

  • The first way:

  • type: the fully qualified name of the custom type

  • alias: alias (short, easy to remember)

<typeAlias type="com.hcz.entity.Student" alias="stu"/>
<typeAlias type="com.hcz.vo.ViewStudent" alias="vstu"/>
  • The second way:
  • <package> name is the package name, for all classes in this package, [class name] is the alias
<package name="com.hcz.entity"/>
<package name="com.hcz.vo"/>

Note: If the return is a collection, it should also be the type contained in the collection, not the collection List itself. As shown below:

Insert picture description here

Implementation principle:
1. Mybatis executes the SQL statement, and then mybatis calls the parameterless construction method of the class to create the object.
2. Mybatis calls the set method to assign the specified column value of the ResultSet to the attribute with the same name.

Mapping file:
<select id="selectMultiPosition" resultType="com.bjpowernode.domain.Student">
       select id,name, email,age from student
</select>
Approximately equivalent to the following jdbc code:
ResultSet rs = executeQuery(" select id,name  from student" )
while(rs.next()){
    
    
    Student  student = new Student();
	student.setId(rs.getInt("id"));
	student.setName(rs.getString("name"));
	studentList.add(student);
}
Here the column data after sql execution is converted to the Java object StudentInsert picture description here
(1) Simple type
  • Define a method in the interface
int countStudent();
  • Mapping file
<!--返回int类型数值-->
<select id="countStudent" resultType="int">
    select count(*) from student
</select>
  • Define the test method
@Test
public void testRetunInt(){
    
    
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    StudentDao dao = sqlSession.getMapper(StudentDao.class);
    int count = dao.countStudent();
    System.out.println("学生总人数:"+ count);
}
(2) Object type
  • Define a method in the interface
List<Student> selectMulitParam(@Param("myname") String name,
                                @Param("myage") Integer age);
  • Mapping file
<select id="selectMulitParam" resultType="Student">
    select * from student where name=#{myname} or age=#{myage}
</select>
  • Define the test method
@Test
public void testSelectMulitParam(){
    
    
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    StudentDao dao = sqlSession.getMapper(StudentDao.class);
    List<Student> studentList = dao.selectMulitParam("李四", 28);
    for (Student student: studentList){
    
    
        System.out.println("学生="+student);
    }
}
(3) Map type
  • The return value is map, where the column name (id, name, age) is the key value of the map, and the column value (1002, Zhang San, 34 is the value of the map)
  • It can only return one row of records, not multiple rows
  • Define a method in the interface
Map<Object,Object> selectReturnMap(int id);
  • Mapping file
<select id="selectReturnMap" resultType="java.util.HashMap">
    select * from student where id=#{sid}
</select>
  • Define the test method
public void testReturnMap(){
    
    
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    StudentDao dao = sqlSession.getMapper(StudentDao.class);
    Map<Object,Object> retMap = dao.selectReturnMap(1002);
    System.out.println("查询结果是 Map:"+retMap);
}

2、resultMap

  • ResultMap is the result map, which specifies the correspondence between the column name and the attribute of the Java object. It is more flexible to assign the column value to the specified attribute. It is often used when the column name and the Java object attribute name are inconsistent.

Usage:
(1) Define resultMap first, and specify the correspondence between column names and attributes
(2) In <select> Replace resultType with resultMap

  • Define a method in the interface
List<Student> selectUseResultMap(QueryParam param);
  • Mapping file
	<resultMap id="studentMap" type="Student">
        <!-- 主键字段使用 id -->
        <id column="id" property="id" />
        <!--非主键字段使用 result-->
        <result column="name" property="name"/>
        <result column="email" property="email" />
        <result column="age" property="age" />
    </resultMap>
        <!--resultMap: resultMap 标签中的 id 属性值-->
    <select id="selectUseResultMap" resultMap="studentMap">
        select * from student where name=#{paramName} or
        age=#{paramAge}
    </select>

Resolution:
(1) id: value is a custom unique name, used in <select>
(2) type: the fully qualified name or alias of the Java object that you want to convert to
(3) column: column name in the database
(4) property: the property name of the Java object
(5) resultMap: the id value of <resultMap>

  • Define the test method
@Test
public void testSelectUseResultMap(){
    
    
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    StudentDao dao = sqlSession.getMapper(StudentDao.class);
    QueryParam param = new QueryParam();
    param.setParamName("李四");
    param.setParamAge(28);
    List<Student> stuList = dao.selectUseResultMap(param);
    stuList.forEach( stu -> System.out.println(stu));
}

3. Two processing methods for entity class attribute names and column names are different

(1) Use <resultMap>
  • Define a method in the interface
List<MyStudent> selectMyStudent();
  • Mapping file
<resultMap id="myStudentMap" type="MyStudent">
    <id column="id" property="stuid" />
    <result column="name" property="stuname"/>
    <result column="email" property="stuemail" />
    <result column="age" property="stuage" />
</resultMap>

<select id="selectMyStudent" resultMap="myStudentMap">
    select * from student
</select>
  • Define the test method
@Test
public void testSelectMyStudent(){
    
    
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    StudentDao dao = sqlSession.getMapper(StudentDao.class);
    List<MyStudent> stuList = dao.selectMyStudent();
    stuList.forEach( stu -> System.out.println("MyStudent="+stu));
}
(2) Use column alias and <resultType>

The default principle of reultType is : the column value of the same name is assigned to the attribute of the same name, and the column alias (the attribute name of the Java object) is used here.

  • Define a method in the interface
List<MyStudent> selectDiffColProperty();
  • Mapping file
<select id="selectDiffColProperty" resultType="MyStudent">
    select id as stuid,name as stuname,email as stuemail,age as stuage
    from student
</select>
  • Define the test method
@Test
public void testSelectDiffColProperty(){
    
    
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    StudentDao dao = sqlSession.getMapper(StudentDao.class);
    List<MyStudent> stuList = dao.selectDiffColProperty();
    stuList.forEach( stu -> System.out.println("MyStudent="+stu));
}

Note : resultType and resultMap cannot be used together, only one of the two can be selected

Two, fuzzy query like

(1) Add "%xxx%" to the query data in the Java code (more convenient)

  • Define a method in the interface
List<Student> selectLikeOne(String name);
  • Mapping file
<select id="selectLikeOne" resultType="Student">
    select id,name,email,age from student where name like #{name }
</select>
  • Define the test method
@Test
public void testSelectLikeOne(){
    
    
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    StudentDao dao = sqlSession.getMapper(StudentDao.class);
    //准备好like的内容
    String name="%李%";
    List<Student> stuList = dao.selectLikeOne(name);
    stuList.forEach( stu -> System.out.println("OnelikeStudent="+stu));
}

(2) Add "%" #{xxx} "%" to the conditional position of the SQL statement in the mapping file

  • Define a method in the interface
List<Student> selectLikeTow(String name);
  • Mapping file
<select id="selectLikeTow" resultType="Student">
    select id,name,email,age from student where name like "%" #{name} "%"
</select>
  • Define the test method
@Test
public void testSelectLikeTow(){
    
    
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    StudentDao dao = sqlSession.getMapper(StudentDao.class);
    String name="李";
    List<Student> stuList = dao.selectLikeTow(name);
    stuList.forEach( stu -> System.out.println("TowlikeStudent="+stu));
}

Three, thinking outline

Insert picture description here

Please correct me if there are any deficiencies!

Guess you like

Origin blog.csdn.net/hcz666/article/details/113098063