一.本文介绍

首先读这篇文章之前如果没有接触过Spring Boot可以看一下之前的文章,并且读这篇文章还需要你至少能写基本的sql语句。我在写这篇文章之前也想过到底是选择JPA还是Mybaties作为持久层框架(持久层框架我理解就是替你去数据库执行操作并把查询结果处理好了再返给你),JPA底层封装了Hibernate,所以JPA和Mybaties的比较实际就是Hibernate和Mybaties的比较,让我最后选择Mybaties的理由就是Hibernate主要是个全自动的持久层框架而Mybaties是半自动的持久层框架,如果用Hibernate可能不需要自己写sql因为它的底层都给你封装好了,而Mybaties需要自己写好sql框架会替你执行并把查询结果按你的要求处理好返回给你,这样来看实际Mybaties具有更好的灵活性。

二.Spring Boot整合Mybaties

首先建一个数据库例子脚本如下

CREATE TABLE `user_tag_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`tag` int(11) NOT NULL DEFAULT '' COMMENT '计时标签',
`wx_id` varchar(11) NOT NULL DEFAULT '' COMMENT '微信唯一标识',
`start_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP COMMENT '计时开始时间',
`end_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP COMMENT '计时结束时间',
`status` int(11) NOT NULL COMMENT '0:计时未结束 1: 计时结束',
`current_data` date NOT NULL COMMENT '记录时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

pom.xml增添如下依赖

 <!-- Spring Boot与Mybaties整合需要引入的依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency> <!-- 数据库驱动需要引入的依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency> <!-- 数据库连接池需要引入的依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.0</version>
</dependency>

在resources目录下新建application.properties文件,内容如下:

#指定数据库驱动
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#指定数据库url(其中localhost可以换成指定ip)
spring.datasource.url=jdbc:mysql://localhost:3306/demo
#数据库账号密码
spring.datasource.username=root
spring.datasource.password=root

建立如下图的项目结构

根据表建立对应实体并放入entity包中如下

package cn.test.entity;

import java.util.Date;

public class UserTagInfo{
private Integer id; /**
* 计时标签
*/
private Integer tag; /**
* 微信唯一标识
*/
private String wxId; /**
* 计时开始时间
*/
private Date startTime; /**
* 计时结束时间
*/
private Date endTime; /**
* 0:计时未结束 1: 计时结束
*/
private Integer status; /**
* 记录时间
*/
private Date currentData; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public Integer getTag() {
return tag;
} public void setTag(Integer tag) {
this.tag = tag;
} public String getWxId() {
return wxId;
} public void setWxId(String wxId) {
this.wxId = wxId == null ? null : wxId.trim();
} public Date getStartTime() {
return startTime;
} public void setStartTime(Date startTime) {
this.startTime = startTime;
} public Date getEndTime() {
return endTime;
} public void setEndTime(Date endTime) {
this.endTime = endTime;
} public Integer getStatus() {
return status;
} public void setStatus(Integer status) {
this.status = status;
} public Date getCurrentData() {
return currentData;
} public void setCurrentData(Date currentData) {
this.currentData = currentData;
} @Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", tag=").append(tag);
sb.append(", wxId=").append(wxId);
sb.append(", startTime=").append(startTime);
sb.append(", endTime=").append(endTime);
sb.append(", status=").append(status);
sb.append(", currentData=").append(currentData);
sb.append("]");
return sb.toString();
}
}

在cn.test.mapper下新建一个接口如下

package cn.test.mapper;

import cn.test.entity.UserTagInfo;
import org.springframework.stereotype.Component; @Component
public interface UserTagInfoMapper { int deleteByPrimaryKey(Integer id); int insert(UserTagInfo record); UserTagInfo selectByPrimaryKey(Integer id); int updateByPrimaryKey(UserTagInfo record);
}

在resources下的mapper文件夹里新建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.test.mapper.UserTagInfoMapper" >
<resultMap id="BaseResultMap" type="cn.test.entity.UserTagInfo" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="tag" property="tag" jdbcType="INTEGER" />
<result column="wx_id" property="wxId" jdbcType="VARCHAR" />
<result column="start_time" property="startTime" jdbcType="TIMESTAMP" />
<result column="end_time" property="endTime" jdbcType="TIMESTAMP" />
<result column="status" property="status" jdbcType="INTEGER" />
<result column="current_data" property="currentData" jdbcType="DATE" />
</resultMap> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
id,tag,wx_id,start_time,end_time,status,current_data
from user_tag_info
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from user_tag_info
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="cn.test.entity.UserTagInfo" >
insert into user_tag_info (id, tag, wx_id,
start_time, end_time, status,
current_data)
values (#{id,jdbcType=INTEGER}, #{tag,jdbcType=INTEGER}, #{wxId,jdbcType=VARCHAR},
#{startTime,jdbcType=TIMESTAMP}, #{endTime,jdbcType=TIMESTAMP}, #{status,jdbcType=INTEGER},
#{currentData,jdbcType=DATE})
</insert>
<update id="updateByPrimaryKey" parameterType="cn.test.entity.UserTagInfo" >
update user_tag_info
set tag = #{tag,jdbcType=INTEGER},
wx_id = #{wxId,jdbcType=VARCHAR},
start_time = #{startTime,jdbcType=TIMESTAMP},
end_time = #{endTime,jdbcType=TIMESTAMP},
status = #{status,jdbcType=INTEGER},
current_data = #{currentData,jdbcType=DATE}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>

这里有4点要说一下

1.首先@Component注解的作用是:Mybaties会为加了这个注解的接口生成一个代理对象(接口本身不能new出对象来)执行你的sql并按你的定义返回查询结果,这就要求每个接口最好有一个同名的xml配置文件,也可以看到xml文件里的<mapper namespace="cn.test.mapper.UserTagInfoMapper" >也是这个作用

2.简单说一下xml里的标签的含义

resultMap代表对返回结果的封装,id属性必须唯一,type对应具体的实体,它的子标签id代表数据库中的主键,result代表普通的属性,column代表数据库的列名,property代表实体中的属性名;select、delete、insert、update分别对应查询、删除、插入、修改的sql,这里面的id对用的是接口中的方法名,parameterType代表入参类型

3.在DemoApplication启动类上增加如下注解,否则会报错找不到对应的mapper文件

package com.daojia;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
@MapperScan("com.daojia.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Description:
Field userTagInfoMapper in cn.test.service.impl.UserTagInfoServiceImpl required a bean of type 'cn.test.mapper.UserTagInfoMapper' that could not be found.
Action:
Consider defining a bean of type 'cn.test.mapper.UserTagInfoMapper' in your configuration.

在cn.test.service下新建业务处理接口类如下

package cn.test.service;

import cn.test.entity.UserTagInfo;

public interface UserTagInfoService {

     int insert(UserTagInfo userTagInfo);

     UserTagInfo select(Integer id);

     int update(UserTagInfo userTagInfo);

     int delete(Integer id);
}

在cn.test.service.impl下新建业务处理接口实现类,其中需要利用@Autowired注入对用的Mapper如下

package cn.test.service.impl;

import cn.test.entity.UserTagInfo;
import cn.test.mapper.UserTagInfoMapper;
import cn.test.service.UserTagInfoService;
import org.springframework.beans.factory.annotation.Autowired; public class UserTagInfoServiceImpl implements UserTagInfoService { @Autowired
private UserTagInfoMapper userTagInfoMapper; @Override
public int insert(UserTagInfo userTagInfo) {
userTagInfo.setCurrentData(new Date());
return userTagInfoMapper.insert(userTagInfo);
} @Override
public UserTagInfo select(Integer id) {
return userTagInfoMapper.selectByPrimaryKey(id);
} @Override
public int update(UserTagInfo userTagInfo) {
return userTagInfoMapper.updateByPrimaryKey(userTagInfo);
} @Override
public int delete(Integer id) {
return userTagInfoMapper.deleteByPrimaryKey(id);
}
}

在cn.test.controllers下新建调用业务层的controller如下

package cn.test.controllers;

import cn.test.entity.UserTagInfo;
import cn.test.service.UserTagInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/usertag")
public class UserTagInfoController { @Autowired
private UserTagInfoService userTagInfoService; @RequestMapping("/select")
public UserTagInfo select(@RequestParam("id")Integer id){
UserTagInfo result = userTagInfoService.select(id);
return result;
}

//使用@PostMapping支持post请求方式
  //使用@RequestBody支持把提交的json数据自动封装成对象
  @PostMapping("/insert")

public int insert(@RequestBody UserTagInfo userTagInfo){

int id = userTagInfoService.insert(userTagInfo);
return id;
}
}

启动DemoApplication的main函数依然绑定了8080端口,访问http://localhost:8080/usertag/select?id=1报错如下

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): cn.test.mapper.UserTagInfoMapper.selectByPrimaryKey

这里需要我们在application.properties中加入配置如下

#spring编译的时候不会加入xml文件,需要我们告诉spring哪些xml添加进去不然会报找不到对应的mapper.xml
mybatis.type-aliases-package=cn.test.entity
mybatis.mapperLocations=classpath:mapper/*.xml

实际效果如下

在执行插入的时候遇到的一个问题我传时间用的格式是yyyy-MM-dd HH:mm:ss报错如下

大致意思是jackson不支持转换上述格式的字符串为java.util.Date类型,一般直接传时间戳格式解决。

三.总结

到这里Spring Boot整合Mybaties就结束了,首先要引入相关的依赖并在application.properties文件里加入数据库配置;然后创建实体,持久层接口,接口对应的配置文件,要注意配置文件的命名空间及标签id与接口保持一致;然后注意在application.properties文件加入扫描mapper配置,启动类上加上扫描接口的注解,最后就是注意jackson不能把yyyy-MM-dd HH:mm:ss格式的字符串直接转换为java.util.Date类型。

Spring Boot初识(2)- Spring Boot整合Mybaties的更多相关文章

  1. spring transaction 初识

    spring 事务初识 1.spring事务的主要接口,首先盗图一张,展示出spring 事务的相关接口.Spring并不直接管理事务,而是提供了多种事务管理器,他们将事务管理的职责委托给Hibern ...

  2. Spring boot(三)整合mybaties+thymeleaf实现基础crud

    工程结构: 首先在pom文件中引入依赖 <?xml version="1.0" encoding="UTF-8"?> <project xml ...

  3. 初识在Spring Boot中使用JPA

    前面关于Spring Boot的文章已经介绍了很多了,但是一直都没有涉及到数据库的操作问题,数据库操作当然也是我们在开发中无法回避的问题,那么今天我们就来看看Spring Boot给我们提供了哪些疯狂 ...

  4. Spring Boot 2.x 综合示例-整合thymeleaf、mybatis、shiro、logging、cache开发一个文章发布管理系统

    一.概述 经过HelloWorld示例(Spring Boot 2.x 快速入门(上)HelloWorld示例)( Spring Boot 2.x 快速入门(下)HelloWorld示例详解)两篇的学 ...

  5. 【Spring Boot&&Spring Cloud系列】Spring Boot初识

    项目代码地址:https://github.com/AndyFlower/Spring-Boot-Learn/tree/master/Spring-boot-helloworld 一.Spring B ...

  6. Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结

    Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结 这两天闲着没事想利用**Spring Boot**加上阿里的开源数据连接池**Druid* ...

  7. 解决Spring Boot(2.1.3.RELEASE)整合spring-data-elasticsearch3.1.5.RELEASE报NoNodeAvailableException[None of the configured nodes are available

    Spring Boot(2.1.3.RELEASE)整合spring-data-elasticsearch3.1.5.RELEASE报NoNodeAvailableException[None of ...

  8. Spring 5.x 、Spring Boot 2.x 、Spring Cloud 与常用技术栈整合

    项目 GitHub 地址:https://github.com/heibaiying/spring-samples-for-all 版本说明: Spring: 5.1.3.RELEASE Spring ...

  9. Spring Boot 2.X 如何快速整合jpa?

    本文目录 一.JPA介绍二.Spring Data JPA类结构图1.类的结构关系图三.代码实现1.添加对应的Starter2.添加连接数据库的配置3.主要代码 一.JPA介绍 JPA是Java Pe ...

随机推荐

  1. SSL、TLS协议格式、HTTPS通信过程、RDP SSL通信过程(缺heartbeat)

    SSL.TLS协议格式.HTTPS通信过程.RDP SSL通信过程   相关学习资料 http://www.360doc.com/content/10/0602/08/1466362_30787868 ...

  2. NC 自定义参照类

    package nc.ui.hzctr.costctr.ref; import nc.ui.bd.ref.AbstractRefModel; import nc.vo.pubapp.pattern.p ...

  3. 学习UI设计书籍推荐

    在学习UI设计的过程当中,特别想学或者零基础的人来说,需要学习到很多知识,比如软件 PS AI ,理论 色彩 排版 规范 UE 等,这些都是一名UI设计师需要学习的知识,而学习到这些知识,可以通过视频 ...

  4. Monotonic Array LT896

    An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...

  5. Python3实战系列之五(获取印度售后数据项目)

    问题:续接上一篇.说干咱就干呀,勤勤恳恳写程序呀! 目标:此篇我们试着把python程序打包成.exe程序.这样就可以在服务器上运行了.实现首篇计划列表功能模块的第二步: 2.将python程序转为 ...

  6. Mysql知识点个人整理

    1.概念 数据库:保存有组织的数据的容器. 表: 某种特定类型数据的结构化清单 模式:关于数据库和表的布局和特性的信息?(有时指数据库) 主键: primary key 一个列或一组列,其值能唯一区分 ...

  7. ActiveMQ_2安装

    Linux安装 环境JDK7以上 gz文件拷贝到 /usr/local/目录下 解压 后缀为 .tar.gz的压缩包 进入解压后的文件夹 cd apache-activemq-x.xx.x/ cd b ...

  8. J2CACHE 两级缓存框架

    概述 缓存框架我们有ehcache 和 redis 分别是 本地内存缓存和 分布式缓存框架.在实际情况下如果单台机器 使用ehcache 就可以满足需求了,速度快效率高,有些数据如果需要多台机器共享这 ...

  9. Day02 (黑客成长日记)

    #用户登录次数为三代码 # i = 0 # while i < 3: # username = input('请输入账号:') # password = input('请输入密码:') # if ...

  10. 安装Python 库软件时提示"setuptools must be installed to install from a source distribution"错误

    通过如下方式安装: sudo pip install --upgrade pip sudo pip install setuptools 如果提示pip命令没找到,需要先安装python-pip.