1整合junit
1.1spring initializr
1.2pom.xml添加junit依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
1.3启动类SpringbootTestApplication
@SpringBootApplication
public class SpringbootTestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootTestApplication.class, args);
}
}
1.4业务代码
@Service
public class UserService {
public void add() {
System.out.println("add...");
}
}
1.5测试代码,然后
/**
* userService的测试类
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootTestApplication.class)
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void testAdd() {
userService.add();
}
}
链接:https://pan.baidu.com/s/1Ek_5Mv4kb2UCb3n9bUNDHg
提取码:i3xr
复制这段内容后打开百度网盘手机App,操作更方便哦–来自百度网盘超级会员V1的分享
2整合redis
2.1pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.2启动本地redis
2.3application.yml
spring:
redis:
host: 127.0.0.1 # redis的主机ip
port: 6379
2.4测试类
@RunWith(SpringRunner.class)
@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);
}
}
设置name属性
读取name属性
链接:https://pan.baidu.com/s/1fwKfyXw3lqXpul_o9RtmIA
提取码:dg7z
复制这段内容后打开百度网盘手机App,操作更方便哦–来自百度网盘超级会员V1的分享
3整合mybatis
3.1建造数据库,初始数据在user.sql中
3.2pom.xml
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!--<scope>runtime</scope>-->
</dependency>
3.3application.yml
# datasource
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/springboot?serverTimezone=UTC
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
# mybatis
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml # mapper映射文件路径
type-aliases-package: com.itheima.springbootmybatis.domain
# config-location: # 指定mybatis的核心配置文件
3.4User.java
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
3.5mapper.xml
<select id="findAll" resultType="User">
select * from t_user
</select>
3.6UserMapper
@Mapper
@Repository
public interface UserXmlMapper {
public List<User> findAll();
}
3.7测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootMybatisApplicationTests {
@Autowired
private UserXmlMapper userXmlMapper;
@Test
public void testFindAll2() {
List<User> list = userXmlMapper.findAll();
System.out.println(list);
}
}
链接:https://pan.baidu.com/s/17HZylmNpS91Wdt66loww1g
提取码:6lbd
复制这段内容后打开百度网盘手机App,操作更方便哦–来自百度网盘超级会员V1的分享