文章目录
整合mybatis-plus
- 整合mybatis-plus
- 测试mybatis-plus集成
1.导入依赖
com.baomidou
mybatis-plus-boot-starter
3.2.0
2.配置 1.配置数据源
1.导入数据库的驱动, 此项目为mysql数据库, 因此导入Mysql 的驱动
2.在applicaion.yml中配置数据库的用户名 密码, 地址等
spring:
datasource:
#MySQL配置
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.56.10:3306/gulimall_pms?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: root
- 配置mp 1. 在主启动类中,使用@MapperScan注解, 指定dao层接口的包位置
2.在applicaion.yml指定xml的位置
mybatis-plus:
mapper-locations: classpath:/mapper/**/*.xml
global-config:
db-config:
id-type: auto #自增主键
测试mybatis-plus集成
@RunWith(SpringRunner.class)
@SpringBootTest
class GulimallProductApplicationTests {
@Autowired
private BrandService brandService ;
@Test
void contextLoads() {
BrandEntity brand = new BrandEntity();
brand.setName("华为");
brandService.save(brand);
System.out.println("保存成功!!! ");
}
}
执行完成, contextLoads方法后, 可以在数据库中看到如下的数据, 代表新增成功, 并且代码中没有指定id ,成功的用了自增的主键 测试查询. 查询id为1的数据
@Test
void selectListTest() {
List brandList = brandService.list(new QueryWrapper().eq("brand_id", 1L));
System.out.println(" 查询成功! ");
brandList.forEach((brand)->{
System.out.println(brand);
});
}
控制台打印如下, 代表查询成功! 其他模块 按照同样的步骤生成代码, 并且复制application.yml文件 , 修改成对应的数据库和 端口号. 并在启动类上添加上对应的dao层接口.