1. 引入工程依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency> <dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
  1. 编写DAO接口
package com.example.mybatis.repository;

import com.example.mybatis.domain.SmsCoupon;
import org.apache.ibatis.annotations.Param; /**
* @author shanks on 2019-11-09
*/ public interface SmsCouponDAO { SmsCoupon queryById(@Param("id") Integer id);
}
  1. 编写SQL配置文件(本人不太习惯注解,习惯将SQL写在配置文件中)
<?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="com.example.mybatis.repository.SmsCouponDAO"> <resultMap type="com.example.mybatis.domain.SmsCoupon" id="SmsCouponMap">
<result property="id" column="id" jdbcType="INTEGER"/>
<result property="type" column="type" jdbcType="INTEGER"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="platform" column="platform" jdbcType="INTEGER"/>
<result property="count" column="count" jdbcType="INTEGER"/>
<result property="amount" column="amount" jdbcType="NUMERIC"/>
<result property="perLimit" column="per_limit" jdbcType="INTEGER"/>
<result property="minPoint" column="min_point" jdbcType="NUMERIC"/>
<result property="startTime" column="start_time" jdbcType="TIMESTAMP"/>
<result property="endTime" column="end_time" jdbcType="TIMESTAMP"/>
<result property="useType" column="use_type" jdbcType="INTEGER"/>
<result property="note" column="note" jdbcType="VARCHAR"/>
<result property="publishCount" column="publish_count" jdbcType="INTEGER"/>
<result property="useCount" column="use_count" jdbcType="INTEGER"/>
<result property="receiveCount" column="receive_count" jdbcType="INTEGER"/>
<result property="enableTime" column="enable_time" jdbcType="TIMESTAMP"/>
<result property="code" column="code" jdbcType="VARCHAR"/>
<result property="memberLevel" column="member_level" jdbcType="INTEGER"/>
</resultMap> <!--查询单个-->
<select id="queryById" resultMap="SmsCouponMap">
select
id, type, name, platform, count, amount, per_limit, min_point, start_time, end_time, use_type, note, publish_count, use_count, receive_count, enable_time, code, member_level
from mall.sms_coupon
where id = #{id}
</select> </mapper>
  1. 配置myBatis配置类,也可以放在启动类上
package com.example.mybatis.config;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration; /**
* @author shanks on 2019-11-09
*/
@Configuration
@MapperScan("com.example.mybatis.repository")
public class MyBatisConfig {
}
  1. 配置application.yml文件
spring:
datasource:
#数据源基本配置
url: jdbc:mysql://localhost:3306/mall?allowMultiQueries=true&characterEncoding=utf-8&useSSL=false
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
druid:
#连接池配置, 初始化大小,最小,最大
initial-size: 5
max-active: 10
#配置获取连接等待超时的时间
max-wait: 60000
#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
time-between-eviction-runs-millis: 60000
#配置一个连接在池中最小生存的时间,单位是毫秒
min-evictable-idle-time-millis: 300000
validation-query: SELECT 1
test-while-idle: true
test-on-borrow: false
test-on-return: false mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml
  1. 编写controller,调用MyBatis
package com.example.mybatis.web;

import com.example.mybatis.domain.SmsCoupon;
import com.example.mybatis.service.SmsCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; /**
* @author shanks on 2019-11-09
*/
@RestController
@RequestMapping("/voucher/web/")
public class SmsCouponController { @Autowired
SmsCouponService smsCouponService; @RequestMapping("/querySmsCouponDetail")
public SmsCoupon querySmsCouponDetail(@RequestParam("id") int id){
SmsCoupon smsCoupon = smsCouponService.querySmsCouponDetail(id);
return smsCoupon;
}
}
package com.example.mybatis.service;

import com.example.mybatis.domain.SmsCoupon;

/**
* @author shanks on 2019-11-09
*/
public interface SmsCouponService {
SmsCoupon querySmsCouponDetail(int id);
}
package com.example.mybatis.service.impl;

import com.example.mybatis.domain.SmsCoupon;
import com.example.mybatis.repository.SmsCouponDAO;
import com.example.mybatis.service.SmsCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; /**
* @author shanks on 2019-11-09
*/
@Service
public class SmsCouponServiceImpl implements SmsCouponService { @Autowired
private SmsCouponDAO smsCouponDAO; @Override
public SmsCoupon querySmsCouponDetail(int id) {
SmsCoupon smsCoupon = smsCouponDAO.queryById(id);
return smsCoupon;
}
}
package com.example.mybatis;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class DemoMybatisApplication { public static void main(String[] args) {
SpringApplication.run(DemoMybatisApplication.class, args);
} }

源代码:https://gitee.com/shanksV/springboot-mybatis.git

SpringBoot之集成MyBatis的更多相关文章

  1. 在springboot中集成mybatis开发

    在springboot中利用mybatis框架进行开发需要集成mybatis才能进行开发,那么如何在springboot中集成mybatis呢?按照以下几个步骤就可以实现springboot集成myb ...

  2. springboot之集成mybatis mongo shiro druid redis jsp

    闲来无事,研究一下spingboot  发现好多地方都不一样了,第一个就是官方默认不支持jsp  于是开始狂找资料  终于让我找到了 首先引入依赖如下: <!-- tomcat的支持.--> ...

  3. springboot如何集成mybatis的pagehelper分页插件

    mybatis提供了一个非常好用的分页插件,之前集成的时候需要配置mybatis-config.xml的方式,今天我们来看下它是如何集成springboot来更好的服务的. 只能说springboot ...

  4. SpringBoot系列: 集成MyBatis

    本文主要修改自下面博客:http://www.ityouknow.com/springboot/2016/11/06/spring-boo-mybatis.htmlhttp://tengj.top/2 ...

  5. SpringBoot入门-集成mybatis(四)

    pom.xml <parent> <groupId>org.springframework.boot</groupId> <artifactId>spr ...

  6. (二十)SpringBoot之集成mybatis:使用mybatis注解

    一.使用mybatis注解的集成 1.1 引入maven依赖 <dependencies> <dependency> <groupId>org.springfram ...

  7. (二十一)SpringBoot之集成mybatis:使用mybatis xml

    一.引入maven依赖 <dependencies> <dependency> <groupId>org.springframework.boot</grou ...

  8. springboot在集成mybatis的时候老是报错 The server time zone value '�й���׼ʱ��' is unrecognized

    我已经解决了,感谢万能网友. 解决办法参见:https://blog.csdn.net/yunfeng482/article/details/86698133

  9. SpringBoot 集成MyBatis 中的@MapperScan注解

    SpringBoot 集成MyBatis 中的@MapperScan注解 2018年08月17日 11:41:02 文火慢炖 阅读数:398更多 个人分类: 环境搭建 在SpringBoot中集成My ...

随机推荐

  1. 43 道检验基础的 JavaScript 面试题

    导读 这两天的GitHub Trending repositories被一个名叫 javascript-questions的项目霸榜了,项目中记录了一些JavaScript题目. 文中有些点作者解释的 ...

  2. php导出excel乱码怎么处理

    使用PHP导出excel文档,有时候莫名其妙就会出现导出的数据乱码,现在推荐一个万能修补大法 话不多说,直接上代码 核心就是在处理完数据之后,输出excel文件之前 添加 ob_end_clean() ...

  3. win10 php安装redis 扩展

    redis下载:https://github.com/MicrosoftArchive/redis/releases 我下载的是zip包,下载后安装redis. 开始安装php的reids扩展 查看p ...

  4. 通过搭建MySQL掌握k8s(Kubernetes)重要概念(下):参数配置

    本文通过搭建MySQL环境来了解k8s的重要概念,包括持久卷,网络和参数配置.这是下篇,专门讲解参数配置.如果你有些地方不能完全看明白,请先看上篇"通过搭建MySQL掌握k8s(Kubern ...

  5. Linux入门(磁盘与挂载)

    Linux入门之 磁盘管理与挂载   在我们使用计算机或者是服务器时,总会需要接入外部存储的时候,正如我们使用的移动硬盘.U盘.接入手机等,就是一个接入外部存储的过程.上述这些在接入Windows时我 ...

  6. 并发新构件之DelayQueue:延时队列

    DelayQueue:延时队列,首先是一个队列,所以可以持有对象,但是仅限于实现了Delayed接口的对象.重写getDelay()和compareTo()(因为要比较)方法: 通俗来讲:延时队列的就 ...

  7. 配置Ant执行Jmeter脚本

    1.将 jmeter下extras目录中ant-jmeter-1.1.1.jar包拷贝至ant安装目录下的lib目录中,否则会报错ant-jmeter-1.1.1不存在 2.在jmeter根目录下创建 ...

  8. 域渗透基础之NTLM认证协议

    域渗透基础的两个认证协议ntlm和Kerberos协议是必须总结的~ 这篇简单总结下ntlm协议 晚上写下kerberos 0x01 NTLM简介 NTLM使用在Windows NT和Windows ...

  9. windows系统安全日志取证工具

    0x01 关于日志 Windows安全事件日志中详细记录了是谁在什么时候通过什么手段登录到系统或者注销了登录,通过分析该日志可以详细了解服务器的安全情况以及必要时的取证工作. 0x02 查看日志 传统 ...

  10. 实用---eclipse中的代码提示功能

    Eclipse 的代码提示功能,具体配置 1. 打开Eclipse ,然后“window”→“Preferences” 2. 选择“java”,展开,“Editor”,选择“Content Assis ...