Mybatis 入门之resultMap与resultType讲解实例
来源:互联网 发布:数据录入员累不累 编辑:程序博客网 时间:2024/06/05 15:47
resultMap:适合使用返回值是自定义实体类的情况
resultType:适合使用返回值得数据类型是非自定义的,即jdk的提供的类型
resultMap :
映射实体类的数据类型
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(*) 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(); } }}
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用法
- SQL Server数据库中bit字段类型使用时的注意事项
- 手机端页面遮罩的居中基准
- Android IPC机制(三)在Android Studio中使用AIDL实现跨进程方法调用
- Access restriction : The type BASE64Decoder is not accessible due to restriction
- 兴趣与擅长之间
- Mybatis 入门之resultMap与resultType讲解实例
- 如何利用socket进行HTTP访问
- HDOJ 1201-18岁生日
- dbhome
- IOS8 设置TableView Separatorinset 分割线从边框顶端开始
- 常用的Linux网络配置命令
- QT学习笔记(四):Http下载的另一种实现方式,使用QNetworkAccessManager
- 在应用中更新App版本
- 数据库性能优化之SQL语句优化