Mybatis 入门之resultMap与resultType讲解实例
resultMap:适合使用返回值是自定义实体类的情况
resultType:适合使用返回值得数据类型是非自定义的,即jdk的提供的类型
resultMap :
type:映射实体类的数据类型
id:resultMap的唯一标识
column:库表的字段名
property:实体类里的属性名
配置映射文件:
- <?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:当前库表映射文件的命名空间,唯一的不能重复 -->
- <mapper namespace="com.hao947.sql.mapper.PersonMapper">
- <!-- type:映射实体类的数据类型 id:resultMap的唯一标识 -->
- <resultMap type="person" id="BaseResultMap">
- <!-- column:库表的字段名 property:实体类里的属性名 -->
- <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:当前sql的唯一标识
- parameterType:输入参数的数据类型
- resultType:返回值的数据类型
- #{}:用来接受参数的,如果是传递一个参数#{id}内容任意,如果是多个参数就有一定的规则,采用的是预编译的形式select
- * from person p where p.id = ? ,安全性很高 -->
- <!-- sql语句返回值类型使用resultMap -->
- <select id="selectPersonById" parameterType="java.lang.Integer"
- resultMap="BaseResultMap">
- select * from person p where p.person_id = #{id}
- </select>
- <!-- resultMap:适合使用返回值是自定义实体类的情况
- resultType:适合使用返回值的数据类型是非自定义的,即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>
实体类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");
- // 初始化session工厂
- 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();
- }
- }
- }