【SpringBoot】整合
【摘要】 本文主要讲解Springboot整合。
一、整合Junit
使用@SpringBootTest注解可以定义测试类,要保证测试类在启动类在同一个包下,或者在启动类所处的类的子包下;如果不符合,要使用@SpringBootTest(classes = Demo3Application.class)
下面通过service层和实现类模拟测试
public interface UserService {
void say();
}
@Service
public class UserServiceImpl implements UserService {
@Override
public void say() {
System.out.println("user service --> ");
}
}
@SpringBootTest
class Demo3ApplicationTests {
@Autowired
private UserService userService;
@Test
public void testSay() {
userService.say();
}
}
步骤:
(1)新建SpringBoot项目,勾选Mybatis和Mysql依赖,并导入Druid连接池。
<!-- druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.10</version>
</dependency>
(2)在application.yml文件配置数据库连接相关信息
(3)根据数据库表编写实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Book {
private Integer id;
private String bookType;
private String bookName;
private String bookDesc;
}
(4)编写Mapper层接口
@Mapper
public interface BookMapper {
@Select("SELECT * FROM book WHERE id = #{id}")
@Results({@Result(column = "book_type",property = "bookType"),@Result(column = "book_name",property = "bookName"),@Result(column = "book_desc",property = "bookDesc")})
Book selectById(@Param("id") Integer id);
}
(5)测试并对比结果
@SpringBootTest
public class BookMapperTest {
@Autowired
private BookMapper mapper;
@Test
public void testSelectById() {
Book book = mapper.selectById(2);
System.out.println(book);
}
}
使用index.html设置访问首页跳转:
<script>
document.location.href="pages/books.html"
</script>
【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)