Note/JAVA/Springboot/2.整合JUnit、Mybatis.md

66 lines
1.2 KiB
Markdown
Raw Normal View History

2023-08-31 11:30:31 +08:00
Spring整合JUnit的制作方式
```java
//加载spring整合junit专用的类运行器
@RunWith(SpringJUnit4ClassRunner.class)
//指定对应的配置信息
@ContextConfiguration(classes = SpringConfig.class)
public class AccountServiceTestCase {
//注入你要测试的对象
@Autowired
private AccountService accountService;
@Test
public void testGetById(){
//执行要测试的对象对应的方法
System.out.println(accountService.findById(2));
}
}
```
Spring整合Mybatis的制作方式yml格式
```yml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db?useSSL=false
username: root
password: 8520
```
实体类
```JAVA
public class Book {
private Integer id;
private String type;
private String name;
private String description;
}
```
映射接口Dao
```JAVA
@Mapper
public interface BookDao {
@Select("select * from tbl_book where id = #{id}")
public Book getById(Integer id);
}
```
测试类
```JAVA
@SpringBootTest
class Springboot05MybatisApplicationTests {
@Autowired
private BookDao bookDao;
@Test
void contextLoads() {
System.out.println(bookDao.getById(1));
}
}
```