刚接触springboot,初次整合,简单的数据连接,分享一下自己的配置。

1.目录结构:

 

2.参数配置application.yml

#jsp文件路径,默认路径:src/main/webapp,
spring:
  #redis配置
  redis:
    host: localhost
    port: 6379
    password: 123456
    # 连接超时时间 单位 ms(毫秒)
    timeout: 3000
    max-idle: 200
    max-active: 2000
    max-wait: 1000
#mysql配置
  datasource:
    url: jdbc:mysql://localhost:3306/test_data
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
    # 使用druid数据源
    type: com.alibaba.druid.pool.DruidDataSource
    max-idle: 10
    max-wait: 1000
    min-idle: 5
    initial-size: 5
    dbcp2:
      validation-query: SELRCT 1
      test-on-borrow: true
mybatis:
  mapperLocations: classpath:mappers/*.xml
 #domain object's package
  typeAliasesPackage: com.zykj.projectforspringboot.bean
#配置扫描Mapper
#mapper:
#  #多个mapper用逗号隔开,(注解扫描@MapperScan("com.zykj.projectforspringboot.mappers.GirlMapper) :标识持久层mapper接口,用来找到mapper对象)
#  mappers: com.zykj.projectforspringboot.mappers
#  not-empty: false
#  identity: MYSQL
#配置
  mvc:
    view:
      prefix: /

      suffix: .jsp
http:
  encoding:
      charset: UTF-8
      enabled: true
      force: true

server:
  port: 8080
  session:
    timeout: 10
  tomcat:
    uri-encoding: UTF-8

2.pom文件

<?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>com.zykj</groupId>
    <artifactId>projectforspringboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>projectforspringboot</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.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-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>

        <!--redis配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>1.5.9.RELEASE</version>
        </dependency>

        <!--mysql配置-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

3.mapper映射文件

  mapper.xml在yml中配置扫描mapperLocations: classpath:mappers/*.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.zykj.projectforspringboot.mappers.GirlMapper">
    <resultMap id="baseResultMap" type="com.zykj.projectforspringboot.bean.Girl">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="age" property="age" jdbcType="BIGINT"/>
        <result column="cup_size" property="cupSize" jdbcType="VARCHAR"/>
    </resultMap>
    <insert id="insertGirl" parameterType="com.zykj.projectforspringboot.bean.Girl">
        insert into test_girl ( name, age, cup_size)
        values (#{name,jdbcType=VARCHAR}, #{age,jdbcType=BIGINT},
        #{cupSize,jdbcType=VARCHAR})
    </insert>
    <select id="selectGirl" resultMap="baseResultMap" parameterType="Int">
        SELECT * FROM test_girl WHERE id = #{id,jdbcType=BIGINT}
    </select>
</mapper>

 GirlMapper接口使用@Mapper注解或在加载配置文件时使用包扫描@MapperScan(basePackages = "com.zykj.projectforspringboot.mappers")多个mappper包用逗号隔开。

package com.zykj.projectforspringboot.mappers;

import org.apache.ibatis.annotations.Mapper;
import com.zykj.projectforspringboot.bean.Girl;

//@Mapper
public interface GirlMapper {

    public void insertGirl(Girl girl);

    public Girl selectGirl(int id);

}

4.测试类,

 首先,提出一个基础的测试类BaseTest.java

package com.zykj.projectforspringboot.commons;

import com.zykj.projectforspringboot.ProjectforspringbootApplication;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=ProjectforspringbootApplication.class)
@WebAppConfiguration
@MapperScan(value = "com.zykj.projectforspringboot.mappers")
public class BaseTest {
}

TestMysql.java和 TestRedis.java

package com.zykj.projectforspringboot.testMysql;


import com.zykj.projectforspringboot.bean.Girl;
import com.zykj.projectforspringboot.commons.BaseTest;
import com.zykj.projectforspringboot.mappers.GirlMapper;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.TestExecutionListeners;

import javax.swing.plaf.PanelUI;

public class TestMysql extends BaseTest {
    @Autowired
    private GirlMapper girlMapper;
    @Test
    public void insert(){
        Girl girl =  new Girl();
        girl.setAge(15);
        girl.setCupSize("C");
        girl.setName("Lily");
        girlMapper.insertGirl(girl);
        System.out.println("保存成功!");
    }
    @Test
    public void getGirl(){
        Girl girl = girlMapper.selectGirl(1);
        System.out.println("girl`s age = " + girl.getAge());
    }

}
package com.zykj.projectforspringboot.testRedis;


import com.zykj.projectforspringboot.commons.BaseTest;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

public class testRedis extends BaseTest {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Test
    public void getValue(){
        String age = null;
        try {
            stringRedisTemplate.opsForValue().set("Jack","23");
            age = stringRedisTemplate.opsForValue().get("Jack");
            Boolean isDelete = stringRedisTemplate.delete("Jack");
            System.out.println("删除成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Jack 的年龄:"+ age);
    }
}

5.实体类

 在yml文件中通过typeAliasesPackage: com.zykj.projectforspringboot.bean扫描实体类

package com.zykj.projectforspringboot.bean;

import java.io.Serializable;
public class Girl implements Serializable {
    private static final long serialVersionUID = -1658170337012912131L;
    private int id;
    private String name;
    private int age;
    private String cupSize;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getCupSize() {
        return cupSize;
    }

    public void setCupSize(String cupSize) {
        this.cupSize = cupSize;
    }
}

在测试过程中pom文件引用jar包的版本过低会引起一些错误,数据库连接(例如mysql-connector-java包之前用5.1.21版本一直报错java.math.BigInteger cannot be cast to java.lang.Long),(例如mybatis-spring-boot-starter之前使用1.0,0一直引不到@Mapper注解),这都是之前踩过的坑,记录下来为以后自己和他人搭建环境是少走弯路。