springboot2.0開發框架搭建

生成項目

https://start.spring.io/到這個網址生成項目基本模板java


整合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更詳細的配置請參考官方文檔

Druid官方文檔mysql

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
使用方法git

把這個工具生成的代碼考到項目裏就可直接用CRUD接口了github


整合通用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接口web


整合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. 代碼中就能夠直接使用了

一個簡單的工具類供你們參考redis

@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

以前的文章spring

添加HTTPS支持

application.yml中添加如下配置sql

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
相關文章
相關標籤/搜索