Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 16e707b
Showing
27 changed files
with
1,843 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Maven # | ||
target/ | ||
|
||
# IDEA # | ||
.idea/ | ||
*.iml | ||
|
||
# Eclipse # | ||
.settings/ | ||
.classpath | ||
.project | ||
|
||
# jrebel # | ||
src/main/resources/rebel.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#Spring Boot集成MyBatis的基础项目,项目使用Maven管理 | ||
|
||
#MyBatis3.3.0 | ||
|
||
#Spring Boot 1.3.0.RELEASE | ||
|
||
项目使用Spring Boot 1.3.0.RELEASE + Mybatis3.3.0 | ||
|
||
项目集成了Mybatis分页插件和通用Mapper插件 | ||
|
||
项目使用的mysql数据库,根据需要可以切换为其他数据库 | ||
|
||
#说明 | ||
|
||
虽然MyBatis官方提供了`mybatis-spring-boot-starter`,但是改配置的可以控制的地方太少,因此短时间不会直接使用该starter | ||
|
||
在集成MyBatis配置`MapperScannerConfigurer`需要特别注意,将该类单独放在一个配置文件中,例如本项目中的`MyBatisMapperScannerConfig`类: | ||
|
||
```java | ||
@Configuration | ||
//注意,由于MapperScannerConfigurer执行的比较早,所以必须有下面的注解 | ||
//MyBatisConfig.class是一个包含了SqlSessionFactory配置的类 | ||
@AutoConfigureAfter(MyBatisConfig.class) | ||
public class MyBatisMapperScannerConfig { | ||
|
||
@Bean | ||
public MapperScannerConfigurer mapperScannerConfigurer() { | ||
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); | ||
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory"); | ||
mapperScannerConfigurer.setBasePackage("tk.mybatis.springboot.mapper"); | ||
Properties properties = new Properties(); | ||
properties.setProperty("mappers", "tk.mybatis.mapper.MyMapper"); | ||
properties.setProperty("notEmpty", "false"); | ||
properties.setProperty("IDENTITY", "MYSQL"); | ||
//这里使用的通用Mapper的MapperScannerConfigurer,所有有下面这个方法 | ||
mapperScannerConfigurer.setProperties(properties); | ||
return mapperScannerConfigurer; | ||
} | ||
|
||
} | ||
``` | ||
|
||
##MyBatis工具 | ||
|
||
###http://www.mybatis.tk | ||
|
||
##推荐使用Mybatis通用Mapper3 | ||
|
||
###https://github.com/abel533/Mapper | ||
|
||
##推荐使用Mybatis分页插件PageHelper | ||
|
||
###https://github.com/pagehelper/Mybatis-PageHelper | ||
|
||
##作者信息 | ||
|
||
- 作者博客:http://blog.csdn.net/isea533 | ||
|
||
- 作者邮箱:abel533@gmail.com | ||
|
||
- Mybatis工具群: 211286137 (Mybatis相关工具插件等等) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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>tk.mybatis</groupId> | ||
<artifactId>mybatis-spring-boot</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>1.3.0.RELEASE</version> | ||
</parent> | ||
|
||
<properties> | ||
<java.version>1.8</java.version> | ||
<!-- 依赖版本 --> | ||
<mybatis.version>3.3.0</mybatis.version> | ||
<mapper.version>3.3.2</mapper.version> | ||
<pagehelper.version>4.0.3</pagehelper.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-jdbc</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-aop</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-freemarker</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>druid</artifactId> | ||
<version>1.0.11</version> | ||
</dependency> | ||
|
||
<!--Mybatis--> | ||
<dependency> | ||
<groupId>org.mybatis</groupId> | ||
<artifactId>mybatis</artifactId> | ||
<version>${mybatis.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mybatis</groupId> | ||
<artifactId>mybatis-spring</artifactId> | ||
<version>1.2.3</version> | ||
</dependency> | ||
<!-- Mybatis Generator --> | ||
<dependency> | ||
<groupId>org.mybatis.generator</groupId> | ||
<artifactId>mybatis-generator-core</artifactId> | ||
<version>1.3.2</version> | ||
<scope>compile</scope> | ||
<optional>true</optional> | ||
</dependency> | ||
<!--分页插件--> | ||
<dependency> | ||
<groupId>com.github.pagehelper</groupId> | ||
<artifactId>pagehelper</artifactId> | ||
<version>${pagehelper.version}</version> | ||
</dependency> | ||
<!--通用Mapper--> | ||
<dependency> | ||
<groupId>tk.mybatis</groupId> | ||
<artifactId>mapper</artifactId> | ||
<version>${mapper.version}</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>springloaded</artifactId> | ||
<version>1.2.5.RELEASE</version> | ||
</dependency> | ||
</dependencies> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2014-2016 abel533@gmail.com | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package tk.mybatis.mapper; | ||
|
||
import tk.mybatis.mapper.common.Mapper; | ||
import tk.mybatis.mapper.common.MySqlMapper; | ||
|
||
/** | ||
* 继承自己的MyMapper | ||
* | ||
* @author liuzh | ||
* @since 2015-09-06 21:53 | ||
*/ | ||
public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> { | ||
//TODO | ||
//FIXME 特别注意,该接口不能被扫描到,否则会出错 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package tk.mybatis.springboot; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.builder.SpringApplicationBuilder; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; | ||
import org.springframework.web.servlet.config.annotation.EnableWebMvc; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; | ||
import tk.mybatis.springboot.conf.WebMvcConfig; | ||
|
||
/** | ||
* @author liuzh | ||
* @since 2015-12-12 18:22 | ||
*/ | ||
@Controller | ||
@EnableWebMvc | ||
@ComponentScan | ||
@EnableAutoConfiguration | ||
public class Application extends WebMvcConfigurerAdapter { | ||
|
||
@RequestMapping("/") | ||
String home() { | ||
//return "Spring-Boot集成 MyBatis分页插件和通用Mapper"; | ||
return "redirect:countries"; | ||
} | ||
|
||
public static void main(String[] args) { | ||
// new SpringApplicationBuilder().main(Application.class).build().run(args); | ||
SpringApplication.run(Application.class, args); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/tk/mybatis/springboot/conf/DataSourceConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package tk.mybatis.springboot.conf; | ||
|
||
import com.alibaba.druid.pool.DruidDataSource; | ||
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.annotation.Order; | ||
|
||
import javax.sql.DataSource; | ||
|
||
/** | ||
* 数据源 | ||
* | ||
* @author liuzh | ||
* @since 2015-12-19 10:06 | ||
*/ | ||
@Configuration | ||
public class DataSourceConfig { | ||
|
||
@Bean | ||
@ConfigurationProperties(prefix = "spring.druid") | ||
public DataSource dataSource() { | ||
return DataSourceBuilder.create().type(DruidDataSource.class).build(); | ||
} | ||
|
||
} |
104 changes: 104 additions & 0 deletions
104
src/main/java/tk/mybatis/springboot/conf/MyBatisConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2014-2016 abel533@gmail.com | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package tk.mybatis.springboot.conf; | ||
|
||
import com.github.pagehelper.PageHelper; | ||
import org.apache.ibatis.plugin.Interceptor; | ||
import org.apache.ibatis.session.SqlSessionFactory; | ||
import org.mybatis.spring.SqlSessionFactoryBean; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
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.core.io.support.ResourcePatternResolver; | ||
import tk.mybatis.mapper.entity.Config; | ||
|
||
import javax.sql.DataSource; | ||
import java.io.IOException; | ||
import java.util.Properties; | ||
|
||
/** | ||
* MyBatis基础配置 | ||
* | ||
* @author liuzh | ||
* @since 2015-12-19 10:11 | ||
*/ | ||
@Configuration | ||
public class MyBatisConfig { | ||
|
||
@Autowired | ||
DataSource dataSource; | ||
|
||
@Autowired | ||
SqlSessionFactoryBean sqlSessionFactoryBean; | ||
|
||
@Autowired | ||
SqlSessionFactory sqlSessionFactory; | ||
|
||
@Bean | ||
@ConfigurationProperties(prefix = "mybatis.mapper") | ||
public Config mapperConfig() { | ||
return new Config(); | ||
} | ||
|
||
@Bean(name = "sqlSessionFactory") | ||
public SqlSessionFactoryBean sqlSessionFactoryBean() { | ||
SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); | ||
bean.setDataSource(dataSource); | ||
bean.setTypeAliasesPackage("tk.mybatis.springboot.model"); | ||
|
||
//分页插件 | ||
PageHelper pageHelper = new PageHelper(); | ||
Properties properties = new Properties(); | ||
properties.setProperty("reasonable", "true"); | ||
properties.setProperty("supportMethodsArguments", "true"); | ||
properties.setProperty("returnPageInfo", "check"); | ||
properties.setProperty("params", "count=countSql"); | ||
pageHelper.setProperties(properties); | ||
|
||
//添加插件 | ||
bean.setPlugins(new Interceptor[]{pageHelper}); | ||
|
||
//添加XML目录 | ||
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); | ||
try { | ||
bean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml")); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return bean; | ||
} | ||
|
||
@Bean | ||
public SqlSessionFactory sqlSessionFactory() { | ||
try { | ||
return sqlSessionFactoryBean.getObject(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
Oops, something went wrong.