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. html 试题试卷(包含latex)下载成word - - java

    html 试题试卷(包含latex)下载成word 主要目的: 分享将带latex的html格式的试题试卷以word的格式下载,并且加一些灵活的排版样式 接受群众的检阅,获得反馈 骗取打赏,或者git ...

  2. (八十二)c#Winform自定义控件-穿梭框

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

  3. 揭秘C# SQLite的从安装到使用

    SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了 ...

  4. CRS-2674: Start of 'ora.cssd' on 'rac2' failed 引发的rac集群服务起不来问题

    问题背景:客户反馈Oracle rac集群节点宕机 1.首先查看宕机原因,归档日志满导致服务重启,查看归档日志路径是USE_DB_RECOVERY_FILE_DEST (默认路径), 安装的时候没有做 ...

  5. Xadmin查询

    目录 深浅coopy运用 ModelForm的补充 提取模型当中相关属性 getattr和get_field的区别 __ str__,get_field,getattr初识 str ,当用getatt ...

  6. mpvue 签字组件

    <template> <div > <canvas class='firstCanvas' canvas-id="firstCanvas" @touc ...

  7. 02-15 Logistic回归(鸢尾花分类)

    目录 Logistic回归(鸢尾花分类) 一.导入模块 二.获取数据 三.构建决策边界 四.训练模型 4.1 C参数与权重系数的关系 五.可视化 更新.更全的<机器学习>的更新网站,更有p ...

  8. Java12新特性 -- switch表达式

    传统switch表达式的弊端: 匹配是自上而下的,如果忘记写break, 后面的case语句不论匹配与否都会执行: 所有的case语句共用一个块范围,在不同的case语句定义的变量名不能重复: 不能在 ...

  9. Python玩转人工智能最火框架 TensorFlow应用实践 ☝☝☝

    Python玩转人工智能最火框架 TensorFlow应用实践 (一个人学习或许会很枯燥,但是寻找更多志同道合的朋友一起,学习将会变得更加有意义✌✌) 全民人工智能时代,不甘心只做一个旁观者,那就现在 ...

  10. 攻防世界(XCTF)WEB(进阶区)write up(三)

    挑着做一些好玩的ctf题 FlatScience web2 unserialize3upload1wtf.sh-150ics-04web i-got-id-200 FlatScience 扫出来的lo ...