寫在前面html
springboot與mybatis整合及其簡單,與傳統SSM框架整合少了不少煩人的配置,springboot與mybatis整合只需導入mybatis包,這樣springboot就會幫咱們自動配置及整合了。下面直接上所需代碼:java
---------------------方法一:使用mybatis官方提供的Spring Boot整合包實現---------------------mysql
1 maven 配置web
<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>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <!-- spring-boot整合mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!-- mysql驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency>
2 mapper接口掃描配置 在啓動類配置spring
@SpringBootApplication @MapperScan("com.tools.*.mapper") public class ToolsApp { public static void main( String[] args ) { SpringApplication.run(ToolsApp.class, args); } }
或者直接配置mapper 註解sql
package cn.tool.mapper; import java.util.List; import org.apache.ibatis.annotations.Mapper; import cn.qlq.bean.User; @Mapper public interface UserMapper { public List<User> findAll(); }
3 mapper.xml文件配置數據庫
###mybatis
mybatis.mapper-locations=classpath:mapper/com/jiuying/small/*/*Mapper.xml
mybatis.config-location=classpath:config/sqlMapConfig.xml
4.mybatis全局配置文件:apache
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
5 mapper 文件api
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="cn.qlq.mapper.UserMapper"> <select id="findAll" resultType="user"> select * from user </select> </mapper>
---------------方法二:使用mybatis-spring整合的方式,也就是咱們傳統的方式--------------springboot
這裏咱們推薦使用第二種,由於這樣咱們能夠很方便的控制Mybatis的各類配置。
首先,建立一個Mybatis的配置類:
代碼:
package cn.qlq.config; import javax.sql.DataSource; import org.mybatis.spring.SqlSessionFactoryBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; @Configuration public class MyBatisConfig { @Bean @ConditionalOnMissingBean //當容器裏沒有指定的Bean的狀況下建立該對象 public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); // 設置數據源 sqlSessionFactoryBean.setDataSource(dataSource); // 設置mybatis的主配置文件 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); Resource mybatisConfigXml = resolver.getResource("classpath:mybatis/SqlMapConfig.xml"); sqlSessionFactoryBean.setConfigLocation(mybatisConfigXml); // 設置別名包 sqlSessionFactoryBean.setTypeAliasesPackage("cn.qlq.bean"); return sqlSessionFactoryBean; } }
而後,建立Mapper接口的掃描類MapperScannerConfig:
package cn.qlq.config; import org.mybatis.spring.mapper.MapperScannerConfigurer; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @AutoConfigureAfter(MyBatisConfig.class) //保證在MyBatisConfig實例化以後再實例化該類 public class MapperScannerConfig { // mapper接口的掃描器 @Bean public MapperScannerConfigurer mapperScannerConfigurer() { MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); mapperScannerConfigurer.setBasePackage("cn.qlq.mapper"); return mapperScannerConfigurer; } }
建立一個spring配置類,掃描的包,與讀取的資源文件,建立數據源:
package cn.qlq.config; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import com.jolbox.bonecp.BoneCPDataSource; @Configuration // 經過該註解來代表該類是一個Spring的配置,至關於一個xml文件 @ComponentScan(basePackages = "cn.qlq") // 配置掃描包 @PropertySource(value = { "classpath:db.properties"}, ignoreResourceNotFound = true) public class SpringConfig { @Value("${jdbc.url}") private String jdbcUrl; @Value("${jdbc.driverClassName}") private String jdbcDriverClassName; @Value("${jdbc.username}") private String jdbcUsername; @Value("${jdbc.password}") private String jdbcPassword; @Bean(destroyMethod = "close") public DataSource dataSource() { BoneCPDataSource boneCPDataSource = new BoneCPDataSource(); // 數據庫驅動 boneCPDataSource.setDriverClass(jdbcDriverClassName); // 相應驅動的jdbcUrl boneCPDataSource.setJdbcUrl(jdbcUrl); // 數據庫的用戶名 boneCPDataSource.setUsername(jdbcUsername); // 數據庫的密碼 boneCPDataSource.setPassword(jdbcPassword); // 檢查數據庫鏈接池中空閒鏈接的間隔時間,單位是分,默認值:240,若是要取消則設置爲0 boneCPDataSource.setIdleConnectionTestPeriodInMinutes(60); // 鏈接池中未使用的連接最大存活時間,單位是分,默認值:60,若是要永遠存活設置爲0 boneCPDataSource.setIdleMaxAgeInMinutes(30); // 每一個分區最大的鏈接數 boneCPDataSource.setMaxConnectionsPerPartition(100); // 每一個分區最小的鏈接數 boneCPDataSource.setMinConnectionsPerPartition(5); return boneCPDataSource; } }
application.properties:
server.port=80 logging.level.org.springframework=DEBUG #springboot mybatis #jiazai mybatis peizhiwenjian #mybatis.mapper-locations = classpath:mapper/*Mapper.xml #mybatis.config-location = classpath:mapper/config/sqlMapConfig.xml #mybatis.type-aliases-package = cn.qlq.bean #shujuyuan spring.datasource.driver-class-name= com.mysql.jdbc.Driver spring.datasource.url = jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8 spring.datasource.username = root spring.datasource.password = 123456
pom.xml:
<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> <groupId>cn.qlq</groupId> <artifactId>springboot-ssm</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencies> <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>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <!-- spring-boot整合mybatis --> <!-- <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> --> <!-- mysql驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.7.RELEASE</version> </dependency> <!-- 基本配置 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> <!-- Mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.7</version> </dependency> <!-- Mybatis整合Spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <!-- 鏈接池 --> <dependency> <groupId>com.jolbox</groupId> <artifactId>bonecp-spring</artifactId> <version>0.8.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency> </dependencies> <build> <!-- 配置了不少插件 --> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.啓動測試:
參考:https://www.cnblogs.com/qlqwjy/p/8463822.html