SpringBoot学习笔记03

SpringBoot 整合其他框架

1.Junit

Junit简介:Junit入门使用教程

实现步骤

  1. 搭建SpringBoot工程

  2. 引入starter-test起步依赖(IDEA会自动引入,可在pom文件中查看)

  3. 编写测试类
    IDEA会自动创建一个与主包(main中引导类所在的包)相同的包,包中会自动创建引导类的测试类
    在这里插入图片描述

  4. 添加测试相关注解

    • @RunWith(SpringRunner.class)
    • @SpringBootTest(classes = 启动类.class)

    注意:自动生成的测试类的注解@SpringBootTest没有后面的括号。
    这是因为,当测试类所在的包与主包(main中引导类所在的包)相同或者为主包的子包时,会自动寻找启动类,这种情况下@SpringBootTest不需要指定启动类

    package com.study.springboottest;
    
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    
    @RunWith(SpringRunner.class)
    @SpringBootTest //与主包同名,无须指定启动类
    public class SpringbootTestApplicationTest {
    }
    
    package com.study.test;
    
    import com.study.springboottest.SpringbootTestApplication;
    import com.study.springboottest.UserService;
    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;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = SpringbootTestApplication.class)//与主包及其子包不同名则需要指定测试类
    public class UserServiceTest {
    }
    
  5. 编写测试方法

    /**
     * UserService类的测试类
     */
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = SpringbootTestApplication.class)
    public class UserServiceTest {
    
    	@Autowired
    	private UserService userService;
    
    	@Test
    	public void testAdd() {
    		userService.add();
    	}
    }
    

    点击测试函数左侧的箭头运行测试,测试通过
    在这里插入图片描述

2.Redis

Redis简介:Redis全面教程
实现步骤

  1. 搭建SpringBoot工程
    在构建SpringBoot项目的时候选择Reids
    在这里插入图片描述

  2. 引入Redis起步依赖(IDEA自动完成)

  3. 配置Redis相关属性
    在application.properties或application.yml中进行配置

    redis:
    host: 127.0.0.1    #配置主机ip
    port: 6379         #配置端口
    password: 123456   #redis密码
    
  4. 注入RedisTemplate模板

    @SpringBootTest
    public class SpringbootRedisApplicationTests {
    
    	//注入模板
    	@Autowired
    	private RedisTemplate redisTemplate;
    
    }
    

    注意:注入模板时可能会报错,提示不能自动注入,但是不影响运行
    这个问题可能是SpingBoot版本的问题,所有2.7.*的版本都会报错,可以降低版本来消除此报错

  5. 编写测试方法

    @SpringBootTest
    public class SpringbootRedisApplicationTests {
    
    	@Autowired
    	private RedisTemplate redisTemplate;
    
    	@Test
    	public void testSet() {
    		//存入数据
    		redisTemplate.boundValueOps("name").set("zhangsan");
    	}
    
    	@Test
    	public void testGet() {
    		//获取数据
    		Object name = redisTemplate.boundValueOps("name").get();
    		System.out.println(name);
    	}
    }
    

    点击测试函数左侧的绿色箭头进行测试,均测试通过
    在这里插入图片描述
    疑问:实际测试中,存入redis的键值对名称与IDEA中的并不相同,具体什么原因可能需要后续学习解决
    在这里插入图片描述

MyBatis

MyBatis简介: MyBatis详解
实现步骤

  1. 搭建SpringBoot工程
    选择两个依赖
    在这里插入图片描述

  2. 引入mybatis起步依赖,添加mysql驱动(IDEA自动完成)

  3. 编写DataSource(和MyBatis)相关配置

  4. 定义表和实体类

  5. 编写dao和mapper文件/纯注解开发

  6. 测试

纯注解开发(推荐)

在application文件中编写DataSource

# datasource
spring:
 	  datasource:
  		url: jdbc:mysql:///springboot
  		username: root
   	password: 030322Lr
   	driver-class-name: com.mysql.cj.jdbc.Driver

在数据库中导入表,然后创建实体类

package com.study.springbootmybatis.domain;
//实体类User.java
public class User {

	private int id;
	private String username;
	private String password;
}

编写实体类

package com.study.springbootmybatis.domain;

public class User {

	private int id;
	private String username;
	private String password;
}

编写Mapper接口

//UserMapper接口
package com.study.springbootmybatis.mapper;

@Mapper
public interface UserMapper {

	@Select("select * from t_user")
	public List<User> findAll();
}

测试

package com.study.springbootmybatis;

@SpringBootTest
class SpringbootMybatisApplicationTests {

	@Autowired
	private UserMapper userMapper;

	@Test
	public void testFindAll() {
		List<User> list = userMapper.findAll();
		System.out.println(list);
	}
}

XML配置方式

在application文件中配置DataSource和MyBatis

# datasource
spring:
  datasource:
    url: jdbc:mysql:///springboot
    username: root
    password: 030322Lr
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml # mapper的映射文件路径
  type-aliases-package: com.study.springbootmybatis.domain # User实体类所在的包
  # config-location: # 指定mybatis的核心配置文件

在resources中新建一个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.study.springbootmybatis.mapper.UserXMLMapper">
    <select id="findAll" resultType="user">
        select * from t_user
    </select>
</mapper>

实体类与纯注解开发相同

编写Mapper接口

package com.study.springbootmybatis.mapper;

@Mapper
public interface UserXMLMapper {

	//没有加注解
	public List<User> findAll();
}

测试

package com.study.springbootmybatis;

@SpringBootTest
class SpringbootMybatisApplicationTests {

	@Autowired
	private UserXMLMapper userXMLMapper;

	@Test
	public void testXMLFindAll() {
		List<User> list = userXMLMapper.findAll();
		System.out.println(list);
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值