SpringBoot之集成MyBatis
2019-11-09 16:04:33来源:博客园 阅读 ()
SpringBoot之集成MyBatis
- 引入工程依赖包
<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>
- 编写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);
}
- 编写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>
- 配置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 {
}
- 配置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
- 编写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
原文链接:https://www.cnblogs.com/muyl/p/11828565.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- springboot2配置JavaMelody与springMVC配置JavaMelody 2020-06-11
- MyBatis中的$和#,用不好,准备走人! 2020-06-11
- SpringBoot 2.3 整合最新版 ShardingJdbc + Druid + MyBatis 2020-06-11
- 掌握SpringBoot-2.3的容器探针:实战篇 2020-06-11
- nacos~配置中心功能~springboot的支持 2020-06-10
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash