MyBatis02-Parameter Passing and Result Encapsulation
1. Parameter Transfer
1. Parameter Types
We can do that in studentDao. The XML file specifies what type of parameters are in this method.
<?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.xuangong.mapper.StudentDao"> <select id="getOneStudent" resultType="com.xuangong.pojo.Student" parameterType="int"> select id,name,email,age,money from student where id = #{id} </select> </mapper>
Here the parameterType can be used to place the type of the parameter. Of course, MyBatis supports automatic recognition, so it's OK not to write, but there are a few things to note when writing.
Parameter types can be written as follows:
These correspond to each other, not to the relationship between the base type and the reference type, but to the relationship between the alias and the actual class name.
2. Pass a parameter
The previous example is to pass a parameter, which is no longer discussed.
3. Passing Multiple Parameters
You need to use the @Param annotation here to make mapper.xml and multiple parameters in the comment match each other, or you don't know who corresponds to whom.
Start by creating the StudentDao interface, where you write the name of the mapping.
public interface StudentDao { Student getOneStudent(Integer id); //In fact, the name of the parameter after the type here does not matter anything, mainly the name in @Param, which should correspond to each other in the sql statement, otherwise it cannot be found List<Student> getSomeStudent(@Param("myId") Integer myId, @Param("myName") String username); }
4. Delivery Object
To pass an object, create an object class first.
public class QueryParam { private String queryName; private Integer queryAge; public String getQueryName() { return queryName; } public void setQueryName(String queryName) { this.queryName = queryName; } public Integer getQueryAge() { return queryAge; } public void setQueryAge(Integer queryAge) { this.queryAge = queryAge; } public QueryParam(String queryName, Integer queryAge) { this.queryName = queryName; this.queryAge = queryAge; } }
Then write the Dao interface and dao.xml.
List<Student> getStudentByQuery(QueryParam queryParam); int insertStudentByQuery(QueryParam queryParam);
StudentDao.xml file.
<select id="getStudentByQuery" resultType="com.xuangong.pojo.Student"> select id,name,email,age,money from student where name = #{queryName} or age = #{queryAge} </select> <insert id="insertStudentByQuery"> insert into student (name,age) values (#{queryName},#{queryAge}) </insert>
Test.
@Test public void testGetStudentByQuery(){ QueryParam queryParam = new QueryParam(); queryParam.setQueryName("Zhang San"); queryParam.setQueryAge(37); List<Student> students = studentDao.getStudentByQuery(queryParam); students.forEach(student -> System.out.println(student)); } @Test public void testInsertStudentByQuery(){ QueryParam queryParam = new QueryParam(); queryParam.setQueryName("Lu Junyi"); queryParam.setQueryAge(35); int num = studentDao.insertStudentByQuery(queryParam); System.out.println(num); }
This discovery can also be found and added if:
How else can we find the property names of the classes passed in the interface and the pairs in the sql statement?
5. Pass multiple parameters by location
Old rules, interfaces, xml.
int updateStudentByPosition(Integer id,String email);
<update id="updateStudentByPosition"> update student set email = #{arg1} where id = #{arg0} </update>
Note that you have to write arg0, arg1 here. Writing args0 previously made a mistake.
Test method.
/** * Test whether the update method can operate */ @Test public void testUpdateStudentByPosition(){ int num = studentDao.updateStudentByPosition(1, "dad@qq.com"); }
6. map Transfer Parameters
It doesn't make any difference: interfaces, xml, tests.
List<Student> selectMultiMap(Map<String,Object> map);
<select id="selectMultiMap" resultType="com.xuangong.pojo.Student"> select id,name,email,age,money from student where id = #{id} or age = #{age} </select>
@Test public void testSelectMultiMap(){ Map<String,Object> map = new HashMap<>(); map.put("id",5); map.put("age",39); List<Student> students = studentDao.selectMultiMap(map); students.forEach(student -> System.out.println(student)); }
2. Encapsulate the output
1,resultType
This is the easiest way to do it. It can be of the following types:
(1) Simple types: such as counting out the int type
(2) Object type: such as output Student type
(3) Map type: However, when the result encapsulates a weiMap, only one record can be returned, no more than one value can be used, otherwise an error will be reported
The test returns Map, code: interface method, xml, test.
Map<String,Object> selectResultMap(@Param("id") int id);
<select id="selectResultMap" resultType="java.util.Map"> select id,name,email,age,money from student where id = #{id} </select>
/** * Return value is a Map test */ @Test public void testSelectResultMap(){ Map<String, Object> map = studentDao.selectResultMap(2); System.out.println(map); }
2,resultMap
This way, when the sql list name is inconsistent with the java entity class name, you can declare its value so that it can be mapped.
/** * Test the return value type of resultMap here * @param queryParam * @return */ List<Student> selectMyStudent1(QueryParam queryParam);
<!--test resultMap Type of--> <resultMap id="studentMap" type="com.xuangong.pojo.Student"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="email" property="email"/> <result column="age" property="age"/> <result column="money" property="money"/> </resultMap> <select id="selectMyStudent1" resultMap="studentMap"> select id,name,email,age from student where name = #{queryName} or age = #{queryAge} </select>
Here the primary key is id, the other columns are result, column is the column name of the database table, and property is the class name of the entity class so that they correspond to each other.
Save to resultMap
3. Solutions for inconsistencies between database column names and entity class names
(1) Aliasing
That is, rename it in sql, a little low, code: interface, xml, test.
/** * Test for inconsistencies between database table column names and entity class names */ List<Student1> selectStudent1ByAlias(QueryParam param);
<!--Query database tables using aliases to resolve name inconsistencies--> <select id="selectStudent1ByAlias" resultType="com.xuangong.pojo.Student1"> select id,name as userName,email,age as userAge from student where name = #{queryName} or age = #{queryAge} </select>
/** * Test for inconsistencies between database table column names and entity class names */ @Test public void testSelectStudent1ByAlias(){ QueryParam queryParam = new QueryParam(); queryParam.setQueryName("Wu Yong"); queryParam.setQueryAge(33); List<Student1> list = studentDao.selectStudent1ByAlias(queryParam); list.forEach(student1 -> System.out.println(student1)); }
(2) Set up using resultMap
This is where you use java code to implement mapping relationships, code: interfaces, xml, tests.
/** * Test for inconsistencies between database table column names and entity class names--using resultMap */ List<Student1> selectStudent1ByResultMap(QueryParam param);
<!--Use resultMap Ways to resolve name inconsistencies--> <resultMap id="student1Map" type="com.xuangong.pojo.Student1"> <id column="id" property="id"></id> <result column="name" property="userName"/> <result column="email" property="email"/> <result column="age" property="userAge"/> <result column="money" property="money"/> </resultMap> <select id="selectStudent1ByResultMap" resultMap="student1Map"> select id,name,email,age,money from student where name = #{queryName} or age = #{queryAge} </select>
/** * Test for inconsistencies between database table column names and entity class names--how resultmap works */ @Test public void testSelectStudent1ByResultMap(){ QueryParam queryParam = new QueryParam(); queryParam.setQueryName("King Five"); queryParam.setQueryAge(37); List<Student1> list = studentDao.selectStudent1ByResultMap(queryParam); list.forEach(student1 -> System.out.println(student1)); }
4. Fuzzy Query
(1) java code provides like
Interface, xml, test.
/** * Test Fuzzy Query--Add like to java */ List<Student> selectLikeFirst(String name);
<select id="selectLikeFirst" resultType="com.xuangong.pojo.Student"> select id,name,email,age,money from student where name like #{name} </select>
/** * Testing like fuzzy queries in java code */ @Test public void testSelectOneByJavaLike(){ List<Student> students = studentDao.selectLikeFirst("%river%"); students.forEach(student -> System.out.println(student)); }
(2) provide like in sql statements
Interface, xml, test.
/** * Test Fuzzy Query--Add like to sql */ List<Student> selectLikeSecond(String name);
<select id="selectLikeSecond" resultType="com.xuangong.pojo.Student"> select id,name,email,age,money from student where name like "%" #{name} "%" </select>
/** * Testing like fuzzy queries in java code */ @Test public void testSelectOneBySqlLike(){ List<Student> students = studentDao.selectLikeSecond("Lue"); students.forEach(student -> System.out.println(student)); }