MyBatis是一款优秀的持久层框架,同样也是做OR Mapping的。与JPA不同,MyBatis里面需要我们自己来定制sql。

MyBatis和JPA的选择

其实如果业务比较操作比较简单使用JPA加hibernate还是比较方便的。但是如果业务复杂即sql映射也复杂这个时候还是使用mybatis比较方便。另外在bat更多的使用是mybatis,这样dba对所写的业务sql能够有更强的把控。

SpringBoot中使用MyBatis

添加依赖

        <dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.4</version>
</dependency>

简单配置

  • mybatis.mapper-locations:表示mapper的xml文件位置
  • mybatis.type-aliases-package:类型别名的包名。这个配置是为了配置mapper的xml文件中resultType返回值的包的位置,如果未配置需要使用全包名
  • mybatis.type-handlers-package:typeHandlers就是用来完成javaType和jdbcType之间的转换,举个比较简单的例子,我创建一个博客表,表中的创建时间和修改时间用VARCHAR类型,但是在我的POJO对象中,创建时间和修改时间的类型是Date,这样我在向数据库插入数据时,需要将日期类型转化成VARCHAR,而从数据库中查询出的结果中,又需要将VARCHAR类型转换成Date。MyBatis中的类型处理器也存在系统定义的和自定义两种,MyBatis会根据javaType和jdbcType来决定采用哪个typeHandler来处理这些转换规则,而且系统定义的能满足大部分需求,可以说是非常好用,用户只需要自定义一些特有的转换规则,如枚举类型。
  • mybatis.configuration.map-underscore-to-camel-case:下划线与驼峰命名规则的对应关系
  • mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl:控制台打印MyBatis执行sql

Mapper的定义与扫描

MyBatis中映射有两种 一种是使用xml一种是使用注解(两者的选择,对于简单的场景使用注解比较方便;但是对于复杂的查询业务还是使用xml的方式比较方便)。另外使用@MapperScan配置扫描Mapper文件的位置,使用@Mapper注解标识Mapper文件。如下代码是使用注解的使用定义的Mapper文件的映射,并使用@Mapper注解标识是Mapper文件。

/**
* Created by zhangdd on 2020/7/29
*/
@Mapper
public interface CoffeeMapper {
@Insert("insert into t_coffee (name, price, create_time, update_time)"
+ "values (#{name}, #{price}, now(), now())")
@Options(useGeneratedKeys = true)
Long save(Coffee coffee); @Select("select * from t_coffee where id = #{id}")
@Results({
@Result(id = true, column = "id", property = "id"),
@Result(column = "create_time", property = "createTime"),
// map-underscore-to-camel-case = true 可以实现一样的效果
// @Result(column = "update_time", property = "updateTime"),
})
Coffee findById(@Param("id") Long id);
}

调用Mapper方法执行sql

package com.lucky.spring;

import com.lucky.spring.mapper.CoffeeMapper;
import com.lucky.spring.model.Coffee;
import lombok.extern.slf4j.Slf4j;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
@MapperScan("com.lucky.spring.mapper")
@Slf4j
public class Application implements CommandLineRunner { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} @Autowired
private CoffeeMapper coffeeMapper; @Override
public void run(String... args) throws Exception {
Coffee c = Coffee.builder().name("espresso")
.price(Money.of(CurrencyUnit.of("CNY"), 20.0)).build();
Long id = coffeeMapper.save(c);
log.info("Coffee {} => {}", id, c); c = coffeeMapper.findById(id);
log.info("Coffee {}", c);
}
}

打印结果如下

2020-07-29 22:20:53.251  INFO 78971 --- [           main] com.lucky.spring.Application             : Coffee 1 => Coffee(id=5, name=espresso, price=CNY 20.00, createTime=null, updateTime=null)
2020-07-29 22:20:53.265 INFO 78971 --- [ main] com.lucky.spring.Application : Coffee Coffee(id=1, name=espresso, price=CNY 20.00, createTime=Wed Jul 29 22:07:31 CST 2020, updateTime=Wed Jul 29 22:07:31 CST 2020)

可以看到有时候插入数据后,我们需要这个数据的ID;同时查询数据的时间格式不一定是我们所需要的。这些场景如何结局呢?如下:

时间返回格式 和 显示null值字段

#所有日期格式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
# 所有字段都不过滤 null值也显示
spring.jackson.default-property-inclusion=always

获取插入数据的自增ID

使用注解的映射Mapper文件

使用@Options注解的useGeneratedKeys属性。useGeneratedKeys设为true是表示使用jdbc的getGeneratedKeys方法来取出数据的主键值(默认为false)

使用xml映射

同样也是使用 useGeneratedKeys 设置为true,keyProperty标记返回的主键值传递给某个具体的属性

<insert id="add2" parameterType="employee"  useGeneratedKeys="true" keyProperty="id">
insert into employee(username) values(#{username})
</insert>

最新文章

  1. window系统下,简单的FTP上传和下载操作
  2. Linux CentOS安装postgresql 9.4
  3. 给groupBox添加滚动条
  4. SSH乱码解决
  5. matlab生成HEX文件-任意信号 大于64K长度
  6. 模拟创建类变量,static变量加类方法,单例
  7. 安装python-devel 在升级到python2.7之后
  8. 一起学习 微服务(MicroServices)-笔记
  9. linux中less命令使用
  10. HDU 2993 MAX Average Problem dp斜率优化
  11. HTML5 总结-表单-输入类型
  12. 解决sqlite删除数据后,文件大小不变问题(VACUUM)
  13. JavaScript随记汇总
  14. MySQL的char和varchar
  15. php单例连接数据库
  16. SMB协议利用之ms17-010-永恒之蓝漏洞抓包分析SMB协议
  17. 20175312 2018-2019-2 《Java程序设计》第1周学习总结
  18. nodejs 环境配置技巧
  19. Linux后台进程管理以及ctrl+z(挂起)、ctrl+c(中断)、ctrl+\(退出)和ctrl+d(EOF)的区别(转)
  20. 蓝桥杯—ALGO-122 未名湖畔的烦恼(枚举)

热门文章

  1. PLSQL导入dmp文件完整步骤(附图)
  2. Spring-Validation(数据校验) 你值得拥有
  3. Beautiful Soup 4 方法便捷查询
  4. 02 flask源码剖析之flask快速使用
  5. linux专题(二):走近Linux系统 (2020-04-08 10:08)
  6. Java设计模式 --- 七大常用设计模式示例归纳
  7. 30页软件测试人面试宝典文档资料,助你拿下了百度、美团、字节跳动、小米等大厂的offer【内含答案】
  8. Burp Suite Repeater Module - 中继模块
  9. Python Ethical Hacking - WEB PENETRATION TESTING(4)
  10. 记录一次升级ant-design-vue的遇见的bug