一、SpringBoot整合Junit
1.使用Idea快速搭建SpringBoot工程
或在pom.xml中引入依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency>
2.创建Service类
package com.example.springboottest.service; import org.springframework.stereotype.Service; @Service public class UserSerive { public void add(){ System.out.println("我是添加方法...."); } }
3.编写测试类,添加测试相关注解
• @RunWith(SpringRunner.class)
• @SpringBootTest(classes = 启动类.class)
package com.example.springboottest.service; 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 //这里测试类和被测试的类在同一目录下可不指定启动类.class public class UserServiceTest { @Autowired private UserSerive userSerive; @Test public void testAdd(){ userSerive.add(); } }
二、SpringBoot整合Redis
1.使用Idea快速搭建SpringBoot工程,在nosql勾选redis服务。
或在pom.xml中加入依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
2.在配置文件application.yml中加入redis配置信息
spring: redis: host: 127.0.0.1 port: 6379 #这里根据自己的需求添加redis配置信息
3. 编写测试方法,注入RedisTemplate模板,成功写入和读取Redis信息
package com.example.springbootredis; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; @SpringBootTest class SpringbootRedisApplicationTests { @Autowired private RedisTemplate redisTemplate; @Test void setData() { redisTemplate.boundValueOps("name").set("你好~"); } @Test void getData() { Object name = redisTemplate.boundValueOps("name").get(); System.out.println(name); } }
三、SpringBoot整合MyBatis
1.使用Idea快速搭建SpringBoot工程,在SQL中勾选MyBatis Framework和MySql Driver服务。
2.在配置文件application.yml中加入mysql配置信息
spring: datasource: url: jdbc:mysql://IP地址:3306/数据库名?serverTimezone=Asia/Shanghai&useSSL=false&characterEncoding=utf8 username: root password: xxxx driver-class-name: com.mysql.cj.jdbc.Driver
3.创建数据库表对应的实体类
package com.example.springbootmybatis.entity; public class WpTerms { private Long termId; private String name; private String slug; private Integer termGroup; public Long getTermId() { return termId; } public void setTermId(Long termId) { this.termId = termId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSlug() { return slug; } public void setSlug(String slug) { this.slug = slug; } public Integer getTermGroup() { return termGroup; } public void setTermGroup(Integer termGroup) { this.termGroup = termGroup; } }
4.编写sql语句以及测试读取数据
注解方式:在mapper文件中加入注解直接写sql语句
package com.example.springbootmybatis.mapper; import com.example.springbootmybatis.entity.WpTerms; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import java.util.List; @Mapper public interface UserMapper { @Select("select `term_id` as termId,name,slug,term_group as termGroup from wp_terms") List<WpTerms> findAll(); }
在xml映射文件中写sql语句
在配置文件application.yml中加入mybatis配置信息
mybatis: mapper-locations: classpath:mapper/*Mapper.xml #mapper的映射路径 # config-location: 指定mybatis的核心配置文件
在resources下创建mapper文件,然后在mapper里创建WpTermsMapper.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.example.springbootmybatis.mapper.WpTermsMapper" > <resultMap id="BaseResultMap" type="com.example.springbootmybatis.entity.WpTerms" > <id column="term_id" property="termId" jdbcType="INTEGER" /> <result column="name" property="name" jdbcType="VARCHAR" /> <result column="slug" property="slug" jdbcType="VARCHAR" /> <result column="term_group" property="termGroup" jdbcType="INTEGER" /> </resultMap> <select id="selectAll" resultMap="BaseResultMap" > select * from wp_terms </select> </mapper>
文章评论