Spring Boot:Spring Boot配置Mybatis
1. maven引入依賴
<!-- 資料庫連線池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <!-- mybatis 依賴--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis-spring-boot.version}</version> </dependency> <!-- jdbc支援--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- mysql驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-connector-java.version}</version> </dependency>
2. 設定Druid資料來源
新增SingleDataSourceConfig.java
配置檔案,整合Druid資料來源
/** * 資料來源配置 * @Auther: hrabbit * @Date: 2018-12-19 11:18 AM * @Description: */@Configuration@MapperScan({"com.hrabbit.admin.modual.system.mapper"})public class SingleDataSourceConfig { /** * Druid配置 */ @Bean @ConfigurationProperties(prefix = "spring.datasource") public DruidProperties druidProperties() { return new DruidProperties(); } }
其中的DruidProperties.java
的配置資訊如下(這裡使用了Lombok,因此省略了get/set方法,不瞭解Lombok
的童鞋請搜尋下):
/** * 資料庫資料來源配置 * @author hrabbit * @date 2018-11-21 11:18 */@Component@ConfigurationProperties(prefix = "spring.datasource")@Datapublic class DruidProperties { private String url = "jdbc:mysql://127.0.0.1:3306/guns?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull"; private String username = "root"; private String password = "root"; private String driverClassName = "com.mysql.jdbc.Driver"; private Integer initialSize = 2; private Integer minIdle = 1; private Integer maxActive = 20; private Integer maxWait = 60000; private Integer timeBetweenEvictionRunsMillis = 60000; private Integer minEvictableIdleTimeMillis = 300000; private String validationQuery = "SELECT 'x'"; private Boolean testWhileIdle = true; private Boolean testOnBorrow = false; private Boolean testOnReturn = false; private Boolean poolPreparedStatements = true; private Integer maxPoolPreparedStatementPerConnectionSize = 20; private String filters = "stat"; public void config(DruidDataSource dataSource) { dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setDriverClassName(driverClassName); dataSource.setInitialSize(initialSize); //定義初始連線數 dataSource.setMinIdle(minIdle); //最小空閒 dataSource.setMaxActive(maxActive); //定義最大連線數 dataSource.setMaxWait(maxWait); //最長等待時間 // 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連線,單位是毫秒 dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); // 配置一個連線在池中最小生存的時間,單位是毫秒 dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); dataSource.setValidationQuery(validationQuery); dataSource.setTestWhileIdle(testWhileIdle); dataSource.setTestOnBorrow(testOnBorrow); dataSource.setTestOnReturn(testOnReturn); // 開啟PSCache,並且指定每個連線上PSCache的大小 dataSource.setPoolPreparedStatements(poolPreparedStatements); dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); try { dataSource.setFilters(filters); } catch (SQLException e) { e.printStackTrace(); } } }
3.配置application.yml
檔案中的mapper.xml的存放路徑
### 啟動埠號server: port: 8080### 設定資料庫spring: datasource: url: jdbc:mysql://127.0.0.1:3306/hrabbit_admin?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false username: root password: root### 配置Mybatismybatis: ### xml存放路徑 mapper-locations: classpath:mapper/*Mapper.xml
mapper.xml的路徑resources/mapper/*.Mapper.xml
如下:
image.png
4. 建立SysUserMapper.java
的介面:
建立一個根據id查詢的介面:
/** * 系統使用者資料庫層 * @Auther: hrabbit * @Date: 2018-12-19 12:02 PM * @Description: */public interface SysUserMapper { /** * 根據id查詢使用者資訊 * @param id * @return */ SysUser findSysUserById(@Param("id") Long id); }
5. 在SysUserMapper.xml
編寫sql語句:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" ""><mapper namespace="com.hrabbit.admin.modual.system.mapper.SysUserMapper"> <!-- 根據使用者id獲取角色資訊 --> <select id="findSysUserById" resultType="com.hrabbit.admin.modual.system.bean.SysUser"> select * from sys_user where id=#{id}; </select></mapper>
6. 啟動專案,在Postman訪問獲取使用者資訊的介面:
image.png
作者:hrabbits
連結:
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/1747/viewspace-2819770/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Spring Boot + Mybatis + Spring MVC環境配置(二):Mybatis Generator配置Spring BootMyBatisMVC
- Spring boot學習(三) Spring boot整合mybatisSpring BootMyBatis
- Spring Boot + Mybatis + Spring MVC環境配置(三):DataSource配置Spring BootMyBatisMVC
- Spring Boot 整合 MyBatisSpring BootMyBatis
- spring boot 整合mybatisSpring BootMyBatis
- Spring Boot + Mybatis + Spring MVC環境配置(一) :Spring Boot初始化,依賴新增Spring BootMyBatisMVC
- Spring Boot:Spring Boot配置SwaggerSpring BootSwagger
- Spring Boot系列(三):Spring Boot整合Mybatis原始碼解析Spring BootMyBatis原始碼
- Spring Boot & 配置Spring Boot
- Spring boot 六 整合 MyBatisSpring BootMyBatis
- Spring Boot MyBatis配置多種資料庫Spring BootMyBatis資料庫
- Spring boot學習(五)Spring boot整合Mybatis Generator以及PageHelperSpring BootMyBatis
- Spring Boot + Mybatis + Spring MVC環境配置(五):templates模板使用Spring BootMyBatisMVC
- Spring Boot + Mybatis + Spring MVC環境配置(四):MVC框架搭建Spring BootMyBatisMVC框架
- Spring boot學習(二) Spring boot基礎配置Spring Boot
- Spring Boot學習筆記:Spring Boot核心配置Spring Boot筆記
- spring boot 配置 JPASpring Boot
- Spring Boot核心配置Spring Boot
- Spring Boot 教程 - MyBatis-PlusSpring BootMyBatis
- Spring Boot 整合 Mybatis 和 MySQLSpring BootMyBatisMySql
- Spring Boot —— 整合 MyBatis-PlusSpring BootMyBatis
- Spring boot入門(二):Spring boot整合MySql,Mybatis和PageHelper外掛Spring BootMySqlMyBatis
- Spring Boot 原始碼分析 資料來源 + Mybatis 配置Spring Boot原始碼MyBatis
- Spring boot學習(九)Spring boot配置郵件傳送Spring Boot
- Spring boot學習(八)Spring boot配置ehcache快取框架Spring Boot快取框架
- Spring Boot Security配置教程Spring Boot
- Spring Boot日誌配置Spring Boot
- Spring Boot 配置檔案Spring Boot
- Spring Boot中配置 AuditListenerSpring Boot
- 【Spring Boot】yaml配置注入Spring BootYAML
- 【Spring Boot】快速整合Mybatis-PlusSpring BootMyBatis
- spring-boot mybatis 連線MySQLSpringbootMyBatisMySql
- Spring Boot (十三): Spring Boot 小技巧Spring Boot
- 小代學Spring Boot之整合MyBatisSpring BootMyBatis
- Spring Boot使用MyBatis Generator、SwaggerSpring BootMyBatisSwagger
- Spring Boot+Mybatis專案總結Spring BootMyBatis
- Spring Boot(六)整合 MyBatis 操作 MySQL 8Spring BootMyBatisMySql
- Spring Boot 學習筆記(3):MyBatisSpring Boot筆記MyBatis