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. (七十九)c#Winform自定义控件-导航菜单

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  2. 把功能强大的Spring EL表达式应用在.net平台

    Spring EL 表达式是什么? Spring3中引入了Spring表达式语言—SpringEL,SpEL是一种强大,简洁的装配Bean的方式,他可以通过运行期间执行的表达式将值装配到我们的属性或构 ...

  3. [Python] Python 学习记录(1)

    1.概论 弱类型 一个变量能被赋值时能与原类型不同 x = 1 x = "1"  #不加分号,给x赋值为int后再次赋值为string是可行的 与或非 and or not /  ...

  4. web前端之浏览器: 知识汇总

    一.URL到页面 准备阶段: 输入URL,Enter进入查找 浏览器在本地查找host文件,匹配对应的IP: 找到返回浏览器并缓存 没有,则进入路由查找: 找到返回浏览器并缓存 再没有,再进入公网DN ...

  5. 人生,还没困难到"非死不可"

    最近半个月,美国著名的Facebook公司,出了好几件大事.第一件事,2019年9月19日,一名陈姓中国软件工程师在Facebook加州总部跳楼自杀.第二件事,2019年10月4日,一名软件工程师在座 ...

  6. 服务网关Spring Cloud Zuul

    Spring Cloud Zuul 开发环境 idea 2019.1.2 jdk1.8.0_201 Spring Boot 2.1.9.RELEASE Spring Cloud Greenwich S ...

  7. Spring 源码阅读 一

    终于,有一天我也来看Spring的源码了,看了一阵之后感觉心情那叫一个舒畅,对Spring底层的实现也有了进一步的了解, 最直观的感受就是Spring的命名风格很赞,很长,真的长到使人见名知意, 闲言 ...

  8. DJango错误日志生成

    DJango错误日志生成 setting.py设置 LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'formatters': ...

  9. 那些你不知道的HTML知识,快来学习一下吧

    前言 HTML作为前端三大基础知识点之一,是每一个前端开发人员都要掌握的部分.今天这篇文章我们来看看一些平时不太会注意,却在面试时可能会问到的题目,来看看你都会吗? 如何使用div模拟实现textar ...

  10. libevent::事件::定时器2

    #define evtimer_new(b, cb, arg) event_new((b), -1, 0, (cb), (arg)) #include <cstdio> #include ...