本文共 11443 字,大约阅读时间需要 38 分钟。
SpringBootAdmin是码云上一个以springboot为核心的开源的后台管理系统。这里是链接地址:
由于是后台系统,应该采用数据库分离,权限,用户,角色和业务模块分开。
application.properties
#服务端口server.port = 8001# ĿcontextPathserver.context-path = /# sessionserver.session-timeout=60#which active#spring.profiles.active=pro logging.level.com.mys.my.mapper = DEBUG#主数据库配置spring.datasource.master.type = com.alibaba.druid.pool.DruidDataSourcespring.datasource.master.driver-class-name = com.mysql.jdbc.Driverspring.datasource.master.name = geekcattlespring.datasource.master.url=jdbc:mysql://localhost:3306/geekcattle?useUnicode=true&characterEncoding=UTF-8spring.datasource.master.username = rootspring.datasource.master.password = 1234spring.datasource.master.filters = statspring.datasource.master.maxActive = 20spring.datasource.master.initialSize = 5spring.datasource.master.maxWait = 60000spring.datasource.master.minIdle = 20spring.datasource.master.timeBetweenEvictionRunsMillis = 60000spring.datasource.master.minEvictableIdleTimeMillis = 300000spring.datasource.master.validationQuery = select 'x'spring.datasource.master.testWhileIdle = truespring.datasource.master.testOnBorrow = falsespring.datasource.master.testOnReturn = falsespring.datasource.master.poolPreparedStatements = truespring.datasource.master.maxOpenPreparedStatements = 20#从数据库配置spring.datasource.film.type = com.alibaba.druid.pool.DruidDataSourcespring.datasource.film.driver-class-name = com.mysql.jdbc.Driverspring.datasource.film.name = filmspring.datasource.film.url=jdbc:mysql://localhost:3306/film?useUnicode=true&characterEncoding=UTF-8spring.datasource.film.username = rootspring.datasource.film.password = 1234spring.datasource.film.filters = statspring.datasource.film.maxActive = 20spring.datasource.film.initialSize = 1spring.datasource.film.maxWait = 60000spring.datasource.film.minIdle = 20spring.datasource.film.timeBetweenEvictionRunsMillis = 60000spring.datasource.film.minEvictableIdleTimeMillis = 300000spring.datasource.film.validationQuery = select 'x'spring.datasource.film.testWhileIdle = truespring.datasource.film.testOnBorrow = falsespring.datasource.film.testOnReturn = falsespring.datasource.film.poolPreparedStatements = truespring.datasource.film.maxOpenPreparedStatements = 20#MVCspring.mvc.view.prefix = classpath:/templates/spring.mvc.view.suffix = .htmlspring.mvc.date-format=yyyy-MM-dd HH:mm:ss#spring.thymeleaf.mode = HTML5spring.thymeleaf.cache = falsespring.thymeleaf.encoding = UTF-8spring.thymeleaf.content-type = text/html#mybatiesspring.mapper.plugin = tk.mybatis.mapper.generator.MapperPluginspring.mapper.Mapper = com.mys.my.util.CustomerMapper#jsonspring.jackson.time-zone=Asia/Chongqingspring.jackson.date-format=yyyy-MM-dd HH:mm:ssspring.jackson.joda-date-time-format=yyyy-MM-dd HH:mm:ss# Redis数据库索引(默认为0)spring.redis.database=1# Redis服务器地址spring.redis.host=# Redis服务器连接端口spring.redis.port=6379# Redis服务器连接密码(默认为空)spring.redis.password=# 连接池最大连接数(使用负值表示没有限制)spring.redis.pool.max-active=30# 超时时间spring.redis.timeout=100000# 连接池中的最大空闲连接spring.redis.pool.max-idle=20# 连接池最大阻塞等待时间(使用负值表示没有限制)spring.redis.pool.max-wait=-1
MyBatisConfig.java
/* * Copyright (c) 2017新建一个数据库配置文件 MyBatisConfigFilm.javaAll rights reserved. */package com.mys.my.conf;import java.sql.Connection;import java.sql.SQLException;import java.util.Properties;import javax.sql.DataSource;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.beans.factory.annotation.Qualifier;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.context.annotation.Primary;import org.springframework.context.annotation.PropertySource;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 com.github.pagehelper.PageHelper;/** * MyBatis基础配置 */@Configuration@PropertySource("classpath:application.properties")//@EnableTransactionManagementpublic class MyBatisConfig {// @Autowired// DataSource dataSource; @Bean(name="masterDataSource") @ConfigurationProperties(prefix = "spring.datasource.master") @Primary//默认数据源 public DataSource dataSource() { System.out.println("======================================"+DataSourceBuilder.create().build()); return DataSourceBuilder.create().build(); } @Bean(name="masterSqlSessionFactory") @Primary//默认数据源 public SqlSessionFactory sqlSessionFactoryBean(@Qualifier("masterDataSource") DataSource dataSource) throws SQLException { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); System.out.println("dataSource===================================================="+dataSource); bean.setDataSource(dataSource); bean.setTypeAliasesPackage("com.mys.my.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")); bean.setMapperLocations(resolver.getResources("classpath:mapper/*/*.xml")); return bean.getObject(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } @Bean("masterSqlSessionTemplate") @Primary public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory){ return new SqlSessionTemplate(sqlSessionFactory); } @Bean("masterTransactionManager") @Primary public PlatformTransactionManager annotationDrivenTransactionManager(@Qualifier("masterDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); }}
/* * Copyright (c) 2017All rights reserved. */package com.mys.my.conf;import java.sql.SQLException;import java.util.Properties;import javax.sql.DataSource;import org.apache.ibatis.plugin.Interceptor;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.SqlSessionTemplate;import org.mybatis.spring.annotation.MapperScan;import org.springframework.beans.factory.annotation.Qualifier;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.core.io.support.ResourcePatternResolver;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import org.springframework.transaction.PlatformTransactionManager;import com.github.pagehelper.PageHelper;/** * MyBatis基础配置 */@Configuration@MapperScan(basePackages={"com.mys.my.mapper.fiz"},sqlSessionFactoryRef="filmSqlSessionFactory")public class MyBatisConfigFilm{ @Bean(name="filmDatasource") @ConfigurationProperties(prefix = "spring.datasource.film") public DataSource dataSource() { System.out.println("======================================"+DataSourceBuilder.create().build()); return DataSourceBuilder.create().build(); } @Bean(name="filmSqlSessionFactory") public SqlSessionFactory sqlSessionFactoryBean(@Qualifier("filmDatasource") DataSource dataSource) throws SQLException { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); System.out.println("dataSource===================================================="+dataSource()); bean.setDataSource(dataSource()); bean.setTypeAliasesPackage("com.mys.my.pojo"); //分页插件 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")); bean.setMapperLocations(resolver.getResources("classpath:mapper/fiz/*.xml")); return bean.getObject(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } @Bean("filmSqlSessionTemplate") public SqlSessionTemplate sqlSessionTemplate(@Qualifier("filmSqlSessionFactory")SqlSessionFactory sqlSessionFactory){ return new SqlSessionTemplate(sqlSessionFactory); } @Bean(name = "filmTransactionManager") public PlatformTransactionManager annotationDrivenTransactionManager(@Qualifier("filmDatasource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource()); }}
@MapperScan(basePackages={"com.mys.my.mapper.fiz"}的配置是扫描配置的dao文件 *.java bean.setMapperLocations(resolver.getResources("classpath:mapper/fiz/*.xml")); 这个配置是扫描对应的mapper文件 *.xml。
MyBatisMapperScannerConfig.java
/* * Copyright (c) 2017All rights reserved. */package com.mys.my.conf;import org.springframework.boot.autoconfigure.AutoConfigureAfter;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;//import com.mys.my.mapper.dataSource.MyFilmDatasource;import tk.mybatis.spring.mapper.MapperScannerConfigurer;import java.util.Properties;/** * MyBatis扫描接口,使用的tk.mybatis.spring.mapper.MapperScannerConfigurer,如果你不使用通用Mapper */@Configuration//TODO 注意,由于MapperScannerConfigurer执行的比较早,所以必须有下面的注解@AutoConfigureAfter(MyBatisConfig.class)public class MyBatisMapperScannerConfig { @Bean public MapperScannerConfigurer mapperScannerConfigurer() { MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); mapperScannerConfigurer.setSqlSessionFactoryBeanName("masterSqlSessionFactory"); mapperScannerConfigurer.setSqlSessionTemplateBeanName("masterSqlSessionTemplate"); mapperScannerConfigurer.setBasePackage("com.mys.my.mapper"); Properties properties = new Properties(); properties.setProperty("mappers", "com.mys.my.util.CustomerMapper"); properties.setProperty("notEmpty", "false"); properties.setProperty("IDENTITY", "MYSQL"); mapperScannerConfigurer.setProperties(properties); return mapperScannerConfigurer; }}
转载地址:http://efmlf.baihongyu.com/