SpringBoot integrates other frameworks
1.Junit
Introduction to Junit: Getting started with Junit
Implementation steps
-
Build a SpringBoot project
-
Introduce the starter-test starting dependency (IDEA will automatically import it, which can be viewed in the pom file)
-
Writing a test class
IDEA will automatically create a package that is the same as the main package (the package where the boot class in main is located), and the test class of the boot class will be automatically created in the package
-
Add test-related comments
- @RunWith(SpringRunner.class)
- @SpringBootTest(classes = startup class.class)
Note: The annotation @SpringBootTest of the automatically generated test class does not have the following brackets.
This is because, when the package where the test class is located is the same as the main package (the package where the boot class in main is located) or a subpackage of the main package, the startup class will be automatically found. In this case, @SpringBootTest does not need to specify the startup classpackage 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 { }
-
write test method
/** * UserService类的测试类 */ @RunWith(SpringRunner.class) @SpringBootTest(classes = SpringbootTestApplication.class) public class UserServiceTest { @Autowired private UserService userService; @Test public void testAdd() { userService.add(); } }
Click the arrow to the left of the test function to run the test, and the test passes
2.Redis
Introduction to Redis: Redis Comprehensive Tutorial
Implementation Steps
-
Build a SpringBoot project
Select Reids when building a SpringBoot project
-
Introduce Redis start-up dependency (IDEA auto-completion)
-
Configure Redis related properties
and configure them in application.properties or application.ymlredis: host: 127.0.0.1 #配置主机ip port: 6379 #配置端口 password: 123456 #redis密码
-
Inject the RedisTemplate template
@SpringBootTest public class SpringbootRedisApplicationTests { //注入模板 @Autowired private RedisTemplate redisTemplate; }
Note: An error may be reported when injecting the template, indicating that it cannot be automatically injected, but it does not affect the operation.
This problem may be a problem with the SpingBoot version. All 2.7.* versions will report an error. You can reduce the version to eliminate this error. -
write test method
@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); } }
Click the green arrow on the left side of the test function to test, and all the tests pass.
Question: In the actual test, the name of the key-value pair stored in redis is different from that in IDEA. The specific reason may need to be solved by subsequent study.
MyBatis
Introduction to MyBatis: Detailed
implementation steps of MyBatis
-
Build a SpringBoot project
and select two dependencies
-
Introduce mybatis starting dependency, add mysql driver (IDEA automatically completes)
-
Write DataSource (and MyBatis) related configuration
-
Define tables and entity classes
-
Write dao and mapper files/pure annotation development
-
test
Pure annotation development (recommended)
Write DataSource in application file
# datasource
spring:
datasource:
url: jdbc:mysql:///springboot
username: root
password: 030322Lr
driver-class-name: com.mysql.cj.jdbc.Driver
Import tables in the database and create entity classes
package com.study.springbootmybatis.domain;
//实体类User.java
public class User {
private int id;
private String username;
private String password;
}
Write entity class
package com.study.springbootmybatis.domain;
public class User {
private int id;
private String username;
private String password;
}
Write the Mapper interface
//UserMapper接口
package com.study.springbootmybatis.mapper;
@Mapper
public interface UserMapper {
@Select("select * from t_user")
public List<User> findAll();
}
test
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 configuration method
Configure DataSource and MyBatis in the application file
# 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的核心配置文件
Create a new xml configuration file in resources
<?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>
Entity classes are the same as pure annotation development
Write the Mapper interface
package com.study.springbootmybatis.mapper;
@Mapper
public interface UserXMLMapper {
//没有加注解
public List<User> findAll();
}
test
package com.study.springbootmybatis;
@SpringBootTest
class SpringbootMybatisApplicationTests {
@Autowired
private UserXMLMapper userXMLMapper;
@Test
public void testXMLFindAll() {
List<User> list = userXMLMapper.findAll();
System.out.println(list);
}
}