Spring Boot整合Mybatis、和其他
废话不多说直接开始:
目录:
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>com.zto.springboot-mybatis</groupId>
<artifactId>zto-springboot-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.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>
</properties>
<!-- Additional lines to be added here... -->
<dependencies>
<!-- 对常用测试依赖的支持,包括junit,hamcrest和Mockito,海域sping-test模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 核心sping Boot starter,包含自动配置支持,日志和YAML -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 对redis键值数据存储的支持,包括spring-redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
<!-- 对全栈web开发的支持,包括tomcat和spring-webmbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot对缓存的支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- springBoot对jdbc数据库的支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- springBoot的默认日志系统lockback -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<!-- 面向切面变成的支持,包括spring-aop和Aspectj -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<!-- mybatis和spring的整合 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.5</version>
</dependency>
<!-- oracle -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- maven逆向工程插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.yml
---
logging:
level:
root: INFO
server:
port: 8081
spring:
datasource:
zttb:
type: com.alibaba.druid.pool.DruidDataSource
url: 。。。
username: 。。。
password:。。。
driver-class-name: oracle.jdbc.driver.OracleDriver
minIdle: 1
maxActive: 2
initialSize: 1
timeBetweenEvictionRunsMillis: 3000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 'ZTM' FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
DatabaseConfig.java
package com.zto.springboot.mybatis.config;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import com.alibaba.druid.pool.DruidDataSource;
/**
*
* @ClassName: DatabaseConfig
* @Description: mybatis相关配置
* @author: xyc
* @date: 2016年11月10日 下午4:55:54
*
*/
@Component
@Configuration
public class DatabaseConfig {
/**
*
* @Title: getDataSource
* @Description: 数据源的配置
* @param: @return
* @return: DataSource
* @throws
*/
@Bean
@ConfigurationProperties(prefix = "spring.datasource.zttb")
public DataSource getDataSource() {
return new DruidDataSource();
}
/**
*
* @Title: sqlSessionFactory
* @Description: mybatis的sqlSessionFactory配置
* @param: @param dataSource
* @param: @return
* @param: @throws Exception
* @return: SqlSessionFactory
* @throws
*/
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource)
throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean
.setMapperLocations(resolver
.getResources("classpath*:com/zto/springboot/mybatis/mapper/*Mapper.xml"));
sqlSessionFactoryBean
.setTypeAliasesPackage("com.zto.springboot.mybatis.entity");
return sqlSessionFactoryBean.getObject();
}
/**
*
* @Title: mapperScannerConfigurer
* @Description: mapper接口扫描包
* @param: @return
* @return: MapperScannerConfigurer
* @throws
*/
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer
.setBasePackage("com.zto.springboot.mybatis.mapper");
return mapperScannerConfigurer;
}
/**
*
* @Title: transactionManager
* @Description: 配置事务管理器
* @param: @param dataSource
* @param: @return
* @param: @throws Exception
* @return: DataSourceTransactionManager
* @throws
*/
@Bean
public DataSourceTransactionManager transactionManager(DataSource dataSource)
throws Exception {
return new DataSourceTransactionManager(dataSource);
}
}
ZtoGroupStoresController.java
package com.zto.springboot.mybatis.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zto.springboot.mybatis.entity.ZtoGroupStores;
import com.zto.springboot.mybatis.service.IZtoGroupStoresService;
@RestController
public class ZtoGroupStoresController {
@Autowired
private IZtoGroupStoresService storesService;
@RequestMapping("/getStoresInfo")
public List<ZtoGroupStores> getStoresInfo(String companyCode,String hallCode){
List<ZtoGroupStores> groupStores = storesService.selectByCompanyCodeAndHallCode(companyCode,hallCode);
return groupStores;
}
}
Application.java
package com.zto.springboot.mybatis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@EnableCaching
@EnableConfigurationProperties
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class })
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
try {
Thread.sleep(Integer.MAX_VALUE);
} catch (Exception e) {
System.exit(1);
}
}
}
实体、Mapper、service层就不贴了,直接调用就行...
启动服务:就可以访问到了....
项目放到码云上面:https://git.oschina.net/yuchao521/zto-springboot-mybatis
其他的整合就不说了,因为网上已经有很多了,我就把自己实现的项目放到码云上面:
springboot整合google Guava和redis实现cache:
http://git.oschina.net/yuchao521/springboot-cache
springboot整合redis:
http://git.oschina.net/yuchao521/springboot-redis
springboot整合dubbo:
http://git.oschina.net/yuchao521/springboot-dubbo
springboot整合rocketmq:
http://git.oschina.net/yuchao521/springboot-demo
springboot整合调度
http://git.oschina.net/yuchao521/springboot-schedule
springboot整合tbschedule
http://git.oschina.net/yuchao521/zto-schedule