整合junit
创建一个DAO类和接口
![](https://img-blog.csdnimg.cn/img_convert/a63e84fde180b82e5a1d4fc6f2638835.png)
![](https://img-blog.csdnimg.cn/img_convert/205059da867b1b8947a49df6fa514f46.png)
进行boot测试:
![](https://img-blog.csdnimg.cn/img_convert/f488a73840ed3772972092feb086de82.png)
![](https://img-blog.csdnimg.cn/img_convert/0e74162634adb391a09f287ae67ee1d6.png)
点击运行contextLoads测试方法
运行结果:
![](https://img-blog.csdnimg.cn/img_convert/a8f9420c2f72f265c7a3be99d2de08b4.png)
总结:
![](https://img-blog.csdnimg.cn/img_convert/b5bf14de5089382aff9bf6deb197e5a0.png)
![](https://img-blog.csdnimg.cn/img_convert/90611e1306623ff1b3963241de99baad.png)
整合Mybatis
创建一个新模块
![](https://img-blog.csdnimg.cn/img_convert/489355b99dd55399deb5d5f4f14731fb.png)
实现在数据库里放置数据(数据名称为ssm_db):
![](https://img-blog.csdnimg.cn/img_convert/1b03ca7cab84f4001742189e9800de0f.png)
在yml中配置相关信息,设置数据源参数:
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db
username: root
password: 123456
创建一个实体类Book:
![](https://img-blog.csdnimg.cn/img_convert/7539c9b924249e9229c6a4a040778867.png)
设置属性:
![](https://img-blog.csdnimg.cn/img_convert/c1827775a2f02087ac0d4afa7b83132b.png)
再添加相应的get,set,toString方法
再创建BookDao接口,定义数据层接口与映射配置
![](https://img-blog.csdnimg.cn/img_convert/77c8f468235c50b1152ce8690db563bf.png)
![](https://img-blog.csdnimg.cn/img_convert/05cade6f13697083334886a08f743a67.png)
测试类中注入dao接口,测试功能:
![](https://img-blog.csdnimg.cn/img_convert/43b24ca6a264f07605a912d8cbf546e1.png)
![](https://img-blog.csdnimg.cn/img_convert/655a43817347148bd397e1da9b2c3127.png)
运行contextLoads():
![](https://img-blog.csdnimg.cn/img_convert/6bff46e98ad58536db6e6c56e910e3e3.png)
但是会有一行红字:Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
把yml配置文件里的com.mysql.jdbc.Driver(这个驱动过时了)改成com.mysql.cj.jdbc.Driver即可