SpringBoot框架复习(三)集成其他技术

SpringBoot集成MyBatis

  1. 添加起步依赖
<!--mybatis起步依赖-->
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>1.1.1</version>
</dependency>
  1. 添加数据库驱动坐标
<!-- MySQL连接驱动 -->
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
</dependency>
  1. 添加数据库连接信息
# 数据库信息
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
  1. 创建实体类
  • 注意:@Mapper标记该类是一个mybatis的mapper接口,可以被spring boot自动扫描到spring上下文中
public class User implements Serializable {

    private String id;
    private String name;
    private String email;
    private String telephone;
    private String password;
    private Integer root;

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getRoot() {
        return root;
    }

    public void setRoot(Integer root) {
        this.root = root;
    }

    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", email='" + email + '\'' +
                ", telephone='" + telephone + '\'' +
                ", password='" + password + '\'' +
                ", root=" + root +
                '}';
    }
}
  1. 编写Mapper
@Mapper
public interface UserMapper {

    public List<User> findAll();
}
  1. 配置Mapper映射文件
  • 在src\main\resources\mapper路径下加入UserMapper.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.lwb.Mapper.UserMapper">
    <select id="findAll" resultType="user">
        select * from user
    </select>
</mapper>
  1. 在application.properties中添加mybatis的信息
#spring集成Mybatis环境
#pojo别名扫描包
mybatis.type-aliases-package=com.lwb.domain
#加载Mybatis映射文件
mybatis.mapper-locations=classpath:Mapper/*Mapper.xml

SpringBoot整合JUNIT

  1. 添加Junit的起步依赖
<!--测试的起步依赖-->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>
  1. 编写测试类
@RunWith(SpringRunner.class)
@SpringBootTest(classes = application.class)
public class test {

    @Test
    public void test1(){

    }
}

SpringBoot整合Redis

  1. 添加redis的起步依赖
<!-- 配置使用redis启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
  1. 配置redis的连接信息
#Redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
  1. 注入RedisTemplate测试redis操作
@RunWith(SpringRunner.class)
@SpringBootTest(classes = application.class)
public class RedisTest {
	@Autowired
	private UserRepository userRepository;
	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	@Test
	public void test() throws JsonProcessingException {
		//从redis缓存中获得指定的数据
		String userListData = redisTemplate.boundValueOps("user.findAll").get();
		//如果redis中没有数据的话
		if(null==userListData){
		//查询数据库获得数据
		List<User> all = userRepository.findAll();
		//转换成json格式字符串
		ObjectMapper om = new ObjectMapper();
		userListData = om.writeValueAsString(all);
		//将数据存储到redis中,下次在查询直接从redis中获得数据,不用在查询数据库
		redisTemplate.boundValueOps("user.findAll").set(userListData);
		System.out.println("===============从数据库获得数据===============");
		}else{
		System.out.println("===============从redis缓存中获得数据===============");
		}
		System.out.println(userListData);
	}
}

你可能感兴趣的:(SpringBoot,数据库,mybatis,mysql,spring,java)