官網說明css
首先 pom文件html
<?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>club.lemos</groupId> <artifactId>springboot-mybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot-mybatis</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.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-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-solr</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.27</version> </dependency> <!-- mybatis的分頁插件--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.2.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置文件(application.properties)java
#tomcat配置 server.port=9090 server.contextPath=/ #源端口 #source.port = 8089 #使用@Value("${author.name}")獲取 ___test #author.name = seven sins #使用@ConfigurationProperties接收整個對象, 使用時用@Autowired注入 ___test #admin.userName = admin #admin.passWord = 123 #指定環境 #spring.profiles.active = dev #spring.http.encoding #spring.http.encoding.enable = true spring.http.encoding.charset = UTF-8 spring.http.encoding.force = true #視圖解析配置 #spring.mvc.view.prefix = /WEB-INF/views/ #spring.mvc.view.suffix = .html #關閉Favicon #如須要設置本身的Favicon, 只須要將本身的favicon.ico文件放在resources或static下 spring.mvc.favicon.enabled = false #關閉freemarker模板緩存 spring.freemarker.cache =false #dataSource spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver # 下面爲鏈接池的補充設置,應用到上面全部數據源中 # 初始化大小,最小,最大 spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive=20 # 配置獲取鏈接等待超時的時間 spring.datasource.maxWait=60000 # 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 spring.datasource.timeBetweenEvictionRunsMillis=60000 # 配置一個鏈接在池中最小生存的時間,單位是毫秒 spring.datasource.minEvictableIdleTimeMillis=300000 spring.datasource.validationQuery=SELECT 1 FROM DUAL spring.datasource.testWhileIdle=true spring.datasource.testOnBorrow=false spring.datasource.testOnReturn=false # 打開PSCache,而且指定每一個鏈接上PSCache的大小 spring.datasource.poolPreparedStatements=true spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 # 配置監控統計攔截的filters spring.datasource.filters=stat,wall,log4j # 經過connectProperties屬性來打開mergeSql功能;慢SQL記錄 spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 # 合併多個DruidDataSource的監控數據 spring.datasource.useGlobalDataSourceStat=true #日誌配置 #logging.file=log.log logging.level.root = DEBUG logging.level.org.springframework=DEBUG #spring.cloud.task.batch.events.enabled=true #tomcat server.tomcat.uri-encoding = UTF-8 server.tomcat.compression = off # endpoints #endpoints.enabled=false #endpoints.env.enabled=true #quartz.enabled = true # redis #spring.redis.host = 127.0.0.1 #spring.redis.port = 6379 #spring.redis.pool.max-idle= 666 #spring.redis.pool.min-idle= 0 #spring.redis.pool.max-active= 888 #spring.redis.pool.max-wait= 777
數據源相關設置mysql
package club.lemos.demo.configuration.config; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.WebStatFilter; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; @Configuration public class DruidConfiguration { @Bean @ConfigurationProperties(prefix="spring.datasource") public DataSource druidDataSource() { return new DruidDataSource(); } @Bean public FilterRegistrationBean statFilter(){ FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); //添加過濾規則. filterRegistrationBean.addUrlPatterns("/*"); //添加不須要忽略的格式信息. filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return filterRegistrationBean; } }
mybatis 基礎配置git
package club.lemos.demo.configuration.config; import com.github.pagehelper.PageHelper; 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.aop.framework.autoproxy.BeanNameAutoProxyCreator; import org.springframework.boot.autoconfigure.AutoConfigureAfter; 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 org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.TransactionManagementConfigurer; import org.springframework.transaction.interceptor.TransactionInterceptor; import javax.annotation.Resource; import javax.sql.DataSource; import java.util.Properties; /** * mybatis基礎配置 * Created by lg on 2017/2/4. */ @Configuration @AutoConfigureAfter(DruidConfiguration.class) @EnableTransactionManagement public class MybatisConfig implements TransactionManagementConfigurer{ @Resource private DataSource dataSource; @Bean(name = "sqlSessionFactory") public SqlSessionFactory sqlSessionFactoryBean() { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); // bean.setTypeAliasesPackage("com.demo.domain"); Mapper.xml中類型爲類的全名, 可不配置此屬性 //分頁插件 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:mapping/*.xml")); return bean.getObject(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } @Bean public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } /** * 配置事務 */ @Bean public PlatformTransactionManager annotationDrivenTransactionManager() { return new DataSourceTransactionManager(dataSource); } @Bean public TransactionInterceptor transactionInterceptor(DataSourceTransactionManager transactionManager) { TransactionInterceptor ti = new TransactionInterceptor(); ti.setTransactionManager(annotationDrivenTransactionManager()); Properties properties = new Properties(); properties.setProperty("find*", "PROPAGATION_REQUIRED, readOnly"); properties.setProperty("get*", "PROPAGATION_REQUIRED, readOnly"); properties.setProperty("insert*", "PROPAGATION_REQUIRED"); properties.setProperty("delete*", "PROPAGATION_REQUIRED"); properties.setProperty("update*", "PROPAGATION_REQUIRED"); ti.setTransactionAttributes(properties); return ti; } @Bean public BeanNameAutoProxyCreator transactionAutoProxy() { BeanNameAutoProxyCreator transactionAutoProxy = new BeanNameAutoProxyCreator(); transactionAutoProxy.setProxyTargetClass(false); transactionAutoProxy.setBeanNames(new String[] { "*ServiceImpl" }); transactionAutoProxy.setInterceptorNames(new String[] { "transactionInterceptor" }); return transactionAutoProxy; } }
mybatis 掃描配置github
package club.lemos.demo.configuration.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; /** * mybatis掃描配置 * @author seven sins * */ @Configuration @AutoConfigureAfter(MybatisConfig.class) public class MyBatisMapperScannerConfig { @Bean public MapperScannerConfigurer mapperScannerConfigurer() { MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory"); mapperScannerConfigurer.setBasePackage("club.lemos.demo.mapper"); // mapper package、xml namespace return mapperScannerConfigurer; } }
也能夠經過 application.properties 配置,參考原文web
#mybatis配置 mybatis.mapper-locations=classpath:mapping/*.xml mybatis.configuration.mapUnderscoreToCamelCase=true mybatis.configuration.useColumnLabel=true
下面提供一個樣例redis
Mapper接口spring
package club.lemos.demo.mapper; import club.lemos.demo.domain.User; public interface UserMapper { void addUser(User user); }
Mapping文件sql
<?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="club.lemos.demo.mapper.UserMapper"> <insert id="addUser" useGeneratedKeys="true" keyProperty="id" parameterType="club.lemos.demo.domain.User"> INSERT INTO user(name, password) VALUES(#{name}, #{password}) </insert> </mapper>
項目地址:點擊這裏