1. springboot自动装配原理---
2. springboot整合mbatis-plus
1. springboot自动装配原理---
1.1 springboot 包扫描原理
主包中得@SpringBootApplication 是一个复合注释 ,包含了@EnableAutoConfiguration 开启自动配置 这也是一个复合注释,包含了@AutoConfigurationPackage 自动配置包 这个注释中引用了Registrar.class 类中得 registerBeanDefinitions 方法 获取包名
1.2 springboot 自动装配原理
主包中得@SpringBootApplication 是一个复合注释 ,包含了@EnableAutoConfiguration 开启自动配置 这也是一个复合注释,引用AutoConfigurationImportSelector这个类,这个类会加载很多自动装配类,而这些自动装配类完成相应的自动装配原理。
2. springboot整合mbatis-plus
2.1 mbatis-plus 简述
MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
mp不能替代mbatis mp只能进行单表查询,多表查询还要使用mbatis得功能
2.2 引入mp得依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
有mp的依赖 就不用添加mbatis依赖
2.3 配置所需的数据源
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql:///db1
spring.datasource.druid.username=root
spring.datasource.druid.password=123456
# 打印sql 日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
2.3 创建实体类
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Student implements Serializable{
private Integer id;
private String name;
private Integer age;
private String sax;
private Integer schoolId;
private School school;
public Student(String name, Integer age, String sax, Integer schoolid) {
this.name = name;
this.age = age;
this.sax = sax;
this.schoolId = schoolid;
}
}
2.4 创建mapper层
public interface StudentMapper extends BaseMapper<Student> {
}
2.5 在主函数上 为接口生成代理实现类
@SpringBootApplication
@MapperScan(basePackages = "pjx.springboot2.mapper")
public class Springboot2Application {
public static void main(String[] args) {
SpringApplication.run(Springboot2Application.class, args);
}
}
2.6 测试
@SpringBootTest
class Springboot2ApplicationTests {
@Autowired
StudentMapper studentMapper;
@Test
void findById(){
System.out.println(studentMapper.selectById(1));
}
}
2.7 使用mp 完成 CRUD 条件查询以及分页查询
创建mp分页类
@Configuration
@MapperScan(defaultScope = "pjx.springboot2.mapper")
public class MybatisPlusConfig {
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
测试
@SpringBootTest
class Springboot2ApplicationTests {
@Autowired
StudentMapper studentMapper;
/**
* 条件查询
*/
@Test
void find() {
QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
queryWrapper.ge("age", 10);
System.out.println(studentMapper.selectList(queryWrapper));
}
/**
* 增加
*/
@Test
void intest(){
Student student = new Student("pm6", 11, "男", 2);
System.out.println(studentMapper.insert(student));
}
/**
* 删除
*/
@Test
void deleteById(){
studentMapper.deleteById(7);
List<Integer> idlist = new ArrayList<>();
idlist.add(6);
idlist.add(5);
studentMapper.deleteBatchIds(idlist);
}
/**
* 修改
*/
@Test
void update(){
Student student = new Student("pm4", 13, "女", 2);
student.setId(1);
studentMapper.updateById(student);
}
/**
* 查询所有
*/
@Test
void contextLoads() {
System.out.println(studentMapper.selectList(null));
}
/**
* 模糊查询 + 分页查询
*/
@Test
void find2(){
Page<Student> page = new Page<>(1,4);
QueryWrapper<Student> wrapper = new QueryWrapper<>();
wrapper.like("name","墨");
Page<Student> studentPage = studentMapper.selectPage(page, wrapper);
System.out.println(studentPage);
System.out.println("当前页的记录"+page.getRecords());//获取当前页的记录
System.out.println("获取总页数"+page.getPages());//获取当前页的记录
System.out.println("获取总条数"+page.getTotal());//获取当前页的记录
}
}
3. springboot整合swagger2
3.1 加入swagger2 的依赖
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.9.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.7.8</version>
</dependency>
3.2 创建swagger2的配置类
@Configuration
@EnableSwagger2
public class swaggerConfig {
@Bean
public Docket docket(){
Docket docket = new Docket(DocumentationType.SWAGGER_2).groupName("pm");
return docket;
}
}