1. 整合JUnit
问题导入
回忆一下Spring整合JUnit的步骤?
1.1 Spring整合JUnit(复习)
1.2 SpringBoot整合JUnit
【第一步】添加整合junit起步依赖(可以直接勾选)
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope>
</dependency>
【第二步】编写测试类,默认自动生成了一个
@SpringBootTest
class Springboot07JunitApplicationTests {@Autowiredprivate BookService bookService;@Testpublic void testSave() {bookService.save();}
}
2. 基于SpringBoot实现SSM整合
问题导入
回忆一下Spring整合MyBatis的核心思想?
2.1 Spring整合MyBatis(复习)
-
SpringConfig
-
导入JdbcConfig
-
导入MyBatisConfig
-
@Configuration
@ComponentScan("com.itheima")
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class, MyBatisConfig.class})
public class SpringConfig {}
JDBCConfig
-
定义数据源(加载properties配置项:driver、url、username、password)
#jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_db
jdbc.username=root
jdbc.password=itheima
public class JdbcConfig {@Value("${jdbc.driver}")private String driver;@Value("${jdbc.url}")private String url;@Value("${jdbc.username}")private String userName;@Value("${jdbc.password}")private String password;@Beanpublic DataSource getDataSource() {DruidDataSource ds = new DruidDataSource();ds.setDriverClassName(driver);ds.setUrl(url);ds.setUsername(userName);ds.setPassword(password);return ds;}
}
MyBatisConfig
-
定义SqlSessionFactoryBean
-
定义映射配置
@Bean
public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource dataSource) {SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();ssfb.setTypeAliasesPackage("com.itheima.domain");ssfb.setDataSource(dataSource);return ssfb;
}
@Bean
public MapperScannerConfigurer getMapperScannerConfigurer() {MapperScannerConfigurer msc = new MapperScannerConfigurer();msc.setBasePackage("com.itheima.dao");return msc;
}
2.2 SpringBoot整合MyBatis
-
SpringBoot整合Spring(不存在)
-
SpringBoot整合SpringMVC(不存在)
-
SpringBoot整合MyBatis(主要)
①:创建新模块,选择Spring初始化,并配置模块相关基础信息
②:选择当前模块需要使用的技术集(MyBatis、MySQL)
③:设置数据源参数
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTCusername: rootpassword: roottype: com.alibaba.druid.pool.DruidDataSource
注意事项:
-
SpringBoot版本低于2.4.3(不含),Mysql驱动版本大于8.0时,需要在url连接串中配置时区,或在MySQL数据库端配置时区解决此问题
jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
④:定义数据层接口与映射配置
@Mapper
public interface UserDao {@Select("select * from tbl_book where id=#{id}")Book getById(Integer id);
}
⑤:测试类中注入dao接口,测试功能
@SpringBootTest
class Springboot08MybatisApplicationTests {@Autowiredprivate BookDao bookDao;@Testpublic void testGetById() {Book book = bookDao.getById(1);System.out.println(book);}
}
2.3 案例-SpringBoot实现ssm整合
【第一步】创建SpringBoot工程,添加druid依赖
<!-- todo 1 添加druid连接池依赖-->
<dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.6</version>
</dependency>
【第二步】复制springmvc_11_page工程各种资源(主java类、页面、测试类)
【第三步】删除config包中的所有配置,在BookDao接口上加@Mapper注解
//todo 3 在BookDao接口上加@Mapper注解,让SpringBoot给接口创建代理对象
@Mapper
public interface BookDao {//...
}
【第四步】将application.properties修改成application.yml,配置端口号和连接参数
server:port: 80
# todo 4 配置数据库连接参数
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/ssm_dbusername: rootpassword: roottype: com.alibaba.druid.pool.DruidDataSource
【第五步】修改BookServiceTest配置类,进行配置
// todo 5 修改单元测试类,添加@SpringBootTest主键,修复@Test注解导包
@SpringBootTest
public class BookServiceTest {@Autowiredprivate BookService bookService;@Testpublic void testGetById(){Book book = bookService.getById(2); //传递参数1会抛出异常System.out.println(book);}@Testpublic void testGetAll(){List<Book> all = bookService.getAll();System.out.println(all);}
}
【第六步】在static目录中提供index.html页面,跳转到"pages/books.html"
<script>location.href="pages/books.html"
</script>
最后:运行引导类即可访问