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();
- }
- }
- }
Mybatis 入门之resultMap与resultType讲解实例的更多相关文章
- Mybatis 入门之resultMap与resultType解说实例
resultMap:适合使用返回值是自己定义实体类的情况 resultType:适合使用返回值得数据类型是非自己定义的,即jdk的提供的类型 resultMap : type:映射实体类的数据类型 i ...
- mybatis中的resultMap与resultType、parameterMap与 parameterType的区别
Map:映射:Type:Java类型 resultMap 与 resultType.parameterMap 与 parameterType的区别在面试的时候被问到的几率非常高,项目中出现了一个小b ...
- Mybatis使用时 resultMap与resultType、parameterMap与 parameterType的区别
Map:映射:Type:Java类型 resultMap 与 resultType.parameterMap 与 parameterType的区别在面试的时候被问到的几率非常高,出现的次数到了令人 ...
- MyBatis学习总结(13)——Mybatis查询之resultMap和resultType区别
MyBatis的每一个查询映射的返回类型都是ResultMap,只是当我们提供的返回类型属性是resultType的时候,MyBatis对自动的给我们把对应的值赋给resultType所指定对象的属性 ...
- mybatis入门,基本案例和xml讲解
mybatis入门 先举一个入门案例 1)创建一个mybatis-day01这么一个javaweb工程或java工程 2)导入mybatis和mysql/oracle的jar包到/WEB-INF/li ...
- Mybatis第七篇【resultMap、resultType、延迟加载】
resultMap 有的时候,我们看别的映射文件,可能看不到以下这么一段代码: <resultMap id="userListResultMap" type="us ...
- mybatis的resultMap与resultType的区别
一.概述MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部Res ...
- Mybatis中resultMap与resultType区别
MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultM ...
- Mybatis实现一对一查询 对ResultType和ResultMap分析
实现一对一查询: ResultMap:使用ResultType实现较为简单,如果pojo中没有包括查询出来的列名,需要增加 列名对应的属性,即可完成映射. 如果没有查询结果的特殊要求建议使用Resul ...
随机推荐
- int preg_match( string pattern
preg_match -- 进行正则表达式匹配.并且只匹配一次,注意与preg_match_all区别. int preg_match( string pattern, string subject ...
- javascript中的location的用法
javascript中的location.href有很多种用法,主要如下. self.location.href="/url" 当前页面打开URL页面 location.href= ...
- canvas实现粒子星空连线
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>离 ...
- MySQL-慢日志slow log
文件结构 1.目录结构 drwxrwxr-x mysql mysql Mar bin drwxrwxr-x mysql mysql Dec : binlogdir -rw-r--r-- mysql m ...
- 01 语言基础+高级:1-2 面向对象和封装_day06【类与对象、封装、构造方法】
day06[类与对象.封装.构造方法] 面向对象类与对象三大特征——封装构造方法 能够理解面向对象的思想能够明确类与对象关系能够掌握类的定义格式能够掌握创建对象格式,并访问类中的成员能够完成手机类的练 ...
- PAT Advanced 1052 Linked List Sorting (25) [链表]
题目 A linked list consists of a series of structures, which are not necessarily adjacent in memory. W ...
- JavaEE--调用 WSDL -- httpclient 4.x.x
参考:http://aperise.iteye.com/blog/2223454 http://blog.csdn.net/chenleixing/article/details/43456987 ...
- 2017年3月25日工作日志:Jquery使用小结[绑定事件判断、select标签、军官证正则]
jQuery获取DOM绑定事件 在1.8.0版本之前,我们要想获取某个DOM绑定的事件处理程序可以这样: $.data(domObj,'events');//或者$('selector').data( ...
- list交集、差集、并集、去重并集
// 交集 List<String> intersection = list1.stream().filter(item -> list2.contains(item)).colle ...
- 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第六天】
https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...