SpringBoot整合redis和Mysql(测试)

刚接触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注解),这都是之前踩过的坑,记录下来为以后自己和他人搭建环境是少走弯路。

  

原文地址:https://www.cnblogs.com/myworldfordata/p/9343265.html

时间: 2024-11-07 16:19:16

SpringBoot整合redis和Mysql(测试)的相关文章

三:Springboot整合Redis

一:springboot整合redis redis版本:3.0.0 运行环境:linux 1.安装redis 1.1安装gcc yum install gcc-c++ 1.2解压redis.3.0.0.tar.gz压缩包 tar -zxvf redis-3.0.0.tar.gz 1.3进入解压后的目录进行编译 cd redis-3.0.0 make 1.4将redis安装到指定目录 make PREFIX=/usr/local/redis install 1.5启动redis ./redis-s

SpringBoot整合Redis并完成工具类

SpringBoot整合Redis的资料很多,但是我只需要整合完成后,可以操作Redis就可以了,所以不需要配合缓存相关的注解使用(如@Cacheable),而且我的系统框架用的日志是log4j,不是SpringBoot默认的Logback.通过查询资料我完成了Redis整合,并写了Redis操作工具类.特意在此记录一下,分享给大家,也方便以后查阅. 1.SpringBoot版本如下 <parent> <groupId>org.springframework.boot</gr

超简单!!!SpringBoot整合Redis

1.导入Pom.xml依赖 1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.ap

九、springboot整合redis二之缓冲配置

1.创建Cache配置类 @Configuration @EnableCaching public class RedisCacheConfig extends CachingConfigurerSupport { @Value("${redis.cache.expiration}") private Long expiration; /** * * 管理缓存 */ @Bean public CacheManager cacheManager(RedisTemplate<Obje

SpringBoot 整合 Redis缓存

在我们的日常项目开发过程中缓存是无处不在的,因为它可以极大的提高系统的访问速度,关于缓存的框架也种类繁多,今天主要介绍的是使用现在非常流行的NoSQL数据库(Redis)来实现我们的缓存需求. SpringBoot整合Redis是非常方便快捷的,我用的是Mybatis,这里就不说Springboot整合Mybatis了网上有很多,同样非常简单. 下面进入正题: 原文地址:https://www.cnblogs.com/yueguanguanyun/p/9756058.html

Spring Boot 学习----SpringBoot整合Redis

SpringBoot整合Redis 说明:由于使用的是window系统,所以本文中使用的redis是再Docker中运行的. 至于如何启动redis这里不再说明,需要的请自行百度. 配置pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </depend

八、springboot整合redis

整合Redis 一. 注解方式实现添加缓存 1.在pom.xml加入依赖 <!-- 配置使用redis启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency> 2. 修改引导类 修改开启缓存,添加注解@EnableCachi

SpringBoot整合Redis初实践

Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理. 有时,为了提升整个网站的性能,在开发时会将经常访问的数据进行缓存,这样在调用这个数据接口时,可以提高数据加载的效率 本文将在Boot项目中进行Redis的整合,将常用的数据缓存到Redis服务器中,提高常用服务的并发能力. 项目环境: jdk1.8 maven3.5 spring boot 2.0.4.RELEASE spring-boot-starter-data-redis 2.0.4.RE

SpringBoot整合Redis注意的一些问题

1:ERR value is not an integer or out of range 1-1:背景 使用redisTemplate.opsForValue().increment(key, delat)方法. 1-2:分析 分析:redis对任何不合法的值,都称为ERR.只有使用StringRedisSerializer序列化器才能使用incrment或者decrement方法 1-3:问题解决 使用GenericToStringSerializer.StringRedisSerializ