從事java開發的小夥伴們,相信對SSM以及SSH都不陌生,這些框架在SpringBoot沒有出現以前是很是火的,隨着技術的不斷髮展,愈來愈多的技術涌現出來,哪怕Web層的框架也愈來愈多,如今的新型創業公司中,也是使用的是新型的技術。我在這邊就簡單的爲小夥伴介紹下springboot結合mybatis。java
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<mybatis-spring-boot-starter>1.3.0</mybatis-spring-boot-starter>
<druid.version>1.1.0</druid.version>
<pagehelper.version>1.1.2</pagehelper.version>
</properties>
<dependencyManagement>
<dependencies>
*****你本身的服務依賴
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot-starter}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</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>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
</dependency>
<!-- 數據庫鏈接 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 分頁插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${pagehelper.version}</version>
</dependency>
<!-- alibaba的druid數據庫鏈接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
</dependencies>
複製代碼
上面是parent裏面的pom依賴關係,子項目中所須要的依賴也能夠在這裏面定義版本。這樣咱們對一個springboot項目的話,依賴更爲清晰。mysql
那麼接下來就是配置文件相關的配置:git
server.port=8080
#session time(s)
server.session.timeout=1800
spring.thymeleaf.cache=false
#datasource
spring.datasource.name=dbName
spring.datasource.url=jdbc\:mysql\://ip:3306/dbName?characterEncoding=utf-8&useUnicode=true&useSSL=false
spring.datasource.username=****montos****
spring.datasource.password=****montos****
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.filters=stat
spring.datasource.maxActive=20
spring.datasource.initialSize=1
spring.datasource.maxWait=60000
spring.datasource.minIdle=1
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=select 'x'
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxOpenPreparedStatements=20
#pagehelper
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
#mybatis log
mybatis.configuration.log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
複製代碼
這上面就簡單的配置了數據源以及分頁相關的設置,在這裏就很少說了。 接下就是代碼裏面的相關配置了。 這裏是基於mybatis註解形式開發,spring中須要加載mybatis持久層Bean。github
package com.montos.service;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.montos.dao")
public class WarehouseStarterApplication {
public static void main(String[] args) {
SpringApplication.run(WarehouseStarterApplication.class, args);
}
}
複製代碼
這裏面的@MapperScan
掃描的是dao中接口的包路徑。這樣咱們就能將咱們須要的Bean掃描到容器中。(固然,直接在dao上面加上@Mapper
也是能夠實現的,不過這裏須要注意的是,dao層的包名須要是在StarterApplication類下面,否則會出現相關dao接口掃描不到的狀況)web
接下來就是持久層相關的操做。 UserMapper:spring
@Delete("delete from User where id = #{id}")
int deleteByPrimaryKey(String id);
@SelectKey(keyProperty = "user.id", resultType = String.class, before = true, statement = "select replace(uuid(), '-', '')")
@Insert("insert into user (id, name) values (#{user.id},#{user.name}")
@Options(keyProperty = "user.id", useGeneratedKeys = true)
int insert(@Param("user") User user);
@Select("select * from User where id = #{id}")
User selectByPrimaryKey(String id);
@Update("update User set name = #{name} where id = #{id}")
int updateByPrimaryKey(User user);
複製代碼
上面就簡單的對CRUD操做進行一個簡單的介紹。 service層中和調用以前的持久層同樣,注入一個持久層的Bean,而後進行調用。sql
這上面也只是對mybatis的基礎使用進行了介紹,有問題的小夥伴記得留言哦~數據庫