SpringBoot基础(三、整合Mybatis、Redis)

目录

SpringBoot整合Mybatis框架

SpringBoot整合Redis

总结

我们以前使用SSM的时候,使用Mybatis是需要各种配置文件、实体类、Dao层的各种映射关系,虽然可以使用注解减少这些配置信息,但还是有好多东西需要配置,自从SpringBoot流行起来(约定大于配置),Mybatis也开发了一套解决方案,简化配置。

SpringBoot整合Mybatis框架

Mybatis的一些命令,可以参照常用MySQL命令总结(数据库学习)

  • 一、添加Mybatis的起步依赖
<!--mybatis起步依赖-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.0.1</version>
</dependency>

一般官方Mybatis给的坐标:<artifactId>mybatis-spring-boot-starter</artifactId>,

Spring提供的坐标:<artifactId>spring-boot-starter-data-redis</artifactId>

  • 二、添加数据库驱动坐标
<!-- MySQL连接驱动 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
  • 三、在application.properties文件中,添加数据库链接信息
#DB Configuration:
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
#pojo别名扫描包
mybatis.type-aliases-package=com.yingqi.domain

我们知道,SpringBoot中整合了很多很多的配置,也就是说以前我们使用的很多配置文件是不适合的了,SpringBoot中有特定的配置名称,那么我们可以在上一章中提到的三种方法选择一种。

  • 四、在MySQL中创建数据库和表
CREATE DATABASE test;
CREATE TABLE `user` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
INSERT INTO `user` VALUES ('1', '张三');
INSERT INTO `user` VALUES ('2', '李四');
  • 五、创建实体Bean
package com.yingqi.doman;

public class User {
    private int id;
    private String name;
    //tostring、get和set方法
}
  • 六、编写Mapper
package com.yingqi.mapper;

import com.yingqi.doman.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
@Mapper
public interface UserMapper {
    @Select("select * from user")
    public List<User> find();
}
  • 七、编写引导类
package com.yingqi;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.yingqi.mapper")
public class QuickStartOneApplication {
    public static void main(String[] args) {
        SpringApplication.run(QuickStartOneApplication.class);
    }
}
  • 八、编写测试类

由于环境中没有junit,需要添加测试的环境依赖

<!--测试的起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

测试find方法是否正常

import com.yingqi.QuickStartOneApplication;
import com.yingqi.doman.User;
import com.yingqi.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = QuickStartOneApplication.class)
public class mapperTest {
    @Autowired
    private UserMapper userMapper;
    @Test
    public void findTest(){
        List<User> users = userMapper.find();
        System.out.println(users);
    }

}

正常输出

 刚刚我运行的时候出现了一个问题,一直报数据库错误,显示时区错误,后面找了一下,发现url中要加入&serverTimezone=UTC属性。

SpringBoot整合Redis

  • 一、添加Redis的起步依赖
<!-- 配置使用redis启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
  • 二、在application.properties文件中,添加Redis连接信息
#Redis
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
  • 三、编写测试类
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yingqi.QuickStartOneApplication;
import com.yingqi.doman.User;
import com.yingqi.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = QuickStartOneApplication.class)
public class redisTest {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Test
    public void test() throws JsonProcessingException {
//从redis缓存中获得指定的数据
        String userListData = redisTemplate.boundValueOps("user.find").get();
//如果redis中没有数据的话
        if (null == userListData) {
//查询数据库获得数据
            List<User> list = userMapper.find();
//转换成json格式字符串
            ObjectMapper objectMapper = new ObjectMapper();
            userListData = objectMapper.writeValueAsString(list);
//将数据存储到redis中
            redisTemplate.boundValueOps("user.find").set(userListData);
            System.out.println("从数据库");
        } else {
            System.out.println("从redis缓存");
        }
        System.out.println(userListData);
    }
}


正常输出

  1. 第一次,
  2. 第二次,

总结

我们整合了Mybatis和Redis,发现是有一些规律的,先导入起步依赖,再配置连接信息,与SpringBoot建立联系(例:@MapperScan("x'x'x"))再写业务代码。

猜你喜欢

转载自blog.csdn.net/weixin_43126117/article/details/94015786
今日推荐