博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Mybatis 入门之resultMap与resultType解说实例
阅读量:4603 次
发布时间:2019-06-09

本文共 3069 字,大约阅读时间需要 10 分钟。

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

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
map = session.selectOne( "com.hao947.sql.mapper.PersonMapper.selectPersonByIdWithMap",1); System.out.println(map); } finally { session.close(); } }}

转载于:https://www.cnblogs.com/zsychanpin/p/6858565.html

你可能感兴趣的文章
JS秒数转成分秒时间格式
查看>>
xp_cmdshell 命令的开启与关闭,和状态查询
查看>>
Linux sudoers
查看>>
MySQL详解(18)-----------分页方法总结
查看>>
bzoj 4595 激光发生器
查看>>
multi cookie & read bug
查看>>
js时间转换
查看>>
(转载) Android Studio你不知道的调试技巧
查看>>
POJ2231 Moo Volume 递推 C语言
查看>>
struts2类型转换的具体流程
查看>>
Hdu 1203 I NEED A OFFER!
查看>>
php文件上传类
查看>>
CF219D Choosing Capital for Treeland
查看>>
luogu P3809 【模板】后缀排序
查看>>
JVM 调优工具
查看>>
SCTF 2014 pwn题目分析
查看>>
集合以及特殊集合
查看>>
USACO 2.2 Runaround Numbers
查看>>
Matlab画图-非常具体,非常全面
查看>>
365. Water and Jug Problem
查看>>