SpringBoot整合第三方技术

整合第三方技术

整合JUnit

Spring整合JUnit(复习)

@RunWith(SpringJUnit4ClassRunner.class)				//设置运行器
@ContextConfiguration(classes = SpringConfig.class)  //加载配置环境
public class UserServiceTest{
	@Autowired
	private BookService bookService;				//注入测试对象
	
	@Test											//功能测试
	public void testSave(){
		bookService.save()
	}
}

SpringBoot整合JUnit

@SpringBootTest
class SpringbootApplicationTests{
	@Autowired
	private BookService bookService;
	@Test
	public void testSave(){
		bookService.save();
	}
}

新注解学习

  • 名称:@SpringBootTest

  • 类型:测试类注解

  • 位置:测试类定义上方

  • 作用:设置JUnit加载的SpringBoot启动类

  • 范例:

    @SpringBootTest(classes = SpringbootJunitApplication.class)
    class SpringbootJunitApplicationTests{}
    
  • 相关属性

    • classes: 设置SpringBoot启动类

注意事项

  • 如果测试类再SpringBoot启动类的包或子包中,可以省略启动类的设置,也就是省略classes的设定
整合SSM
基于SpringBoo实现SSM整合
  • SpringBoot整合Spring(不存在)
  • SpringBoot整合SpringMvc(不存在)
  • SpringBoot整合MyBatis(主要)
Spring整合MyBatis(复习)
  • Spring整合MyBatis(复习)
    • SpringConfig
      • 导入JdbcConfig
      • 导入MyBatisConfig
    • JDBCConfig
      • 定义数据源(加载properties配置项:driver、url、username、password)
    • MyBatisConfig
      • 定义SqlSessionFactoryBean
      • 定义映射配置
SpringBoot整合MyBatis
  1. 创建模块,选择Spring初始化,并配置模块相关基础信息

  2. 选择当前模块需要使用的技术集(MyBatis、MySQL)

  3. 设置数据源参数

    spring:
      datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/ssm_db
        username: root
        password: ******
    

    注意事项:

    SpringBoot版本低于2.4.3(不含),Mysql驱动版本大于8.0时,需要再url连接串中配置时区

    jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
    

    或再MySQL数据库端配置时区解决此问题

  4. 定义数据层接口与映射配置

    @Mapper
    public interface BookDao {
        @Select("select * from tbl_book where id = #{id}")
        public Book getById(Integer id);
    }
    
  5. 测试类中注入dao接口,测试功能

    @SpringBootTest
    class SpringbootMybatisApplicationTests {
        @Autowired
        private BookService bookService;
    
        @Test
        void contextLoads() {
            Book book = bookService.getById(1);
            System.out.println(book);
        }
    
    }
    
基于SpringBoot的SSM整合案例
  1. pom.xml

    配置起步依赖,必要的资源坐标

  2. application.yml

    设置数据源、端口等

  3. 配置类

    全部删除

  4. dao

    设置@Mapper

  5. 测试类

  6. 页面

posted @ 2022-12-07 22:07  筝弈  阅读(33)  评论(0编辑  收藏  举报
2 3
4