mybatis date类型映射_MyBatis 映射入门

一、 简介

        MyBatis 的真正强大在于它的语句映射,这是它的魔力所在。resultMap 元素可以让你从 JDBC 通过ResultSets 提取数据中解放出来,并在一些情形下允许你进行一些 JDBC 不支持的操作。他对简单的语句可以做到零配置,对于复杂一些的语句,只需要描述清楚语句之间的关系。

二、ResultMap 基础信息

ResultMap 的属性列表:

属性描述
id当前命名空间中的一个唯一标识,用于标识一个结果映射。
type类的完全限定名, 或者一个类型别名(关于内置的类型别名,可以参考上面的表格)。
autoMapping如果设置这个属性,MyBatis 将会为本结果映射开启或者关闭自动映射。这个属性会覆盖全局的属性 autoMappingBehavior。默认值:未设置(unset)。
extends继承关系

ResultMap 的子元素列表:

属性描述
constructor用于在实例化类时,注入结果到构造方法中
id一个 ID 结果;标记出作为 ID 的结果可以帮助提高整体性能
result注入到字段或 JavaBean 属性的普通结果
association嵌套结果映射 – 关联可以是 resultMap 元素,或是对其它结果映射的引用
collection嵌套结果映射 – 集合可以是 resultMap 元素,或是对其它结果映射的引用
discriminator使用结果值来决定使用哪个 resultMap

备注:idresult 元素都将一个列的值映射到一个简单数据类型(String, int, double, Date 等)的属性或字段。这两者之间的唯一不同是,id 元素对应的属性会被标记为对象的标识符,在比较对象实例时使用。这样可以提高整体的性能,尤其是进行缓存和嵌套结果映射(也就是连接映射)的时候。

MyBatis 的自动映射级别:

属性描述
FULL自动匹配所有
PARTIAL(默认)自动匹配所有属性,有内部嵌套(association,collection)的除外
NONE禁止自动匹配
<settings>        <setting name="autoMappingBehavior" value="PARTIAL" />settings>

三、Mybatis 简单映射

1、一个最简单的XML文件。基于 JavaBean 的规范,id, title, body 这些属性会对应到 select 语句中的列名。这样的一个 JavaBean 可以被映射到 ResultSet,就像映射到 HashMap 一样简单。

<select id="queryById" resultType="com.ycdhz.mybatis.entity.Blog">    select id, title, body from blog where id = #{id}select>

2、我们可以通过配置mybatis-config中的typeAlias 属性来简化类名。比如:

<typeAliases>        <typeAlias type="com.ycdhz.mybatis.entity.Blog" alias="blog"/>        <package name="com.ycdhz.mybatis.entity"/>typeAliases>
<select id="queryById" resultType="blog">    select id, title, body from blog where id = #{id}select>

3、如果列名和属性名不能匹配上,可以在 SELECT 语句中设置列别名(这是一个基本的 SQL 特性)来完成匹配。

<select id="queryById" resultType="blog">    select id, title as "title", body as "body" from blog where id = #{id}select>

4、在使用resultType 来绑定映射的情况下,MyBatis 会在幕后自动创建一个 ResultMap,再根据属性名来映射列到 JavaBean 的属性上,完全可以不用显式地配置它们。下面我们通过显示配置来看下ResultMap,同时这也是解决列名不匹配的另外一种方式。

<resultMap id="BlogMap" type="com.ycdhz.mybatis.entity.Blog">    <id property="id" column="id" />    <result property="id" column="id" jdbcType="INTEGER"/>    <result property="title" column="title" jdbcType="VARCHAR"/>    <result property="body" column="body" jdbcType="VARCHAR"/>resultMap><select id="queryById" resultMap="BlogMap">     select id, title, body from blog where id = #{id}select>

四、Mybatis 嵌套结果映射

1、构造方法

        通过修改对象属性的方式,可以满足大多数的数据传输对象(Data Transfer Object, DTO)以及绝大部分领域模型的要求。但有些情况下你想使用不可变类。构造方法注入允许你在初始化时为类设置属性的值,而不用暴露出公有方法。

public User(Integer id, String name, String age){    this.id = id;    this.name = name;    this.age = age;}
<resultMap type="com.ycdhz.mybatis.entity.User" id="UserMap">    <constructor>        <idArg column="id" javaType="int" />        <arg column="name" javaType="String"/>        <arg column="age"  javaType="String"/>    constructor>resultMap>

2、association 关联

方式一:使用单表查询+association引用select方式

b

方式二:使用内连接+级联属性:


select b.*, u.name as name, u.age as age from blog b INNER JOIN user u
on b.author_id = u.id where b.id = #{id}

方式三:使用内联方式直接列出


select * from blog b INNER JOIN user u
on b.author_id = u.id where b.id = #{id}

方式四:使用resultMap引用


select * from blog b INNER JOIN user u
on b.author_id = u.id where b.id = #{id}

测试代码

创建两个实体对象

public class Blog implements Serializable {
private static final long serialVersionUID = 317875403149589016L;
private Integer id;
private String title;
private String body;
private String authorId;
private User author;
}
public class User implements Serializable {
private static final long serialVersionUID = 913223077425513414L;
private Integer id;
private String name;
private String age;
private String sex;
private String email;
private String phoneNumber;
private Date createTime;
}

创建对应的xml文件


select
id, title, body, author_id
from blog
where id = #{id}
u
select
id, name, age, sex, email, phone_number, create_time
from blog.user
where id = #{id}

创建测试类

package com.ycdhz.mybatis;
import com.ycdhz.mybatis.entity.Blog;
import com.ycdhz.mybatis.entity.User;
import org.apache.ibatis.session.*;
import org.apache.ibatis.transaction.jdbc.JdbcTransaction;
import org.junit.Before;
import org.junit.Test;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
public class ResultMapperTest {
private Configuration configuration;
private JdbcTransaction jdbcTransaction;
private MappedStatement mappedStatement;
private SqlSessionFactory factory;
@Before
public void init() throws SQLException {
SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
factory = factoryBuilder.build(ResultMapperTest.class.getResourceAsStream("/mybatis-config.xml"));
configuration = factory.getConfiguration();
jdbcTransaction = new JdbcTransaction(factory.openSession().getConnection());
}
@Test
public void resultTest() throws SQLException {
SqlSession sqlSession = factory.openSession();
List blogs = sqlSession.selectList("com.ycdhz.mybatis.dao.BlogDao.queryById", 1);
System.out.println(blogs.size());
}
}

3、collection 集合

方式一:使用单表查询+collection引用select方式

方式二:使用内联方式直接列出


select b.*, c.body as bodyMsg from blog b
left JOIN `comment` c
on b.id = c.blog_id where b.id = #{id}

测试代码

修改Blog实体对象,新增Comment对象

public class Blog implements Serializable {
private static final long serialVersionUID = 317875403149589016L;
private Integer id;
private String title;
private String body;
private List comments;
}
public class Comment implements Serializable {
private static final long serialVersionUID = 284643648411741678L;
private String id;
private Integer blogId;
private String body;
private Blog blog;
}

新增Comment的配置文件


select * from blog.comment where blog_id = #{id}

五、总结

9bdde98f01505944eda12a6093de4096.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值