一、整合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(); } }
二、整合mybatis
步骤:
(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); } }
三、基于SpringBoot整合SSM
使用index.html设置访问首页跳转:
<script> document.location.href="pages/books.html" </script>