SpringBoot整合Mybatis(分层整合)
来源:互联网 发布:优酷淘宝搞笑视频 编辑:程序博客网 时间:2024/06/05 16:09
项目源码:http://download.csdn.net/detail/a295277302/9905817
如果用eclipse打开的话,控制台运行mvn eclipse:eclipse
如果用idea打开的话,控制台运行mvn idea:idea
项目结构
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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>springboot</groupId> <artifactId>springboot</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>springboot Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </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> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</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>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/tk.mybatis/mapper-spring-boot-starter --> <!--Swagger start--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.2.2</version> </dependency> <!--Swagger end--> <!--Mybatis start--> <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> <version>5.1.21</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.3</version> </dependency> <dependency> <groupId>org.mybatis.caches</groupId> <artifactId>mybatis-ehcache</artifactId> <version>1.0.3</version> </dependency> <!-- https://mvnrepository.com/artifact/tk.mybatis/mapper --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>3.4.1</version> </dependency> <!--Mybatis end--> <!--ehcache start--> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-web</artifactId> <version>2.0.4</version> </dependency> <!--ehcache end--> <!-- druid start--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.29</version> </dependency> <!-- druid end--> <!-- shiro spring. --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.2.2</version> </dependency> <!-- shiro ehcache --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <version>1.2.2</version> </dependency> <!--整合Redis start--> <!--<dependency>--> <!--<groupId>org.springframework.boot</groupId>--> <!--<artifactId>spring-boot-starter-redis</artifactId>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>org.springframework.data</groupId>--> <!--<artifactId>spring-data-redis</artifactId>--> <!--<version>1.6.5.RELEASE</version>--> <!--</dependency>--> <!--redis end--> <!--poi start--> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.16</version> </dependency> <!--<!– https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas –>--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.16</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.16</version> </dependency> <!--poi end--> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.3.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
========================================================
设置sqlSessionFactorypackage com.htf.conf;import java.util.Properties;import javax.annotation.PostConstruct;import javax.sql.DataSource;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.ibatis.plugin.Interceptor;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.SqlSessionTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.autoconfigure.AutoConfigureAfter;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.DefaultResourceLoader;import org.springframework.core.io.Resource;import org.springframework.core.io.ResourceLoader;import org.springframework.util.Assert;import org.springframework.util.StringUtils;import com.github.pagehelper.PageHelper;/** * Created by Administrator on 2017/7/20 0020. */@Configuration@ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class })//@ConditionalOnBean(DataSource.class)@EnableConfigurationProperties(MybatisProperties.class)@AutoConfigureAfter(DataSourceAutoConfiguration.class)public class MybatisAutoConfiguration { private static Log log = LogFactory.getLog(MybatisAutoConfiguration.class); @Autowired private MybatisProperties properties; @Autowired(required = false) private Interceptor[] interceptors; @Autowired private ResourceLoader resourceLoader = new DefaultResourceLoader(); @PostConstruct public void checkConfigFileExists() { if (this.properties.isCheckConfigLocation()) { Resource resource = this.resourceLoader .getResource(this.properties.getConfig()); Assert.state(resource.exists(), "Cannot find config location: " + resource + " (please add config file or check your Mybatis " + "configuration)"); } } @Bean(name = "sqlSessionFactory") @ConditionalOnMissingBean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean factory = new SqlSessionFactoryBean(); factory.setDataSource(dataSource); if (StringUtils.hasText(this.properties.getConfig())) { factory.setConfigLocation( this.resourceLoader.getResource(this.properties.getConfig())); } else { if (this.interceptors != null && this.interceptors.length > 0) { factory.setPlugins(this.interceptors); } factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage()); factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage()); factory.setMapperLocations(this.properties.getMapperLocations()); } return factory.getObject(); } @Bean @ConditionalOnMissingBean public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory, this.properties.getExecutorType()); } /** * 分页插件 * * @param dataSource * @return * @author SHANHY * @create 2016年2月18日 */ @Bean public PageHelper pageHelper(DataSource dataSource) { log.info("注册MyBatis分页插件PageHelper"); PageHelper pageHelper = new PageHelper(); Properties p = new Properties(); p.setProperty("offsetAsPageNum", "true"); p.setProperty("rowBoundsWithCount", "true"); p.setProperty("reasonable", "true"); pageHelper.setProperties(p); return pageHelper; }}
=================================MyBatisMapperScannerConfig ===============
package com.htf.conf;import org.springframework.boot.autoconfigure.AutoConfigureAfter;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import tk.mybatis.spring.mapper.MapperScannerConfigurer;import java.util.Properties;/** * Created by Administrator on 2017/7/20 0020. */@Configuration//TODO 注意,由于MapperScannerConfigurer执行的比较早,所以必须有下面的注解@AutoConfigureAfter(MybatisAutoConfiguration.class)public class MyBatisMapperScannerConfig { @Bean public MapperScannerConfigurer mapperScannerConfigurer() { MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory"); mapperScannerConfigurer.setBasePackage("com.htf.mapper"); Properties properties = new Properties(); // 这里要特别注意,不要把MyMapper放到 basePackage 中,也就是不能同其他Mapper一样被扫描到。 properties.setProperty("mappers", MyMapper.class.getName()); properties.setProperty("notEmpty", "false"); properties.setProperty("IDENTITY", "MYSQL"); mapperScannerConfigurer.setProperties(properties); return mapperScannerConfigurer; }}
=======================================MybatisProperties ============================
package com.htf.conf;import org.apache.ibatis.session.ExecutorType;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.core.io.Resource;/** * Created by Administrator on 2017/7/20 0020. */@ConfigurationProperties(prefix = MybatisProperties.MYBATIS_PREFIX)public class MybatisProperties { public static final String MYBATIS_PREFIX = "mybatis"; /** * Config file path. */ private String config; /** * Location of mybatis mapper files. */ private Resource[] mapperLocations; /** * Package to scan domain objects. */ private String typeAliasesPackage; /** * Package to scan handlers. */ private String typeHandlersPackage; /** * Check the config file exists. */ private boolean checkConfigLocation = false; /** * Execution mode. */ private ExecutorType executorType = ExecutorType.SIMPLE; public String getConfig() { return this.config; } public void setConfig(String config) { this.config = config; } public Resource[] getMapperLocations() { return this.mapperLocations; } public void setMapperLocations(Resource[] mapperLocations) { this.mapperLocations = mapperLocations; } public String getTypeHandlersPackage() { return this.typeHandlersPackage; } public void setTypeHandlersPackage(String typeHandlersPackage) { this.typeHandlersPackage = typeHandlersPackage; } public String getTypeAliasesPackage() { return this.typeAliasesPackage; } public void setTypeAliasesPackage(String typeAliasesPackage) { this.typeAliasesPackage = typeAliasesPackage; } public boolean isCheckConfigLocation() { return this.checkConfigLocation; } public void setCheckConfigLocation(boolean checkConfigLocation) { this.checkConfigLocation = checkConfigLocation; } public ExecutorType getExecutorType() { return this.executorType; } public void setExecutorType(ExecutorType executorType) { this.executorType = executorType; }}
=======================================MyMapper==========================================
package com.htf.conf;import tk.mybatis.mapper.common.Mapper;import tk.mybatis.mapper.common.MySqlMapper;/** * 被继承的Mapper,一般业务Mapper继承它 * */public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> { //TODO //FIXME 特别注意,该接口不能被扫描到,否则会出错}
====================================================================================
mybatis.mapper-locations=classpath*:mapper/*Mapper.xmlmybatis.type-aliases-package=com.htf.entity
配置文件添加上面的配置,扫描本地的Mapper.xml 和实体类===============================UserController======================================
@RestController@RequestMapping("/users")public class UserController { @Autowired private UserService userService; @RequestMapping(value="/{id}", method= RequestMethod.GET) public User getUserById(@PathVariable Integer id){ return userService.getUserById(id); }}
通过浏览器访问,返回JSON数据
阅读全文
0 1
- SpringBoot整合Mybatis(分层整合)
- SpringBoot和Mybatis整合
- springboot 整合mybatis
- springboot+mybatis+dubbo整合
- SpringBoot整合MyBatis
- SpringBoot整合Mybatis
- springBoot整合mybatis
- springboot与mybatis整合
- springboot和mybatis整合
- Springboot整合Mysql+Mybatis
- springboot、mybatis、activemq整合
- SpringBoot整合MyBatis
- SpringBoot 整合mybatis
- SpringBoot + Mybatis 整合demo
- springboot整合mybatis
- SpringBoot-整合MyBatis
- Springboot 整合 Mybatis
- SpringBoot-09整合MyBatis
- 有关jQueryEasyUI的form的submit()方法的小问题
- C++程序员学Java系列之二三:匿名对象
- 开篇
- Ubuntu16.04+Windows10双系统 显示正确时间
- Fidder抓包
- SpringBoot整合Mybatis(分层整合)
- spring boot 2
- hdu 1247 字典树
- Spring Cloud-简介
- 数组array和集合的区别:
- 二叉搜索树的后序遍历序列
- CSDN 记录新的开始 20170720
- 数码时钟
- 复杂链表的复制