SpringBoot之集成MyBatis
程序员文章站
2022-05-29 09:41:49
1. 引入工程依赖包 2. 编写DAO接口 3. 编写SQL配置文件(本人不太习惯注解,习惯将SQL写在配置文件中) 4. 配置myBatis配置类,也可以放在启动类上 5. 配置application.yml文件 6. 编写controller,调用MyBatis 源代码: "https://gi ......
- 引入工程依赖包
<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); } }
推荐阅读
-
MyBatis从入门到精通(八):MyBatis动态Sql之foreach标签的用法
-
springboot+mybatis日志显示SQL的最简单方法
-
springboot系列之03-使用IDEA完成第一个示例程序
-
SpringBoot集成JWT实现权限认证
-
五分钟后,你将学会在SpringBoot项目中如何集成CAT调用链
-
SpringBoot 2.0 开发案例之百倍级减肥瘦身之旅
-
SpringBoot集成WebSocket长连接实际应用详解
-
Spring Boot2(二):使用Spring Boot2集成Mybatis缓存机制
-
springboot~mybatis里localdatetime序列化问题
-
Springboot 2.0.x 集成基于Centos7的Redis集群安装及配置