FirstGradle
一、导入依赖

二、build.gradle
- 整合SpringBoot
plugins {
id 'java'
}
group 'com.qiang'
version '1.0.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.1.4.RELEASE'
}
三、Application
package com.qiang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author 小强崽
* @ClassName: Application
* @Description: Gradle整合SpringBoot
* @date 2019/9/16 1:17
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- 启动项目

- 启动成功

四、整合Mabatis
- 导入依赖
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.15'
compile group: 'com.alibaba', name: 'druid', version: '1.1.18'
compile group: 'org.mybatis.spring.boot', name: 'mybatis-spring-boot-starter', version: '2.0.1'
- 设置资源文件夹
sourceSets {
main {
resources {
srcDirs("src/main/java", "src/main/resources")
}
}
}
- 在主函数上加上注解
@MapperScan({"com.qiang.mapper"})
五、application.yml
server:
port: 8080
spring:
application:
name: FirstGradle
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=CTT
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
六、整合PageHelper
- 导入依赖
compile group: 'com.github.pagehelper', name: 'pagehelper', version: '4.1.6'
- 配置PageHelper
package com.qiang.config;
import com.github.pagehelper.PageHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
/**
* @author 小强崽
* @ClassName: MybatisConfig
* @Description: PageHelper配置
* @date 2019/9/16 16:37
*/
@Configuration
public class MybatisConfig {
/**
* 配置Mybatis的分页插件
*/
@Bean
public PageHelper pageHelper() {
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("offsetAsPageNum", "true");
properties.setProperty("rowBoundsWithCount", "true");
properties.setProperty("reasonable", "true");
properties.setProperty("dialect", "mysql");
pageHelper.setProperties(properties);
return pageHelper;
}
}
- 使用
package com.qiang.controller;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qiang.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author 小强崽
* @ClassName: UserController
* @Description: 用户控制器
* @date 2019/9/16 1:51
*/
@RestController
public class UserController {
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
@Resource
private UserService userService;
@RequestMapping("/user")
public Object getAllUser() {
Page<Object> page = PageHelper.startPage(0, 1);
logger.info(page.toString());
return userService.selectAll();
}
}
七、整合Redis
- 导入依赖
compile('org.springframework.boot:spring-boot-starter-data-redis:2.1.3.RELEASE') {
exclude group: 'redis.clients', module: 'jedis'
exclude group: 'io.lettuce', module: 'lettuce-core'
}
compile group: 'redis.clients', name: 'jedis', version: '2.9.0'
- Redis配置
package com.qiang.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
/**
* @author 小强崽
* @ClassName: RedisConfig
* @Description: Redis配置
* @date 2019/9/16 16:49
*/
@Configuration
public class RedisConfig {
/**
* Redis服务器地址
*/
private String host = "127.0.0.1";
/**
* Redis服务器连接端口
*/
private int port = 6379;
/**
* Redis数据库索引(默认为0)
*/
private int database = 15;
/**
* 连接池中的最大空闲连接
*/
private int maxIdle = 8;
/**
* 连接池最大阻塞等待时间(使用负值表示没有限制)
*/
private long maxWaitMillis = -1;
/**
* 连接池最大连接数(使用负值表示没有限制)
*/
private int maxActive = 8;
/**
* 初始化
*/
@Bean
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(maxActive);
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
jedisPoolConfig.setMaxIdle(maxIdle);
return jedisPoolConfig;
}
/**
* 注入RedisConnectionFactory
*/
@Bean
public RedisConnectionFactory redisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setHostName(host);
redisStandaloneConfiguration.setPort(port);
redisStandaloneConfiguration.setDatabase(database);
JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jedisPoolConfigBuilder =
(JedisClientConfiguration.JedisPoolingClientConfigurationBuilder) JedisClientConfiguration.builder();
jedisPoolConfigBuilder.poolConfig(jedisPoolConfig);
return new JedisConnectionFactory(redisStandaloneConfiguration);
}
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
// 设置任何字段可见
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// 设置不是final的属性可以转换
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key采用String的序列化方式
redisTemplate.setKeySerializer(stringRedisSerializer);
// hash的key采用String的序列化方式
redisTemplate.setHashKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson序列化方式
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
// hash的value序列化方式采用jackson序列化方式
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
redisTemplate.setEnableTransactionSupport(true);
return redisTemplate;
}
}
- RestTemplate配置
package com.qiang.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
/**
* @author 小强崽
* @ClassName: RestTemplateConfig
* @Description: RestTemplate配置
* @date 2019/9/16 17:25
*/
@Configuration
public class RestTemplateConfig {
@Bean
@ConditionalOnMissingBean({RestTemplate.class})
public RestTemplate restTemplate(ClientHttpRequestFactory clientHttpRequestFactory) {
return new RestTemplate(clientHttpRequestFactory);
}
@Bean
@ConditionalOnMissingBean({ClientHttpRequestFactory.class})
public ClientHttpRequestFactory clientHttpRequestFactory() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(15000);
factory.setConnectTimeout(15000);
return factory;
}
}
- 使用
package com.qiang.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author 小强崽
* @ClassName: RedisController
* @Description:
* @date 2019/9/16 18:01
*/
@RestController
public class RedisController {
@Autowired
private RedisTemplate redisTemplate;
@RequestMapping("/redis")
public Object getRedisValues(){
redisTemplate.opsForValue().set("a","b");
return redisTemplate.opsForValue().get("a");
}
}
八、项目结构

九、源代码
作者(Author):小强崽
来源(Source):https://www.wuduoqiang.com/archives/FirstGradle
协议(License):署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)
版权(Copyright):商业转载请联系作者获得授权,非商业转载请注明出处。 For commercial use, please contact the author for authorization. For non-commercial use, please indicate the source.
FirstGradle的更多相关文章
随机推荐
- vue3后台管理系统(模板)
系统简介 此管理系统是基于Vite2和Vue3.0构建生成的后台管理系统.目的在于学习vite和vue3等新技术,以便于后续用于实际开发工作中: 本文章将从管理系统页面布局.vue路由鉴权.vuex状 ...
- 【论文小综】基于外部知识的VQA(视觉问答)
我们生活在一个多模态的世界中.视觉的捕捉与理解,知识的学习与感知,语言的交流与表达,诸多方面的信息促进着我们对于世界的认知.作为多模态领域的一个典型场景,VQA旨在结合视觉的信息来回答所提出的问题 ...
- python 07篇 内置函数和匿名函数
一.内置函数 # 下面这些要掌握 # len type id print input open # round min max filter map zip exec eval print(all([ ...
- C语言:常用数学函数
#include <stdio.h> #include <math.h> #include <stdlib.h> #include <time.h> # ...
- 【论文阅读】PRM-RL Long-range Robotic Navigation Tasks by Combining Reinforcement Learning and Sampling-based Planning
目录 摘要部分: I. Introduction II. Related Work III. Method **IMPORTANT PART A. RL agent training [第一步] B. ...
- 浅析vue-cli脚手架命令的执行过程
上一篇文章,已经大致了解脚手架是什么以及脚手架是如何工作的.接下来,稍微深入一下脚手架的工作过程(以vue-cli为例).首先抛出3个问题: 1.明明全局安装的是@vue/cli,最后执行的命令却是v ...
- 微信小程序云开发-云存储-上传、下载、打开文件文件(word/excel/ppt/pdf)一步到位
一.wxml文件 <!-- 上传.下载.打开文件一步执行 --> <view class="handle"> <button bindtap=&quo ...
- Git,Linux,Ubuntu,Tmux的常用命令
常用的连接 Git命令 廖雪峰的Git教程 Git常用命令 ubuntu16.04之GitHub入门教程 Linux相关 tmux命令 Ubuntu常用命令速查手册 Linux 命令大全 其它工具 M ...
- 阿里云RocketMQ定时/延迟消息队列实现
新的阅读体验:http://www.zhouhong.icu/post/157 一.业务需求 需要实现一个提前二十分钟通知用户去做某件事的一个业务,拿到这个业务首先想到的最简单得方法就是使用Redis ...
- YOLO-v4 口罩识别
YOLO-v4 口罩识别 一.YOLO-v4概念 如果想要了解和认识yolo-v4的基本概念,首先要提的就是它的基础版本yolo-v1,对于yolo来说,最经典的算是yolo-v3.如果想要了解它的由 ...