增加 spring-boot + druid + mybatis 整合用例
This commit is contained in:
parent
64296b35e6
commit
5d41bacf9a
34
README.md
34
README.md
@ -46,23 +46,23 @@ spring-cloud:Finchley.SR2
|
||||
|
||||
## 2. spring-boot samples
|
||||
|
||||
| samples | 描述 | 官方文档 |
|
||||
| ------------------------------------------------------------ | ---------------------------------- | ------------------------------------------------------------ |
|
||||
| [spring-boot-base](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-base) | spring-boot 基础 | [spring boot 官方文档](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/)<br>[spring boot 中文官方文档](https://www.breakyizhan.com/springboot/3028.html) |
|
||||
| [spring-boot-yml-profile](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-yml-profile) | yml 语法和多配置切换 | [Using YAML Instead of Properties](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-external-config-yaml) |
|
||||
| [spring-boot-servlet](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-servlet) | 整合servlet 3.0 | [Embedded Servlet Container Support](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-embedded-container) |
|
||||
| [spring-boot-tomcat](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-tomcat) | spring-boot 整合外部容器(tomcat) | [Use Another Web Server](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#howto-use-another-web-server) |
|
||||
| [spring-boot-jsp](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-jsp) | spring-boot 整合 jsp | [JSP Limitations](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-jsp-limitations) |
|
||||
| spring-boot-jpa | spring-boot jpa 的使用 | [Spring Data JPA](https://docs.spring.io/spring-data/jpa/docs/2.1.3.RELEASE/reference/html/) |
|
||||
| spring-boot-mybatis | spring-boot 整合 mybatis | [Mybatis-Spring](http://www.mybatis.org/spring/zh/index.html)<br/>[Mybatis-Spring-Boot-Autoconfigure](http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/) |
|
||||
| spring-boot-druid-mybtais | spring-boot 整合druid、mybatis | [Alibaba druid](https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98) |
|
||||
| [spring-boot-redis](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-redis) | spring-boot 整合 redis | [Working with NoSQL Technologies](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-nosql) |
|
||||
| [spring-boot-mongodb](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-mongodb) | spring-boot 整合 mongodb | [Working with NoSQL Technologies](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-nosql) |
|
||||
| [spring-boot-memcached](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-memcached) | spring-boot 整合 memcached | [Xmemcached](https://github.com/killme2008/xmemcached/wiki/Xmemcached%20%E4%B8%AD%E6%96%87%E7%94%A8%E6%88%B7%E6%8C%87%E5%8D%97) |
|
||||
| [spring-boot-rabbitmq](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-rabbitmq) | spring-boot 整合 rabbitmq | [RabbitMQ support](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-rabbitmq) |
|
||||
| [spring-boot-dubbo](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-dubbo) | spring-boot 整合 dubbo | [Dubbo ](http://dubbo.apache.org/zh-cn/docs/user/quick-start.html) |
|
||||
| [spring-boot-websocket](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-websocket) | spring-boot 整合 websocket | [Using @ServerEndpoint](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#howto-create-websocket-endpoints-using-serverendpoint) |
|
||||
| spring-boot-kafka | spring-boot 整合 kafka | [Apache Kafka Support](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-kafka) |
|
||||
| samples | 描述 | 官方文档 |
|
||||
| ------------------------------------------------------------ | ---------------------------------------------------------- | ------------------------------------------------------------ |
|
||||
| [spring-boot-base](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-base) | spring-boot 基础 | [spring boot 官方文档](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/)<br>[spring boot 中文官方文档](https://www.breakyizhan.com/springboot/3028.html) |
|
||||
| [spring-boot-yml-profile](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-yml-profile) | yml 语法和多配置切换 | [Using YAML Instead of Properties](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-external-config-yaml) |
|
||||
| [spring-boot-servlet](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-servlet) | 整合servlet 3.0 | [Embedded Servlet Container Support](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-embedded-container) |
|
||||
| [spring-boot-tomcat](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-tomcat) | spring-boot 整合外部容器(tomcat) | [Use Another Web Server](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#howto-use-another-web-server) |
|
||||
| [spring-boot-jsp](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-jsp) | spring-boot 整合 jsp | [JSP Limitations](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-jsp-limitations) |
|
||||
| spring-boot-jpa | spring-boot jpa 的使用 | [Spring Data JPA](https://docs.spring.io/spring-data/jpa/docs/2.1.3.RELEASE/reference/html/) |
|
||||
| spring-boot-mybatis | spring-boot 整合 mybatis<br/>HikariDataSources数据源的配置 | [Mybatis-Spring](http://www.mybatis.org/spring/zh/index.html)<br/>[Mybatis-Spring-Boot-Autoconfigure](http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/) |
|
||||
| spring-boot-druid-mybtais | spring-boot 整合druid、mybatis | [Alibaba druid](https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98) |
|
||||
| [spring-boot-redis](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-redis) | spring-boot 整合 redis | [Working with NoSQL Technologies](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-nosql) |
|
||||
| [spring-boot-mongodb](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-mongodb) | spring-boot 整合 mongodb | [Working with NoSQL Technologies](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-nosql) |
|
||||
| [spring-boot-memcached](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-memcached) | spring-boot 整合 memcached | [Xmemcached](https://github.com/killme2008/xmemcached/wiki/Xmemcached%20%E4%B8%AD%E6%96%87%E7%94%A8%E6%88%B7%E6%8C%87%E5%8D%97) |
|
||||
| [spring-boot-rabbitmq](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-rabbitmq) | spring-boot 整合 rabbitmq | [RabbitMQ support](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-rabbitmq) |
|
||||
| [spring-boot-dubbo](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-dubbo) | spring-boot 整合 dubbo | [Dubbo ](http://dubbo.apache.org/zh-cn/docs/user/quick-start.html) |
|
||||
| [spring-boot-websocket](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-websocket) | spring-boot 整合 websocket | [Using @ServerEndpoint](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#howto-create-websocket-endpoints-using-serverendpoint) |
|
||||
| spring-boot-kafka | spring-boot 整合 kafka | [Apache Kafka Support](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-kafka) |
|
||||
|
||||
更多的场景和用例可参阅 [spring-boot 官方samples ](https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples)
|
||||
|
||||
|
66
spring-boot/spring-boot-druid-mybatis/pom.xml
Normal file
66
spring-boot/spring-boot-druid-mybatis/pom.xml
Normal file
@ -0,0 +1,66 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.1.1.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.heibaiying</groupId>
|
||||
<artifactId>spring-boot-druid-mybatis</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>spring-boot-druid-mybatis</name>
|
||||
<description>druid-mybatis project for Spring Boot</description>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!--spring 1.5 x 以上版本对应 mybatis 1.3.x (1.3.1)
|
||||
关于更多spring-boot 与 mybatis 的版本对应可以参见 <a href="http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/">-->
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>1.3.2</version>
|
||||
</dependency>
|
||||
<!--引入mysql驱动-->
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.13</version>
|
||||
</dependency>
|
||||
<!--druid 依赖-->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
<version>1.1.10</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<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>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -0,0 +1,14 @@
|
||||
package com.heibaiying.springboot;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringBootDruidMybatisApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootDruidMybatisApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,38 @@
|
||||
package com.heibaiying.springboot.bean;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 实体类
|
||||
*/
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@ToString
|
||||
public class Programmer {
|
||||
|
||||
private int id;
|
||||
|
||||
private String name;
|
||||
|
||||
private int age;
|
||||
|
||||
private float salary;
|
||||
|
||||
private Date birthday;
|
||||
|
||||
public Programmer(String name, int age, float salary, Date birthday) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.salary = salary;
|
||||
this.birthday = birthday;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.heibaiying.springboot.controller;
|
||||
|
||||
import com.alibaba.druid.stat.DruidStatManagerFacade;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :在 Spring Boot 中可以通过 HTTP 接口将 Druid 监控数据以JSON 的形式暴露出去,
|
||||
* 实际使用中你可以根据你的需要自由地对监控数据、暴露方式进行扩展。
|
||||
*/
|
||||
|
||||
@RestController
|
||||
public class DruidStatController {
|
||||
|
||||
@GetMapping("/stat")
|
||||
public Object druidStat() {
|
||||
// DruidStatManagerFacade#getDataSourceStatDataList 该方法可以获取所有数据源的监控数据
|
||||
return DruidStatManagerFacade.getInstance().getDataSourceStatDataList();
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.heibaiying.springboot.controller;
|
||||
|
||||
import com.alibaba.druid.stat.DruidStatManagerFacade;
|
||||
import com.heibaiying.springboot.bean.Programmer;
|
||||
import com.heibaiying.springboot.dao.ProgrammerDao;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
*/
|
||||
|
||||
@RestController
|
||||
public class ProgrammerController {
|
||||
|
||||
@Autowired
|
||||
private ProgrammerDao programmerDao;
|
||||
|
||||
@GetMapping("/programmers")
|
||||
public List<Programmer> get() {
|
||||
return programmerDao.selectAll();
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.heibaiying.springboot.dao;
|
||||
|
||||
import com.heibaiying.springboot.bean.Programmer;
|
||||
import org.apache.ibatis.annotations.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
*/
|
||||
@Mapper
|
||||
public interface ProgrammerDao {
|
||||
|
||||
|
||||
@Select("select * from programmer")
|
||||
List<Programmer> selectAll();
|
||||
|
||||
@Insert("insert into programmer (name, age, salary, birthday) VALUES (#{name}, #{age}, #{salary}, #{birthday})")
|
||||
void save(Programmer programmer);
|
||||
|
||||
@Select("select * from programmer where name = #{id}")
|
||||
Programmer selectById(int id);
|
||||
|
||||
@Update("update programmer set name=#{name},age=#{age},salary=#{salary},birthday=#{birthday} where id=#{id}")
|
||||
int modify(Programmer programmer);
|
||||
|
||||
@Delete(" delete from programmer where id = #{id}")
|
||||
void delete(int id);
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:mysql://127.0.0.1:3306/mysql?useUnicode=true&characterEncoding=utf-8
|
||||
username: root
|
||||
password: root
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
|
||||
# 使用 druid 作为连接池 更多配置的说明可以参见 druid starter 中文文档 https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
druid:
|
||||
# 初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时
|
||||
initialSize: 5
|
||||
# 最小连接池数量
|
||||
minIdle: 5
|
||||
# 最大连接池数量
|
||||
maxActive: 10
|
||||
# 获取连接时最大等待时间,单位毫秒。配置了maxWait之后,缺省启用公平锁,并发效率会有所下降,如果需要可以通过配置useUnfairLock属性为true使用非公平锁。
|
||||
maxWait: 60000
|
||||
# Destroy线程会检测连接的间隔时间,如果连接空闲时间大于等于minEvictableIdleTimeMillis则关闭物理连接。
|
||||
timeBetweenEvictionRunsMillis: 60000
|
||||
# 连接保持空闲而不被驱逐的最小时间
|
||||
minEvictableIdleTimeMillis: 300000
|
||||
# 用来检测连接是否有效的sql 因数据库方言而差, 例如 oracle 应该写成 SELECT 1 FROM DUAL
|
||||
validationQuery: SELECT 1
|
||||
# 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
|
||||
testWhileIdle: true
|
||||
# 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
|
||||
testOnBorrow: false
|
||||
# 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
|
||||
testOnReturn: false
|
||||
# 是否自动回收超时连接
|
||||
removeAbandoned: true
|
||||
# 超时时间(以秒数为单位)
|
||||
remove-abandoned-timeout: 180
|
||||
|
||||
# druid 监控的配置 如果不使用 druid 的监控功能的话 以下配置就不是必须的
|
||||
# 本项目监控台访问地址: http://localhost:8080/druid/login.html
|
||||
|
||||
# WebStatFilter用于采集web-jdbc关联监控的数据。
|
||||
# 更多配置可参见: https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_%E9%85%8D%E7%BD%AEWebStatFilter
|
||||
web-stat-filter:
|
||||
# 是否开启 WebStatFilter 默认是true
|
||||
enabled: true
|
||||
# 需要拦截的url
|
||||
url-pattern: /*
|
||||
# 排除静态资源的请求
|
||||
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
|
||||
|
||||
# Druid内置提供了一个StatViewServlet用于展示Druid的统计信息。
|
||||
# 更多配置可参见:https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_StatViewServlet%E9%85%8D%E7%BD%AE
|
||||
stat-view-servlet:
|
||||
#是否启用StatViewServlet 默认值true
|
||||
enabled: true
|
||||
# 需要拦截的url
|
||||
url-pattern: /druid/*
|
||||
# 允许清空统计数据
|
||||
reset-enable: true
|
||||
login-username: druid
|
||||
login-password: druid
|
||||
|
||||
|
||||
|
||||
# mybatis 相关配置
|
||||
mybatis:
|
||||
configuration:
|
||||
# 当没有为参数提供特定的 JDBC 类型时,为空值指定 JDBC 类型。
|
||||
# oracle数据库建议配置为JdbcType.NULL, 默认是Other
|
||||
jdbc-type-for-null: 'null'
|
||||
# 是否打印sql语句 调试的时候可以开启
|
||||
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
||||
|
@ -0,0 +1,8 @@
|
||||
-- 建表语句
|
||||
create table if not exists programmer (
|
||||
id int primary key auto_increment,
|
||||
name varchar(20),
|
||||
age tinyint,
|
||||
salary float,
|
||||
birthday datetime
|
||||
)
|
@ -0,0 +1,26 @@
|
||||
package com.heibaiying.springboot;
|
||||
|
||||
import com.heibaiying.springboot.bean.Programmer;
|
||||
import com.heibaiying.springboot.dao.ProgrammerDao;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class SpringBootDruidMybatisApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private ProgrammerDao programmerDao;
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
List<Programmer> programmers = programmerDao.selectAll();
|
||||
programmers.forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user