An example of resultMap
and resultType for getting started with
Mybatis In the case of entity classes resultType: suitable for use The return value data type is non-custom, that is, the type provided by jdk resultMap: the unique identifier of the data type of the mapped entity class resultMap column: the field name of the library table property: the attribute name in the entity class Configuration map file: <?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: The namespace of the current library table mapping file, the only one cannot be repeated --> <mapper namespace="com.hao947.sql.mapper.PersonMapper"> <!-- type: The data type id of the mapped entity class: the unique identifier of the resultMap -->
<resultMap type="person" id="BaseResultMap">
<!-- column: the field name of the library table property: the attribute name in the entity class-->
<id column="person_id" property="personId" />
< result column="name" property="name" />
<result column="gender" property="gender" />
<result column="person_addr" property="personAddr" />
<result column="birthday" property= "birthday" />
</resultMap>
<!--id: the unique identifier of the current sql
parameterType: the data type of the input parameter
resultType: the data type of the return value
#{}: used to accept parameters, if a parameter is passed# The content of {id} is arbitrary. If there are multiple parameters, there are certain rules. The precompiled form is selected
* from person p where p. id = ? , high security -->
<!-- The return value type of the sql statement uses resultMap -->
<select id="selectPersonById" parameterType="java.lang.Integer"
resultMap="BaseResultMap">
select * from person p where p.person_id = #{id}
</select>
<!-- resultMap: suitable for use when the return value is a custom entity class
resultType: suitable data type for the return value It is non-customized, that is, the type provided by jdk-->
<select id="selectPersonCount" resultType="java.lang.Integer">
select count(*) from
person
</select>
<select id="selectPersonByIdWithMap" parameterType ="java.lang.Integer"
resultType="java.util.Map">
select * from person p where p.person_id= #{id}
</select>
</mapper>
Entity class Person.java
<pre name=" code" class="java">package com.hao947. model;
import java.util.Date;
public class Person {
private Integer personId;
private String name;
private Integer gender;
private String personAddr;
private Date birthday;
@Override
public String toString() {
return "Person [personId=" + personId + ", name=" + name + ", gender="
+ gender + ", personAddr=" + personAddr + ", birthday="
+ birthday + "]";
}
}
测试类
public class PersonTest {
SqlSessionFactory sqlSessionFactory;
@Before
public void setUp() throws Exception {
// 读取资源流
InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
// Initialize session factory
sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
}
@Test
public void selectPersonById() {
// 创建一个sqlsession
SqlSession session = sqlSessionFactory.openSession();
try {
Person p = session.selectOne(
"com.hao947.sql.mapper.PersonMapper.selectPersonById", 1);
System.out.println(p);
} finally {
session.close();
}
}
@Test
public void selectPersonCount() {
// 创建一个sqlsession
SqlSession session = sqlSessionFactory.openSession();
try {
Integer p = session.selectOne(
"com.hao947.sql.mapper.PersonMapper.selectPersonCount");
System.out.println(p);
} finally {
session.close();
}
}
@Test
public void selectPersonByIdWithMap() {
// 创建一个sqlsession
SqlSession session = sqlSessionFactory.openSession();
try {
Map<String ,Object> map = session.selectOne(
"com.hao947.sql.mapper.PersonMapper.selectPersonByIdWithMap",1);
System.out.println(map);
} finally {
session.close();
}
}
}
An example of resultMap and resultType for getting started with Mybatis
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=326939055&siteId=291194637
Recommended
Ranking