Mybatis 入门之resultMap与resultType讲解实例
来源:互联网 发布:极简php网盘源码 编辑:程序博客网 时间:2024/06/05 03:20
resultMap:适合使用返回值是自定义实体类的情况
resultType:适合使用返回值得数据类型是非自定义的,即jdk的提供的类型
resultMap :
type:映射实体类的数据类型
id:resultMap的唯一标识
column:库表的字段名
property:实体类里的属性名
配置映射文件:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//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(*) fromperson</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;@Overridepublic String toString() {return "Person [personId=" + personId + ", name=" + name + ", gender="+ gender + ", personAddr=" + personAddr + ", birthday="+ birthday + "]";}}
测试类
public class PersonTest {SqlSessionFactory sqlSessionFactory;@Beforepublic void setUp() throws Exception {// 读取资源流InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");// 初始化session工厂sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);}@Testpublic void selectPersonById() {// 创建一个sqlsessionSqlSession session = sqlSessionFactory.openSession();try {Person p = session.selectOne("com.hao947.sql.mapper.PersonMapper.selectPersonById", 1);System.out.println(p);} finally {session.close();}}@Testpublic void selectPersonCount() {// 创建一个sqlsessionSqlSession session = sqlSessionFactory.openSession();try {Integer p = session.selectOne("com.hao947.sql.mapper.PersonMapper.selectPersonCount");System.out.println(p);} finally {session.close();}}@Testpublic void selectPersonByIdWithMap() {// 创建一个sqlsessionSqlSession session = sqlSessionFactory.openSession();try {Map<String ,Object> map = session.selectOne("com.hao947.sql.mapper.PersonMapper.selectPersonByIdWithMap",1);System.out.println(map);} finally {session.close();}}}
0 0
- Mybatis 入门之resultMap与resultType讲解实例
- Mybatis 入门之resultMap与resultType讲解实例
- Mybatis 入门之resultMap与resultType讲解实例
- Mybatis 入门之resultMap与resultType讲解实例
- Mybatis 入门之resultMap与resultType讲解实例
- Mybatis 入门之resultMap与resultType讲解实例
- Mybatis 入门之resultMap与resultType讲解实例
- MyBatis中 resultType与resultMap
- Mybatis中的resultType与resultMap
- mybatis Resultmap 与 ResultType 区别
- MyBatis之输入(parameterType)与输出(resultType、resultMap)映射
- 【Mybatis框架】输出映射-resultType与resultMap
- mybatis 中resultType与resultMap区别
- 【Mybatis框架】输出映射-resultType与resultMap
- mybatis中的resultMap与resultType的区别
- 【Mybatis框架】输出映射-resultType与resultMap
- 【Mybatis框架】输出映射-resultType与resultMap
- mybatis 中resulttype 与resultmap用法
- 算法->判断单链表环的入口
- 第一章 基础知识
- linux 打开文件数 too many open files 解决方法
- NSString在内存管理中的怪象
- N!的末尾有几个零以及二进制表示最右边1的位置
- Mybatis 入门之resultMap与resultType讲解实例
- arduino引脚图
- 你看懂了程序猿的这些笑话吗?
- 谨在此简单地记录下,心酸,激动
- maven的初步使用--Eclipse android maven的项目创建
- How to Setup Cordova for Windows 7
- Jersey中处理POST格式的Best Practice
- LeetCode刷题笔录 Remove Duplicates From Sorted Array
- Java NIO框架Mina、Netty、Grizzly介绍