赞
踩
目录
出现No qualifying bean of type 'com.dao.PersonDao' available错误
创建一个Dao包下的UserDao
- package com.Dao;
-
- public interface UserDao {
- void save();
- }
创建实现类
- package com.Dao.Impl;
-
- import com.Dao.UserDao;
- import org.springframework.stereotype.Repository;
-
-
- //要使能被spring管理,加上注解@Component(spring管理的通用注解)或@Repository(更合适)
- @Repository
- public class UserDaoImpl implements UserDao {
- @Override
- public void save() {
- System.out.println("userDao is running...");
- }
- }
在测试下
- package com.springbootjunit04;
-
- import com.Dao.UserDao;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
-
- @SpringBootTest
- class Springbootjunit04ApplicationTests {
-
- //1、注入想要让spring管理的值
- @Autowired
- private UserDao userDao;
-
- @Test
- void contextLoads() {
- //2、执行你要测试对象对应的方法
- userDao.save();
-
- }
-
- }

运行结果
出现这种错误是因为springboot默认扫描这个启动类所在的包及其子包,所以就无法识别了,只需要将启动类移动到编写的Dao包之上即可重新被识别。
核心:@SpringBootTest
名称:@SpringBootTest
类型:测试类定义上方
作用:设置Junit加载的SpringBoot启动类
- @SpringBootTest
- class Springbootjunit04ApplicationTests {}
整合Junit步骤:
1、导入测试对应的starter(springboot已经在pom.xml完成)
2、测试类使用SpringBootTest注解(SpringBoot完成了)
3、使用自动装配的形式添加要测试的对象
相关属性:设置SpringBoot启动类
我们一开始设置在com包下的Test类,移出去启动后就会报错,原因是一开始在com包,springboot就会去同名上的com包下去找,如果这个test类在引导类的包下及其子包下则不会报错。报错的原因是test类找不到引导类(启动类)
所有只需要使用classes标签
@SpringBootTest(classes = Springbootjunit04Application.class)
如果测试类在SpringBoot启动类的包或子包中,可以省略启动类的设置,也就是省略classes的设定
创建一个新的模块
配置文件中:配置数据库相关信息application.yml中
-
- #配置相关信息
- spring:
- datasource:
- driver-class-name: com.mysql.cj.jdbc.Driver
- url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
- username: root
- password: 123456
数据库中的表
创建一个pojo包下的Person类
- package com.pojo;
-
- import lombok.Data;
-
- @Data
- public class Person {
- private Integer id;
- private String username;
- private String password;
- private String gender;
- private String addr;
-
- }
创建一个dao包下的PersonDao接口
- package com.dao;
-
- import com.pojo.Person;
- import org.apache.ibatis.annotations.Mapper;
- import org.apache.ibatis.annotations.Select;
- import org.springframework.stereotype.Component;
-
- import java.util.List;
- @Component//交给spring容器管理
- @Mapper//使用注解配置映射
- public interface PersonDao {
- @Select("select *from tb_user")
- List<Person> personList();
- }
在测试类下
- package com;
-
- import com.dao.PersonDao;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
-
- @SpringBootTest
- class Springboot05MybatisApplicationTests {
-
- //测试,先注入dao对象
- @Autowired
- private PersonDao personDao;
-
- @Test
- void contextLoads() {
- System.out.println(personDao.personList());
- }
-
- }

运行结果:
小结:
1、勾选Mybatis技术,也就是导入mybatis对应的starter
2、数据库连接相关信息转换成配置
3、数据库sql映射需要添加@Mapper被容器识别到
@Mapper注释用来表示该接口类的实现类对象交给mybatis底层创建,然后交由Spring框架管理
mysql8以上的版本的加上时区?serverTimezone=UTC不然会报错
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
小结:
1\mysql8以上的版本驱动有前置要求设置时区
2、驱动类过时,提醒更换为com.mysql.cj.jdbc.Driver
使用aliyun创建springboot,或者导入Mybatisplus坐标
dao层的变为,不用写任何方法,点ctrl+鼠标点击进去BaseMapper中可以看到提供了很多方法。
- @Data
- @TableName("tb_user")//告诉是哪张表
- public class Person {
- private Integer id;
- private String username;
- private String password;
- private String gender;
- private String addr;
-
- }
这需要在pojo对象中加上,告诉mp是哪张表。@TableName("tb_user")
- package com.dao;
-
- import com.baomidou.mybatisplus.core.mapper.BaseMapper;
- import com.pojo.Person;
- import org.apache.ibatis.annotations.Mapper;
-
- @Mapper//使用注解配置映射
- public interface PersonDao extends BaseMapper<Person> {
-
- }
测试类中
- package com;
-
-
- import com.dao.PersonDao;
- import com.pojo.Person;
-
- import org.junit.jupiter.api.Test;
-
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.stereotype.Repository;
-
- import javax.annotation.Resource;
-
- @SpringBootTest
- @MapperScan("com.dao")//扫描dao的包,扫描到mapper
- class Springboot06MybatisplusApplicationTests {
-
-
- @Resource
- private PersonDao personDao;
-
- @Test
- void contextLoads() {
-
- System.out.println(personDao.selectById(1));
- }
-
- }

运行结果
Springboot整合mybatis-plus
①手动添加添加springboot整合mp的坐标,可以通过mvnrepository获取
注:由于springboot没有收录mp的坐标版本,需要指定对应的version
②定义数据层接口与映射配置,继承BaseMapper
测试类中注入dao接口,使用即可。
注:需要使用的第三方技术无法创建时勾选时,就手工添加
这个本质就是springboot找不到dao,只需要在启动类中加入@MapperScan("com.dao")就可以正常访问了
在配置文件中写入
-
- #打印日志
- mybatis-plus:
- configuration:
- #标准输出
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
运行一个查看所有的方法,控制台输出如下
定义一个类做拦截器
- package com.config;
-
- import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
- import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- //分页相关
- @Configuration//是一个配置
- public class MPConfig {
- @Bean//给spring管理
- public MybatisPlusInterceptor mybatisPlusInterceptor(){
- //定义Mybatis拦截器
- MybatisPlusInterceptor interceptor=new MybatisPlusInterceptor();
- //添加具体的拦截器
- interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
-
- return interceptor;
-
- }
-
- }

测试类下
- package com;
-
-
- @SpringBootTest
- class Springboot06MybatisplusApplicationTests {
-
-
- @Autowired(required = true)
- private PersonDao personDao;
-
-
- // 分页相关
- @Test
- void GetPage(){
- Page page = new Page(0, 4);
- personDao.selectPage(page, null);
- System.out.println("当前页码:"+page.getCurrent());
- System.out.println("多少页:"+page.getPages());
- System.out.println("每页条数:"+page.getSize());
- System.out.println("总数据数:"+page.getTotal());
- System.out.println("所有的内容:"+page.getRecords());
- }
-
-
- }

分页操作时在MyBatisPlus的常规操作基础上增强得到的,内部是动态的拼写sql语句,因此需要增强对应的功能,使用MyBatisPlus拦截器实现
- package com;
-
-
-
- import javax.annotation.Resource;
-
- @SpringBootTest
- class Springboot06MybatisplusApplicationTests {
-
-
- @Autowired(required = true)
- private PersonDao personDao;
- @Test
- //法1
- public void testBy(){
- String name="z";
- LambdaQueryWrapper<Person> lqw=new LambdaQueryWrapper<>();
- lqw.like(name!=null,Person::getUsername,name);
- personDao.selectList(lqw);
- }
-
- @Test
- //法2
- public void testBy1(){
- String name="z";
- QueryWrapper<Person> qw=new QueryWrapper<>();
- qw.like(name!=null, "username", "zhan");
- personDao.selectList(qw);
- }
-
-
-
-
- }

法1比法2好,因为中间的字段不会写错,而法2没有提示,则容易出错。
运行结果:
Service又称为业务层,dao层又称为数据层
快速开发方案
接口下
- package com.service;
-
- import com.baomidou.mybatisplus.extension.service.IService;
- import com.pojo.Person;
-
- public interface PersonService extends IService<Person> {
- }
实现类
- package com.service.impl;
-
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.dao.PersonDao;
- import com.pojo.Person;
- import com.service.PersonService;
- import org.springframework.stereotype.Component;
- import org.springframework.stereotype.Service;
-
- @Service
- public class PersonServiceImpl extends ServiceImpl<PersonDao, Person> implements PersonService {
- }
-
测试下
-
-
- @SpringBootTest
- public class PersonServiceTest {
- @Autowired
- private PersonService personService;
-
-
- @Test
- void deleteByID(){
- personService.removeById(1);
- }
-
- @Test
- void testAll(){
- personService.list();
- }
-
-
-
- }

运行结果
接口下定义
实现类下定义:
小结:
1、使用通用接口(IService<T>)快速开发Service
2、使用通用实现类(ServiceImpl<M,T>)快速开发ServiceImpl
3、可以在通用接口基础上做功能重载或功能追加
4、注意重载时不要覆盖原方法,避免原始提供功能丢失
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。