1.springboot自动装配原理
(1)springboot包扫描原理
包建议放在主类所在包或者子包。默认包扫描的是主类所在的包以及子包。
主函数在运行时会加载一个使用@SpringBootApplication标记的类,而该类注解是一个复合注解,包含@EnableAutoConfiguration,这个注解开启了自动配置功能。该注解也是一个复合注解,包含@AutoconfigurationPackage。该注解中包含@Import({Registrar.class}),这个注解引入了Registrar类。该类中存在registerBeanDefinitions,可以获取扫描的包名。
如果需要人为修改扫描包的名称则需要在类上@ComponentScan(basepackage = {"包名"})
(2)springboot自动装配原理
思考:没有配置DispatcherServlet,为什么DispatcherServlet能用。
主函数在运行会执行一个使用@SpringbootApplication注解的类,该注解是一个复合注解,包含@EnableAutoConfiguration,该注解开启自动配置功能,该注解也是一个复合注解,包含@Import()该注解需要导入AutoConfigurationImportSelector类。该类会加载很多自动装配类,而这些自动装配类完成相应的自动装配原理
2.mybatis-plus
MyBatis-Plus(opens new window)(简称MP) 是一个Mybatis(opens new window)的增强工具,在MyBatis的基础上只能增强不做改变,为简化开发,提高效率而生。
我们的愿景是成Mybatis最好的搭档,就像魂斗罗中的 1P、2P,基友搭配,效率翻倍。
不能替代mybatis ,以后对于单表操作的所有功能,都可以使用mp完成。但是链表操作的功能还得要校验mybatis。
3.mybatis-plus常用注解
@TableName(value = "tbl_student")//实体类和表名的映射
public class Student implements Serializable {
@TableId(type = IdType.AUTO) //设置数据库id为自增
private Integer sid; //如果你的主键名不叫id,必须人为标记
@TableField(value = "sname") //解决属性名和列名不一致
private String sname;
private Integer age;
private Integer cid;
@TableField(exist = false) //解决数据表中不存在该字段
private Aclass aclass;
}
4.mybatis-plus 实现链表分页查询
(1)分页类
@Configuration
@MapperScan("com.wyl.dao")
public class MybatisPlusConfig {
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
(2)接口
使用链表操作 需要手写方法及SQL语句
@Mapper
public interface StudentMapper extends BaseMapper<Student> {
IPage<Student> findPage(IPage<Student> iPage, @Param("ew") Wrapper<Student> wrapper);
}
(3)mapper.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wyl.dao.StudentMapper">
<resultMap id="resultStudent" type="com.wyl.pojo.Student" autoMapping="true">
<id property="sid" column="sid"/>
<association property="aclass" javaType="com.wyl.pojo.Aclass" autoMapping="true">
<id property="cid" column="cid"/>
</association>
</resultMap>
<sql id="stuAClassFiled">
sid,sname,age,t.cid,c.cid,cname
</sql>
<select id="findPage" resultMap="resultStudent">
select
<include refid="stuAClassFiled"/>
from
tbl_student t join aclass c on
t.cid = c.cid
<if test="ew != null">
<where>${ew.sqlSegment}</where>
</if>
</select>
</mapper>
(4)测试
/**
* 链表分页查询
*/
@Test
void testPage2(){
Page<Student> studentPage = new Page<>(1,3);//current:当前第几页 size:每页显示条数
QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
queryWrapper.likeRight("sname","测试");//添加查询条件
studentMapper.findPage(studentPage,queryWrapper);// 把查询分页的结果封装到page对象中
System.out.println("当前页的记录" + studentPage.getRecords());//获取当前页的记录
System.out.println("获得总页数" + studentPage.getPages()); //获取总页数
System.out.println("获得总条数" + studentPage.getTotal()); //获取总条数
}