spring-boot 註解配置mybatis+druid(新手上路)



以前已經介紹了新手如何搭建一個簡單的springboot,接下來介紹一下springboot+mybatis+druid的配置方法。css
鄙人不才,參考了一些高手的博文以及官方文檔,整理出了一點心得。html
對高手的感謝無以言表,原文地址:http://blog.csdn.net/lxhjh/article/details/51764604java
http://blog.csdn.net/xiaoyu411502/article/details/51392237mysql
使用環境:JDK1.8 、eclipse-neon、maven3.5+、mysql5.5git
框架使用:springboot1.4.一、druid1.0.2六、mybatis自動獲取版本號web
介紹:我使用的配置文件格式爲application.properties,基本都是使用註解的方式搭建。spring
數據庫表結構,databases名爲mydatabases;代表爲city,字段有三個,分別是id、name、province,數據類型是varchar(20)。sql
第一步:數據庫
開始仍是新建springboot項目,而後導包。apache
新建maven項目,選擇jar包。
配置pom.xml文件。
- <properties>
- <java.version>1.8</java.version>
- </properties>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.4.1.RELEASE</version>
- </parent>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.0.26</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>1.1.1</version>
- </dependency>
-
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- <optional>true</optional>
- </dependency>
- </dependencies>
第二步:
建立啓動類。
- import org.mybatis.spring.annotation.MapperScan;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.boot.web.servlet.ServletComponentScan;
-
- @SpringBootApplication
- @ServletComponentScan
- @MapperScan("mapper")<span style="white-space:pre"> </span>
- public class Application {
- public static void main(String[] args) {
- SpringApplication.run(Application.class, args);
- }
- }
第三步:
填寫配置文件。(這裏呀,參數比較多,能夠選擇填寫。)
我使用的是application.properties文件,該文件放在src/main/resources目錄下,springboot框架啓動後會自動讀取它。
- #數據庫設置
- spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
- spring.datasource.driverClassName=com.mysql.jdbc.Driver
- spring.datasource.url=jdbc:mysql://localhost:3306/mydatabases
- spring.datasource.username=root
- spring.datasource.password=123
- #--------------------------
- # 下面爲鏈接池的補充設置,應用到上面全部數據源中
- # 初始化大小,最小,最大
- spring.datasource.initialSize=5
- spring.datasource.minIdle=5
- spring.datasource.maxActive=20
- # 配置獲取鏈接等待超時的時間
- spring.datasource.maxWait=60000
- # 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒
- spring.datasource.timeBetweenEvictionRunsMillis=60000
- # 配置一個鏈接在池中最小生存的時間,單位是毫秒
- spring.datasource.minEvictableIdleTimeMillis=300000
- spring.datasource.validationQuery=SELECT 1 FROM DUAL
- spring.datasource.testWhileIdle=true
- spring.datasource.testOnBorrow=false
- spring.datasource.testOnReturn=false
- # 打開PSCache,而且指定每一個鏈接上PSCache的大小
- spring.datasource.poolPreparedStatements=true
- spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
- # 配置監控統計攔截的filters,去掉後監控界面sql沒法統計,'wall'用於防火牆
- spring.datasource.filters=stat,wall,log4j
- # 經過connectProperties屬性來打開mergeSql功能;慢SQL記錄
- spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
- # 合併多個DruidDataSource的監控數據
- #spring.datasource.useGlobalDataSourceStat=true
第四步:
配置數據源。這裏相關的參數會自動賦值到datasource裏。
- import javax.sql.DataSource;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- import com.alibaba.druid.pool.DruidDataSource;
- @Configuration
- public class DruidDataSourceConfiguration {
-
- @Bean
- @ConfigurationProperties(prefix = "spring.datasource")
- public DataSource druidDataSource() {
- DruidDataSource druidDataSource = new DruidDataSource();
- return druidDataSource;
- }
- }
第五步:
寫一個實體類。這裏能夠隨意哈。
- import java.io.Serializable;
-
- public class City implements Serializable{
- private static final long serialVersionUID = 1L;
- private String id;
- private String name;
- private String province;
- City(){
-
- }
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getProvince() {
- return province;
- }
- public void setProvince(String province) {
- this.province = province;
- }
- @Override
- public String toString() {
- return "City [id=" + id + ", name=" + name + ", province=" + province + "]";
- }
-
- }
第六步:
寫mapper文件。這裏的包地址爲Application的MapperScan的值。
- import org.apache.ibatis.annotations.Param;
- import org.apache.ibatis.annotations.Select;
-
- public interface CityMapper {
-
- @Select("select * from city where id = #{id}")
- City findCityById(@Param("id") String id);
- }
第七步:
這裏就不寫Service了,直接在Controller裏寫了。
- import javax.annotation.Resource;
-
- import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- @RequestMapping("/demo")
- @EnableAutoConfiguration
- public class HelloController {
- @Resource
- private CityMapper cityMapper;
- @RequestMapping("/test")
- String test1(){
- return "hello,test1()";
- }
- @RequestMapping("/findCity2")
- City findCity2(@RequestParam String id){
- return cityMapper.findCityById(id);
- }
- }
第八步:
這裏要寫filter,配合druid監控的使用。
- import javax.servlet.annotation.WebFilter;
- import javax.servlet.annotation.WebInitParam;
-
- import com.alibaba.druid.support.http.WebStatFilter;
-
- @WebFilter(filterName="druidWebStatFilter",urlPatterns="/*",
- initParams={
- @WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")
- }
- )
- public class DruidStatFilter extends WebStatFilter{
-
- }
第九步:
配置監控界面。
- import com.alibaba.druid.support.http.StatViewServlet;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.annotation.WebInitParam;
- @WebServlet(urlPatterns = "/druid/*",
- initParams={
- @WebInitParam(name="allow",value="192.168.16.110,127.0.0.1"),
- @WebInitParam(name="deny",value="192.168.16.111"),
- @WebInitParam(name="loginUsername",value="admin"),
- @WebInitParam(name="loginPassword",value="123"),
- @WebInitParam(name="resetEnable",value="false")
- })
- public class DruidStatViewServlet extends StatViewServlet {
- private static final long serialVersionUID = 1L;
-
- }
第十步:
啓動測試。在application.java中啓動,瀏覽器中先進入:localhost:8080/druid/login.html,輸入用戶名:admin,密碼:123,進入。
再執行一下localhost:8080/demo/findCity2?id=001,(你的數據)查看結果。這裏須要你的數據庫裏有數據。
在監控頁面看看sql監控是否被監控到。
附上源碼:點擊打開連接
肚子餓。。。