redis整合mysql_SpringBoot整合redis和Mysql(测试)

本文介绍了如何在SpringBoot项目中整合Redis和MySQL,包括配置数据库连接、Redis设置、Mapper映射文件等内容,并提供了测试类示例。注意避免因依赖版本过低导致的问题。
摘要由CSDN通过智能技术生成

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

1.目录结构:

782d7475813e09d9bc7a7e67dd862e3e.png

2.参数配置application.yml

#jsp文件路径,默认路径:src/main/webapp,

spring:

#redis配置

redis:

host: localhost

port:6379password:123456# 连接超时时间 单位 ms(毫秒)

timeout:3000max-idle: 200max-active: 2000max-wait: 1000#mysql配置

datasource:

url: jdbc:mysql://localhost:3306/test_data

username: root

password:123456driver-class-name: com.mysql.jdbc.Driver

# 使用druid数据源

type: com.alibaba.druid.pool.DruidDataSource

max-idle: 10max-wait: 1000min-idle: 5initial-size: 5dbcp2:

validation-query: SELRCT 1test-on-borrow: truemybatis:

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文件

4.0.0

com.zykj

projectforspringboot

0.0.1-SNAPSHOT

jar

projectforspringboot

Demo project for Spring Boot

org.springframework.boot

spring-boot-starter-parent

2.0.3.RELEASE

UTF-8

UTF-8

1.8

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-test

2.0.1.RELEASE

org.springframework.boot

spring-boot-starter-cache

2.0.3.RELEASE

junit

junit

4.12

org.springframework.boot

spring-boot-starter-data-redis

1.5.9.RELEASE

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.1.1

mysql

mysql-connector-java

5.1.46

com.alibaba

druid

1.1.0

org.springframework.boot

spring-boot-maven-plugin

3.mapper映射文件

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

insert into test_girl ( name, age, cup_size)

values (#{name,jdbcType=VARCHAR}, #{age,jdbcType=BIGINT},

#{cupSize,jdbcType=VARCHAR})

SELECT* FROM test_girl WHERE id = #{id,jdbcType=BIGINT}

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 interfaceGirlMapper {public voidinsertGirl(Girl girl);public Girl selectGirl(intid);

}

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 classBaseTest {

}

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 classTestMysql extends BaseTest {

@AutowiredprivateGirlMapper girlMapper;

@Testpublic voidinsert(){

Girl girl= newGirl();

girl.setAge(15);

girl.setCupSize("C");

girl.setName("Lily");

girlMapper.insertGirl(girl);

System.out.println("保存成功!");

}

@Testpublic voidgetGirl(){

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 classtestRedis extends BaseTest {

@AutowiredprivateStringRedisTemplate stringRedisTemplate;

@Testpublic voidgetValue(){

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 classGirl implements Serializable {private static final long serialVersionUID = -1658170337012912131L;private intid;privateString name;private intage;privateString cupSize;public intgetId() {returnid;

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

}publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}public intgetAge() {returnage;

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

}publicString getCupSize() {returncupSize;

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值