MyBatis的resultMap
1.大家学习MyBatis时,可能会碰到实体类属性跟数据库字段不同的情况
如:数据库 ------ 实体类
stuname ----> name
即: 数据库中的stuname字段对应的事实体类里的name属性
如果这时,我们要用常规的查询方法时是不能正确查询到stuname的值的,它会显示为null
这时,我们可以使用我们的resultMap来解决这一问题。。。
源码介绍与对比:
1.Student.java (实体类)
package cn.zhang.entity; import java.util.Date; /**
* 学生实体类
*
*/
public class Student { private Integer stuno;
private String name;
private Integer stuage;
private Date studate; @Override
public String toString() {
return "Student [stuno=" + stuno + ", name=" + name + ", stuage="
+ stuage + ", studate=" + studate + "]";
} public Integer getStuno() {
return stuno;
} public void setStuno(Integer stuno) {
this.stuno = stuno;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getStuage() {
return stuage;
} public void setStuage(Integer stuage) {
this.stuage = stuage;
} public Date getStudate() {
return studate;
} public void setStudate(Date studate) {
this.studate = studate;
} }
2.mybatis-config.xml (MyBatis的配置文件)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 配置别名 -->
<typeAliases>
<!--方式一: 按类型名定制别名 -->
<typeAlias type="cn.zhang.entity.Student" alias="Student" />
<!--方式二: 拿当前指定包下的简单类名作为别名 -->
<!-- <package name="cn.zhang.entity"/> -->
</typeAliases>
<environments default="mysql">
<environment id="mysql">
<!-- 使用jdbc的事务 -->
<transactionManager type="JDBC" />
<!-- 使用自带的连接池 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/y2161" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="cn/zhang/dao/StudentDAO.xml" />
</mappers>
</configuration>
3.MybatisUtil.java (获得session的工具类)
package cn.zhang.util; import java.io.IOException;
import java.io.Reader; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; /**
* 获得session的工具类
*
*/
public class MybatisUtil { private static String config = "mybatis-config.xml";
static Reader reader;
static {
try {
reader = Resources.getResourceAsReader(config);
} catch (IOException e) {
e.printStackTrace();
}
}
private static SqlSessionFactory factory = new SqlSessionFactoryBuilder()
.build(reader); // 提供一个可以获取到session的方法
public static SqlSession getSession() throws IOException { SqlSession session = factory.openSession();
return session;
}
}
4.StudentDao.java (定义方法的接口)
package cn.zhang.dao; import java.io.IOException;
import java.util.List; import cn.zhang.entity.Student; public interface StudentDao { /**
* 查询所有记录
* @return
* @throws IOException
*/
public List<Student> findAll() throws IOException; }
5.StudentDaoImpl.java (实现接口方法的实现类)
package cn.zhang.dao.impl; import java.io.IOException;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import cn.zhang.dao.StudentDao;
import cn.zhang.entity.Student;
import cn.zhang.util.MybatisUtil; public class StudentDaoImpl implements StudentDao {
SqlSession session; public StudentDaoImpl() throws IOException {
session = MybatisUtil.getSession();
} /**
* 查询所有
*/
public java.util.List<Student> findAll() throws IOException {
List<Student> list = session.selectList("findAll");
session.close();
return list;
} }
6.StudentDAO.xml (对应的映射文件)
最常规的配法:
<?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">
<mapper namespace="cn.zhang.dao"> <!-- 查询所有 -->
<select id="findAll" resultType="Student">
select * from student
</select> </mapper>
用resultMap的配法:
<?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">
<mapper namespace="cn.zhang.dao"> <!-- 结果映射,指定了数据库和实体类中的对应值 -->
<resultMap type="Student" id="findstudent">
<result property="name" column="stuname" />
</resultMap> <!-- 查询所有 -->
<select id="findAll" resultMap="findstudent">
select * from student
</select> </mapper>
7.log4j.properties
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### set log levels - for more verbose logging change 'info' to 'debug' ### log4j.rootLogger=debug, stdout
8..MyTest.java (测试类)
package cn.zhang.test; import java.io.IOException;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import cn.zhang.dao.StudentDao;
import cn.zhang.dao.impl.StudentDaoImpl;
import cn.zhang.entity.Student; public class MyTest { StudentDao dao;
@Before
public void initData() throws IOException{
dao=new StudentDaoImpl();
} /**
* 查询所有学生
* @throws IOException
*/
@Test
public void findAll() throws IOException{
List<Student> list = dao.findAll();
for (Student student : list) {
System.out.println("编号: "+student.getStuno()+"姓名:"+student.getName());
} } }
对应StudentDAO.xml中常规配法的结果:
对应StudentDAO.xml中resultMap配法的结果:
这样就可以成功的拿到值了,这就是resultMap的作用。
是不是很简单,这里只是记录一下。。
MyBatis的resultMap的更多相关文章
- Mybatis的ResultMap的使用
本篇文章通过一个实际工作中遇到的例子开始吧: 工程使用Spring+Mybatis+Mysql开发.具体的业务逻辑很重,对象之间一层一层的嵌套.和数据库表对应的是大量的model类,而和前端交互的是V ...
- 使用MyBatis的resultMap高级查询时常用的方式总结
以下内容已经通过楼主测试, 从pd设计数据库到测试完成, 之前楼主也没有过Mybatis 使用resultMap觉得有点乱,最近抽出时间总结了一下也算对MyBatis的resultMap进行一次系统的 ...
- Mybatis的ResultMap的使用(转)
本篇文章通过一个实际工作中遇到的例子开始吧: 工程使用Spring+Mybatis+Mysql开发.具体的业务逻辑很重,对象之间一层一层的嵌套.和数据库表对应的是大量的model类,而和前端交互的是V ...
- 【MyBatis】ResultMap
[MyBatis]ResultMap 转载:https://www.cnblogs.com/yangchongxing/p/10486854.html 支持的 JDBC 类型为了未来的参考,MyBat ...
- 使用mybatis的resultMap进行复杂查询
记录下mybatis的集合查询中碰到的问题 https://jaychang.iteye.com/blog/2357143 MyBatis ofType和javaType区别 https: ...
- mybatis 使用resultMap实现关联数据的查询(association 和collection )
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "- ...
- mybatis 使用resultMap实现数据库的操作
resultType:直接表示返回类型 resultMap:对外部resultMap的引用 二者不能同时使用 创建一个实体类Role和User public class Role { private ...
- Mybatis之ResultMap一个简短的引论,关联对象
基础部分能够查看我的还有一篇博客http://blog.csdn.net/elim168/article/details/40622491 MyBatis中在查询进行select映射的时候.返回类型能 ...
- SSM框架开发web项目系列(三) MyBatis之resultMap及关联映射
前言 在上篇MyBatis基础篇中我们独立使用MyBatis构建了一个简单的数据库访问程序,可以实现单表的基本增删改查等操作,通过该实例我们可以初步了解MyBatis操作数据库需要的一些组成部分(配置 ...
随机推荐
- Servlet程序中玩验证码
验证码思想:所谓验证码就是产生若干随机数,存放到session中,然后在servlet中获取session中的该值与页面输入值相比较,进而判断正误. 产生验证码的方法: 随机数放在图片中,封装为一 ...
- JAVA理论概念大神之概念汇总
我个人觉得,JAVA之所以能够经久不衰,有一个很重要的原因就是:JAVA的理论总是给人一种,虽然不知道是什么,但是感觉很厉害的样子.就单是这一点,他就已经超越许多其他语言了,至少吹牛的时候谈资总是很多 ...
- iOS完整学习路线图
- Spring学习记录(七)---表达式语言-SpEL
SpEL---Spring Expression Language:是一个支持运行时查询和操作对象图表达式语言.使用#{...}作为定界符,为bean属性动态赋值提供了便利. ①对于普通的赋值,用Sp ...
- Clash Detection
Clash Detection eryar@163.com Abstract. Clash detection is used for the model collision check. The p ...
- lintcode 最长上升连续子序列 II(二维最长上升连续序列)
题目链接:http://www.lintcode.com/zh-cn/problem/longest-increasing-continuous-subsequence-ii/ 最长上升连续子序列 I ...
- 【原创】开源Math.NET基础数学类库使用(03)C#解析Matlab的mat格式
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 前言 ...
- iOS开发之窥探UICollectionViewController(一) -- Ready Your CollectionViewController
之前用CollectionViewController只是皮毛,一些iOS从入门到精通的书上也是泛泛而谈.这几天好好的搞了搞苹果的开发文档上CollectionViewController的内容,亲身 ...
- MySQL参数
1. sql_safe_updates 官方解释如下: , MySQL aborts . 默认为0,如果设置为1,则delete操作和update操作必须带有where子句,且where子句中的列必须 ...
- T-Sql(八)字段索引和数据加密
t-sql的基本用法讲到第八章也差不多了,最后就讲下字段索引和数据加密,这两个内容对编程人员可能用的地方不是太多,还是那句老话“防患于未然”. 下面我就简单的说下字段索引和数据加密的内容,只是简单概述 ...