1.在maven引入相关的依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> <relativePath /> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <!-- redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- 支持 @ConfigurationProperties 注解 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <!-- 数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.8</version> </dependency> <!-- mysql连接 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.6</version> </dependency>
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.2</version> </dependency>
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.31</version> </dependency>
2.SpringBoot加载文件application.yml
mysql-connector-java6.0的url需要加载时区,driverClassName变为com.mysql.cj.jdbc.Driver。redis的密码在redis.config中修改。
com.mysql.cj.jdbc.Driver
server: port: 9090 session-timeout: 30 tomcat.max-threads: 0 tomcat.uri-encoding: UTF-8 spring: datasource: type : com.alibaba.druid.pool.DruidDataSource url : jdbc:mysql://localhost:3306/book_store?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false username : root password : root driverClassName : com.mysql.cj.jdbc.Driver initialSize : 5 minIdle : 5 maxActive : 20 maxWait : 60000 timeBetweenEvictionRunsMillis : 60000 minEvictableIdleTimeMillis : 300000 validationQuery : SELECT 1 FROM DUAL testWhileIdle : true testOnBorrow : false testOnReturn : false poolPreparedStatements : true maxPoolPreparedStatementPerConnectionSize : 20 filters : stat,wall connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis : 5000 useGlobalDataSourceStat : true mybatis: type-aliases-package: com.qyj.entity pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql http: encoding: force : true charset : UTF-8 enables : true thymeleaf: cache: false mode: HTML5 prefix: classpath\:/static/ suffix: .html encoding: UTF-8 content: text/html redis: host: 127.0.0.1 port: 6379 password: 123456 timeout: 0
3.mybatis配置类MybatisConfig
@Configuration @EnableTransactionManagement public class MyBatisConfig implements TransactionManagementConfigurer { @Autowired DataSource dataSource; //注入pageHelper @Bean public PageInterceptor pageInterceptor() throws IOException { PageInterceptor pageInterceptor = new PageInterceptor(); Properties props = new Properties(); props.setProperty("reasonable", "true"); props.setProperty("supportMethodsArguments", "true"); props.setProperty("returnPageInfo", "check"); props.setProperty("params", "count=countSql"); pageInterceptor.setProperties(props); return pageInterceptor; } @Bean public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } @Bean @Override public PlatformTransactionManager annotationDrivenTransactionManager() { return new DataSourceTransactionManager(dataSource); } }
4.redis配置(参考springboot2.0文档所写)
@Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport { @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.timeout}") private int timeout; @Value("${spring.redis.password}") private String password; @Bean public KeyGenerator wiselyKeyGenerator(){//主键生成策咯 return new KeyGenerator() { @Override public Object generate(Object target, Method method, Object... params) { StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()); sb.append(method.getName()); for (Object obj : params) { sb.append(obj.toString()); } return sb.toString(); } }; } @Bean @ConditionalOnMissingBean(StringRedisTemplate.class) public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { StringRedisTemplate template = new StringRedisTemplate(); template.setConnectionFactory(redisConnectionFactory); return template; } @Bean public RedisConnectionFactory redisConnectionFactory() { RedisStandaloneConfiguration factory = new RedisStandaloneConfiguration(); factory.setHostName(host); factory.setPort(port); factory.setPassword(RedisPassword.of(password)); return new JedisConnectionFactory(factory); } }
5.Service(PageHelp5.0用法)
@Cacheable("queryGgdmDataList") public ResponseData queryGgdmDataList(Map<String, Object> queryParams) { int index = Integer.parseInt(queryParams.get("page").toString()); int size = Integer.parseInt(queryParams.get("rows").toString()); PageHelper.startPage(index, size); StringBuilder sql = new StringBuilder(); List<Map<String,Object>> commList = mybatisDao.queryListDataBySql(sql.toString()); PageInfo page = new PageInfo(commList) ; List list = page.getList(); ResponseData pageResult = new ResponseData((int) page.getTotal(), list); return pageResult; }
6.SpringBoot启动
@SpringBootApplication @ServletComponentScan @EnableAutoConfiguration @MapperScan("xx.xx.dao")//扫描的Dao层 public class ApplicationSpringBoot { public static void main(String[] args) { SpringApplication.run(ApplicationSpringBoot.class,args); } }
7.Controller采用Restful风格
@RestController @RequestMapping("/framework")
public class CommController { @RequestMapping( value = "/bzfl/getComboxData", method = RequestMethod.GET) @ResponseBody public List<Map<String,Object>> getComboxData(@RequestParam String ssfl){ return commService.getComboxData(ssfl); } }
9.数据缓存到redis(工具Redis Resktop Manager)
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:SpringBoot2.0+Mybatis+PageHelper+Redis实现缓存 - Python技术站