2019独角兽企业重金招聘Python工程师标准>>>

数据库以MySQL为例

IDE:spring tool suite

1. 新建项目

file -> new -> Spring Starter Project

填写项目名称,直接next,最后finish。

2. 更改pom.xml

添加如下依赖:

<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.2.0</version>
</dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.0.27</version>
</dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId>
</dependency>

3. 更改配置文件

    配置文件使用的yml格式

spring:datasource:name: ebusurl: jdbc:mysql://localhost:3306/test?autoReconnect=true&useSSL=falseusername: rootpassword: root# 使用druid数据源type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.jdbc.Drivermybatis:type-aliases-package: com.lty.ebus.model#如果不是在Mapper里直接写sql语句,而是在xml里写,需要指定XML文件路径#mapper-locations: classpath:mapping/*.xmllogging:level:root: WARNcom:lantaiyuan:ebus:mapper: TRACE

配置文件使用properties

#主配置文件,配置了这个会优先读取里面的属性覆盖主配置文件的属性
spring.profiles.active=dev
spring.application.name=user# 应用自定义配置
logging.config=classpath:logback.xml# mysql
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
#初始化连接大小
spring.datasource.druid.initial-size=8#最小空闲连接数
spring.datasource.druid.min-idle=5
#最大连接数
spring.datasource.druid.max-active=10
#查询超时时间
spring.datasource.druid.query-timeout=6000
#事务查询超时时间
spring.datasource.druid.transaction-query-timeout=6000
#关闭空闲连接超时时间
spring.datasource.druid.remove-abandoned-timeout=1800# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=10.1.10.80
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0#mybatis
mybatis.mapper-locations=classpath:mapping/*.xml

注意: type-aliases-package配置项。

4. 新建Model和Mapper

model:

public class City {private int id;private String name;private int state;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getState() {return state;}public void setState(int state) {this.state = state;}}

mapper(需要使用@Mapper注解):

@Mapper
public interface CityMapper {@Select("select * from city where state = #{state}")City findByState(@Param("state") int state);/*** 如果不是在Mapper中写sql语句,这里直接写方法即可。* City findByState(@Param("state") int state);*/}

5. 新建Spring boot application

@SpringBootApplication
public class SampleMapperApplication implements CommandLineRunner {public static void main(String[] args) {SpringApplication.run(SampleMapperApplication.class, args);}final private CityMapper cityMapper;public SampleMapperApplication(CityMapper cityMapper) {this.cityMapper = cityMapper;}@Overridepublic void run(String... args) throws Exception {System.out.println(this.cityMapper.findByState(1).getName());}}

6. 右键run as -> Spring Boot App

运行结果如下:

7. 补充说明

其一:

项目目录结构如下:

SampleMapperApplication.java与mapper的位置需注意,不然mapper有可能注入失败。

其二:

yml配置MySQL连接时,加上autoReconnect=true&useSSL=false

url: jdbc:mysql://localhost:3306/test?autoReconnect=true&useSSL=false

如果不加上,会有如下警告:

WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

加上后,不再有警告,此处参考Stack overflow。

其三:

第一次使用Spring boot,能快速整合成功,还参考了mybatis-spring-boot-autoconfigure和Spring Boot 集成MyBatis。

转载于:https://my.oschina.net/u/3149614/blog/860199

Spring Boot与MyBatis整合相关推荐

  1. Spring Boot + Security + MyBatis + Thymeleaf + Activiti 快速开发平台项目

    项目介绍 Spring Boot + Security + MyBatis + Thymeleaf + Activiti 快速开发平台 基于 Layui 的后台管理系统模板,扩展 Layui 原生 U ...

  2. Spring Boot 实战 —— MyBatis(注解版)使用方法

    原文链接: Spring Boot 实战 -- MyBatis(注解版)使用方法 简介 MyBatis 官网 是这么介绍它自己的: MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过 ...

  3. Spring Boot + Security + MyBatis + Thymeleaf + Activiti 快速开发平台

    前言 项目介绍 Spring Boot + Security + MyBatis + Thymeleaf + Activiti 快速开发平台 基于Layui的后台管理系统模板,扩展Layui原生UI样 ...

  4. Spring Boot 2.x整合Quartz

    宣传官网 xb.exrick.cn 在线Demo xboot.exrick.cn 开源版Github地址 github.com/Exrick/x-bo- 开发文档 www.kancloud.cn/ex ...

  5. Spring Boot 集成 Mybatis 实现双数据源

    转载自   Spring Boot 集成 Mybatis 实现双数据源 这里用到了Spring Boot + Mybatis + DynamicDataSource配置动态双数据源,可以动态切换数据源 ...

  6. spring boot集成mybatis+事务控制

    一下代码为DEMO演示,采用注解的方式完成Spring boot和Mybatis的集成,并进行事物的控制 数据源的配置: 1 spring.datasource.url=jdbc:mysql://lo ...

  7. spring boot 集成Mybatis时 Invalid bound statement (not found)

    spring boot 集成Mybatis时,运行提示 org.apache.ibatis.binding.BindingException: Invalid bound statement (not ...

  8. spring boot配置mybatis和事务管理

    spring boot配置mybatis和事务管理 一.spring boot与mybatis的配置 1.首先,spring boot 配置mybatis需要的全部依赖如下: <!-- Spri ...

  9. spring boot使用Jedis整合Redis

    文章目录 spring boot使用jedis整合redis 总结 Spring Boot整合Redis有两种方式,分别是Jedis和RedisTemplate,那么它们二者有什么区别呢? 1.Jed ...

最新文章

  1. Python:Scrapy的settings
  2. webrtc 语音流java_通过WebView WebRTC从麦克风传输语音时的语音识别
  3. toast弹窗_一个弹窗的设计思考
  4. Epic高管:虚幻4引擎目标是不同规模开发商
  5. linux 优化 sysctl.conf,Linux内核sysctl.conf的优化设置
  6. Java语法基础----课后实践作业
  7. 四则运算界面版 结对子
  8. JS base64 加密和 后台 base64解密(防止中文乱码)
  9. 古代汉语(王力版)笔记 通论6-7
  10. 软件需求跟踪矩阵例子
  11. PC端和移动端的区别你知道吗?
  12. Netapp 存储文件共享-windows 系统应用
  13. 文本情感分类python_文本情感分类(一):传统模型
  14. SMTP协议?SMTP端口号?SMTP服务器?
  15. 如何将两张图片合成一张图片
  16. LOB浅析(CLOB/BCLOB/NCLOB)
  17. 移动端 click 300ms 延迟,如何解决
  18. 从内积、外积和叉乘到多维空间的理解
  19. EOS映射的坑——存钱包用户必看
  20. 高速光耦(PS8101,TLP112A,TLP109)基本工作原理应用实例

热门文章

  1. 【OpenCV3】cv::Mat类成员函数详解
  2. MySQL — 优化之explain执行计划详解(转)
  3. 提供openssl -aes-256-cbc兼容加密/解密的简单python函数
  4. Cocos2d-x 3.0正式版及android环境搭建
  5. java web中中文乱码问题汇总
  6. C++中接口与实现分离的技术
  7. PSO DE EA算法的不同及相同之处。
  8. linux下开启程序崩溃生成core文件开关之ulimit详解
  9. strcpy和strncpy区别 memcpy strcpy strncpy lstrcpy lstrncpy wstrcpy, memmove
  10. 类型转换操作符:static_cast, dynamic_cast, const_cast, reinterpret_cast.