生成项目
https://start.spring.io/到这个网址生成项目基本模板
整合Mybatis+Druid
1. pom.xml文件中添加( druid请使用最新版,老版本在springboot下测试有问题)
<!-- 数据库 start -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</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>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<!-- 数据库 end -->
2. application.yml文件中添加
mybatis:
#别名
type-aliases-package: me.zingon.lock.cloud.model
#指定mapper的xml文件的位置
mapper-locations: classpath:mapper/*.xml
configuration:
map-underscore-to-camel-case: true
default-fetch-size: 100
default-statement-timeout: 30
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/lock_cloud?characterEncoding=UTF-8&allowMultiQueries=true
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
druid:
initial-size: 5
max-active: 20
web-stat-filter.enabled: false
Druid更详细的配置请参考官方文档
3. 配置自动扫描dao接口 (springboot启动文件**Application.java)
//这个注解指定要扫描的dao包
@MapperScan(basePackages = "me.zingon.lock.cloud.dao")
@SpringBootApplication
public class LockCloudApplication {
public static void main(String[] args) {
SpringApplication.run(LockCloudApplication.class, args);
}
}
4.自动生成CRUD代码
我之前写过一个代码生成器,开源地址是https://gitee.com/A_yes/generatorrestfulapi
使用方法
把这个工具生成的代码考到项目里就可直接用CRUD接口了
整合通用Mapper
1. pom.xml文件中添加一下依赖
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
2. 修改Application类
import tk.mybatis.spring.annotation.MapperScan;
//把这个注解换为 通用mapper 包下的注解
@MapperScan(basePackages = "me.zingon.temp.dao")
3. 使用
dao借口继承 tk.mybatis.mapper.common.Mapper和import tk.mybatis.mapper.common.MySqlMapper接口
整合Redis
1. pom.xml文件中添加一下依赖
<!-- redis start -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
<!-- redis end -->
2. application.yml中添加以下配置
redis:
host: 127.0.0.1
port: 6379
password: 123456
#这个连接器可以自由选择 jedis 或者 lettuce
lettuce:
pool:
max-active: 8
max-idle: 8
max-wait: -1
3. 代码中就可以直接使用了
一个简单的工具类供大家参考
@Component
public class TestCache {
Logger logger= LoggerFactory.getLogger(this.getClass());
@Autowired
RedisTemplate<String,String> redisTemplate;
// public void set(String key,String value){
// stringRedisTemplate.opsForValue().set(key, value);
// stringRedisTemplate.expire(key,10, TimeUnit.SECONDS);
// }
public String get(final String key) {
logger.debug("redis-get key:{}",key);
return redisTemplate.execute(new RedisCallback<String>() {
@Override
public String doInRedis(RedisConnection redisConnection) throws DataAccessException {
byte[] keyB= redisTemplate.getStringSerializer().serialize(key);
if(redisConnection.exists(keyB)) {
byte[] valueB = redisConnection.get(keyB);
return (String) redisTemplate.getValueSerializer().deserialize(valueB);
}
return null;
}
});
}
public void set(final String key, final String value) {
logger.debug("redis-set key:{} value:{}",key,value);
redisTemplate.execute(new RedisCallback<String>() {
@Override
public String doInRedis(RedisConnection redisConnection) throws DataAccessException {
redisConnection.set(redisTemplate.getStringSerializer().serialize(key),
redisTemplate.getStringSerializer().serialize(value));
return null;
}
});
}
public boolean setnx(final String key,final String value){
logger.debug("redis-setnx key:{} value:{}",key,value);
return redisTemplate.execute(new RedisCallback<Boolean>(){
@Override
public Boolean doInRedis(RedisConnection redisConnection) throws DataAccessException {
return redisConnection.setNX(redisTemplate.getStringSerializer().serialize(key),
redisTemplate.getStringSerializer().serialize(value));
}
});
}
public boolean pexpire(final String key,final Long milliseconds){
logger.debug("redis-pexpire key:{} milliseconds:{}",key,milliseconds);
return redisTemplate.execute(new RedisCallback<Boolean>(){
@Override
public Boolean doInRedis(RedisConnection redisConnection) throws DataAccessException {
return redisConnection.pExpire(redisTemplate.getStringSerializer().serialize(key),
milliseconds);
}
});
}
public long rm(final String key){
logger.debug("redis-rm key:{}",key);
return redisTemplate.execute(new RedisCallback<Long>() {
@Override
public Long doInRedis(RedisConnection redisConnection) throws DataAccessException {
return redisConnection.del(redisTemplate.getStringSerializer().serialize(key));
}
});
}
/**
* 根据前缀列出所有值
* @param prefix
* @return
*/
public List<String> list(final String prefix){
List<String> values=new ArrayList<String>();
Set<byte[]> keys= redisTemplate.execute(new RedisCallback<Set<byte[]>>() {
@Override
public Set<byte[]> doInRedis(RedisConnection redisConnection) throws DataAccessException {
return redisConnection.keys(redisTemplate.getStringSerializer().serialize(prefix));
}
});
for(final byte[] key:keys){
String value = redisTemplate.execute(new RedisCallback<String>() {
@Override
public String doInRedis(RedisConnection redisConnection) throws DataAccessException {
return redisTemplate.getStringSerializer().deserialize(redisConnection.get(key));
}
});
values.add(value);
}
return values;
}
}
整合log4j2
添加HTTPS支持
application.yml中添加以下配置
ssl:
key-store: classpath:monkey.pacargle.com.jks
key-password: 0t4cf638l64xj32
key-store-type: JKS
key-alias: monkey.pacargle.com
完整pom.xml
<?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>me.zingon</groupId>
<artifactId>lock-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>lock-cloud</name>
<description>lock program on cloud</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.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</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- 数据库 start -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</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>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<!-- 数据库 end -->
<!-- redis start -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
<!-- redis end -->
<!-- log4j2 start -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- log4j2 end -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
完整application.yml
spring:
mvc:
view:
prefix: /templates/
suffix: .ftl
#静态资源
resources:
static-locations: classpath:/asd
datasource:
url: jdbc:mysql://127.0.0.1:3306/lock_cloud?characterEncoding=UTF-8&allowMultiQueries=true
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
druid:
initial-size: 5
max-active: 20
web-stat-filter.enabled: false
redis:
host: 127.0.0.1
port: 6379
password: 123456
lettuce:
pool:
max-active: 8
max-idle: 8
max-wait: -1
mybatis:
type-aliases-package: me.zingon.lock.cloud.model
mapper-locations: classpath:mapper/*.xml
configuration:
map-underscore-to-camel-case: true
default-fetch-size: 100
default-statement-timeout: 30
logging:
config: classpath:log4j2.properties