当前位置:   article > 正文

SpringBoot整合第三方技术与MP常用功能_springboot mp

springboot mp

目录

SpringBoot整合第三方技术

1、整合Junit

 @Autowired出现找不到注入对象的原因 

classes属性

2、整合MyBatis

mysql8遇到时区错误 

3、整合Mybatis-plus

出现No qualifying bean of type 'com.dao.PersonDao' available错误

MyBatisPlus常用功能 

1、 给Mp开启日志

2、 MP的分页功能

3、 MP条件查询功能

4、Mp的业务层快速开发


SpringBoot整合第三方技术

1、整合Junit

创建一个Dao包下的UserDao

  1. package com.Dao;
  2. public interface UserDao {
  3. void save();
  4. }

创建实现类

  1. package com.Dao.Impl;
  2. import com.Dao.UserDao;
  3. import org.springframework.stereotype.Repository;
  4. //要使能被spring管理,加上注解@Component(spring管理的通用注解)或@Repository(更合适)
  5. @Repository
  6. public class UserDaoImpl implements UserDao {
  7. @Override
  8. public void save() {
  9. System.out.println("userDao is running...");
  10. }
  11. }

在测试下

  1. package com.springbootjunit04;
  2. import com.Dao.UserDao;
  3. import org.junit.jupiter.api.Test;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. @SpringBootTest
  7. class Springbootjunit04ApplicationTests {
  8. //1、注入想要让spring管理的值
  9. @Autowired
  10. private UserDao userDao;
  11. @Test
  12. void contextLoads() {
  13. //2、执行你要测试对象对应的方法
  14. userDao.save();
  15. }
  16. }

运行结果

 @Autowired出现找不到注入对象的原因 

 出现这种错误是因为springboot默认扫描这个启动类所在的包及其子包,所以就无法识别了,只需要将启动类移动到编写的Dao包之上即可重新被识别。

 核心:@SpringBootTest

名称:@SpringBootTest

类型:测试类定义上方

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

  1. @SpringBootTest
  2. class Springbootjunit04ApplicationTests {}

整合Junit步骤:

1、导入测试对应的starter(springboot已经在pom.xml完成)

2、测试类使用SpringBootTest注解(SpringBoot完成了)

3、使用自动装配的形式添加要测试的对象

classes属性

相关属性:设置SpringBoot启动类

我们一开始设置在com包下的Test类,移出去启动后就会报错,原因是一开始在com包,springboot就会去同名上的com包下去找,如果这个test类在引导类的包下及其子包下则不会报错。报错的原因是test类找不到引导类(启动类)

 所有只需要使用classes标签

@SpringBootTest(classes = Springbootjunit04Application.class)

 如果测试类在SpringBoot启动类的包或子包中,可以省略启动类的设置,也就是省略classes的设定

2、整合MyBatis

创建一个新的模块

配置文件中:配置数据库相关信息application.yml中

  1. #配置相关信息
  2. spring:
  3. datasource:
  4. driver-class-name: com.mysql.cj.jdbc.Driver
  5. url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
  6. username: root
  7. password: 123456

数据库中的表

 创建一个pojo包下的Person类

  1. package com.pojo;
  2. import lombok.Data;
  3. @Data
  4. public class Person {
  5. private Integer id;
  6. private String username;
  7. private String password;
  8. private String gender;
  9. private String addr;
  10. }

创建一个dao包下的PersonDao接口

  1. package com.dao;
  2. import com.pojo.Person;
  3. import org.apache.ibatis.annotations.Mapper;
  4. import org.apache.ibatis.annotations.Select;
  5. import org.springframework.stereotype.Component;
  6. import java.util.List;
  7. @Component//交给spring容器管理
  8. @Mapper//使用注解配置映射
  9. public interface PersonDao {
  10. @Select("select *from tb_user")
  11. List<Person> personList();
  12. }

在测试类下

  1. package com;
  2. import com.dao.PersonDao;
  3. import org.junit.jupiter.api.Test;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. @SpringBootTest
  7. class Springboot05MybatisApplicationTests {
  8. //测试,先注入dao对象
  9. @Autowired
  10. private PersonDao personDao;
  11. @Test
  12. void contextLoads() {
  13. System.out.println(personDao.personList());
  14. }
  15. }

运行结果:

 小结:

1、勾选Mybatis技术,也就是导入mybatis对应的starter

2、数据库连接相关信息转换成配置

3、数据库sql映射需要添加@Mapper被容器识别到

@Mapper注释用来表示该接口类的实现类对象交给mybatis底层创建,然后交由Spring框架管理

mysql8遇到时区错误 

mysql8以上的版本的加上时区?serverTimezone=UTC不然会报错

url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC

小结:

1\mysql8以上的版本驱动有前置要求设置时区

  • 修改url,添加sercerTimezone设定
  • 修改mysql数据库配置(永久设置)

2、驱动类过时,提醒更换为com.mysql.cj.jdbc.Driver

3、整合Mybatis-plus

使用aliyun创建springboot,或者导入Mybatisplus坐标

dao层的变为,不用写任何方法,点ctrl+鼠标点击进去BaseMapper中可以看到提供了很多方法。

  1. @Data
  2. @TableName("tb_user")//告诉是哪张表
  3. public class Person {
  4. private Integer id;
  5. private String username;
  6. private String password;
  7. private String gender;
  8. private String addr;
  9. }

这需要在pojo对象中加上,告诉mp是哪张表。@TableName("tb_user")

  1. package com.dao;
  2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  3. import com.pojo.Person;
  4. import org.apache.ibatis.annotations.Mapper;
  5. @Mapper//使用注解配置映射
  6. public interface PersonDao extends BaseMapper<Person> {
  7. }

测试类中

  1. package com;
  2. import com.dao.PersonDao;
  3. import com.pojo.Person;
  4. import org.junit.jupiter.api.Test;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. import org.springframework.stereotype.Repository;
  7. import javax.annotation.Resource;
  8. @SpringBootTest
  9. @MapperScan("com.dao")//扫描dao的包,扫描到mapper
  10. class Springboot06MybatisplusApplicationTests {
  11. @Resource
  12. private PersonDao personDao;
  13. @Test
  14. void contextLoads() {
  15. System.out.println(personDao.selectById(1));
  16. }
  17. }

 运行结果

Springboot整合mybatis-plus

 ①手动添加添加springboot整合mp的坐标,可以通过mvnrepository获取

 注:由于springboot没有收录mp的坐标版本,需要指定对应的version

②定义数据层接口与映射配置,继承BaseMapper

 测试类中注入dao接口,使用即可。

注:需要使用的第三方技术无法创建时勾选时,就手工添加

出现No qualifying bean of type 'com.dao.PersonDao' available错误

这个本质就是springboot找不到dao,只需要在启动类中加入@MapperScan("com.dao")就可以正常访问了

MyBatisPlus常用功能 

1、 给Mp开启日志

在配置文件中写入

  1. #打印日志
  2. mybatis-plus:
  3. configuration:
  4. #标准输出
  5. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

运行一个查看所有的方法,控制台输出如下 

2、 MP的分页功能

定义一个类做拦截器

  1. package com.config;
  2. import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
  3. import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. //分页相关
  7. @Configuration//是一个配置
  8. public class MPConfig {
  9. @Bean//给spring管理
  10. public MybatisPlusInterceptor mybatisPlusInterceptor(){
  11. //定义Mybatis拦截器
  12. MybatisPlusInterceptor interceptor=new MybatisPlusInterceptor();
  13. //添加具体的拦截器
  14. interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
  15. return interceptor;
  16. }
  17. }

 测试类下

  1. package com;
  2. @SpringBootTest
  3. class Springboot06MybatisplusApplicationTests {
  4. @Autowired(required = true)
  5. private PersonDao personDao;
  6. // 分页相关
  7. @Test
  8. void GetPage(){
  9. Page page = new Page(0, 4);
  10. personDao.selectPage(page, null);
  11. System.out.println("当前页码:"+page.getCurrent());
  12. System.out.println("多少页:"+page.getPages());
  13. System.out.println("每页条数:"+page.getSize());
  14. System.out.println("总数据数:"+page.getTotal());
  15. System.out.println("所有的内容:"+page.getRecords());
  16. }
  17. }

 分页操作时在MyBatisPlus的常规操作基础上增强得到的,内部是动态的拼写sql语句,因此需要增强对应的功能,使用MyBatisPlus拦截器实现

3、 MP条件查询功能

  1. package com;
  2. import javax.annotation.Resource;
  3. @SpringBootTest
  4. class Springboot06MybatisplusApplicationTests {
  5. @Autowired(required = true)
  6. private PersonDao personDao;
  7. @Test
  8. //法1
  9. public void testBy(){
  10. String name="z";
  11. LambdaQueryWrapper<Person> lqw=new LambdaQueryWrapper<>();
  12. lqw.like(name!=null,Person::getUsername,name);
  13. personDao.selectList(lqw);
  14. }
  15. @Test
  16. //法2
  17. public void testBy1(){
  18. String name="z";
  19. QueryWrapper<Person> qw=new QueryWrapper<>();
  20. qw.like(name!=null, "username", "zhan");
  21. personDao.selectList(qw);
  22. }
  23. }

法1比法2好,因为中间的字段不会写错,而法2没有提示,则容易出错。 

运行结果: 

       Service又称为业务层,dao层又称为数据层                                                                      

4、Mp的业务层快速开发

快速开发方案

  • 使用MyBatisPlus提供有业务层通用接口(IService<T>)与业务层通用实现类(ServiceImpl<M,T>)(M为dao层对应的接口,T为实体类)
  • 在通用类基础上功能重载或功能追加
  • 注意重载时不要覆盖原始操作,避免原始提供的功能丢失

 接口下

  1. package com.service;
  2. import com.baomidou.mybatisplus.extension.service.IService;
  3. import com.pojo.Person;
  4. public interface PersonService extends IService<Person> {
  5. }

实现类 

  1. package com.service.impl;
  2. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  3. import com.dao.PersonDao;
  4. import com.pojo.Person;
  5. import com.service.PersonService;
  6. import org.springframework.stereotype.Component;
  7. import org.springframework.stereotype.Service;
  8. @Service
  9. public class PersonServiceImpl extends ServiceImpl<PersonDao, Person> implements PersonService {
  10. }

测试下

  1. @SpringBootTest
  2. public class PersonServiceTest {
  3. @Autowired
  4. private PersonService personService;
  5. @Test
  6. void deleteByID(){
  7. personService.removeById(1);
  8. }
  9. @Test
  10. void testAll(){
  11. personService.list();
  12. }
  13. }

运行结果 

 接口下定义

 实现类下定义:

 小结:

1、使用通用接口(IService<T>)快速开发Service

2、使用通用实现类(ServiceImpl<M,T>)快速开发ServiceImpl

3、可以在通用接口基础上做功能重载或功能追加

4、注意重载时不要覆盖原方法,避免原始提供功能丢失

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/518203
推荐阅读
相关标签
  

闽ICP备14008679号