SpringBoot 与 Mybatis 的集成
1. Springboot包扫描的原理
springboot默认扫描的包主类所在的包以及子包。在主启动类上使用@SpringBootApplication该注解是一个复合组件。其中包含@EnableAutoConfiguration,这个注解也是一个复合注解,其中包含@AutoConfigurationPackage, 该注解中包含@Import({Registrar.class}),该注解导入了Registrar类。注意: 我们是否可以人为修改扫描的包路径呢? 可以。
2. Springboot自动装配原理 2.3.12
思考: springmvc时,需要在web.xml文件中配置前端控制器DispatcherServlet. 我们现在springboot有没有配置前端控制呢?[没有] 因为Springboot完成了自动装配的原理。
配置一个编码过滤器,我们现在有没有配置编码过滤器呢?
原理: 在主启动类上使用@SpringBootApplication该注解是一个复合组件。其中包含@EnableAutoConfiguration,这个注解也是一个复合注解,@Import({AutoConfigurationImportSelector.class})这个注解导入了AutoConfigurationImportSelector,会把你导入一些自动装配类,而这些自动装配类完成了想要得到自动装配功能。
我们以DispatcherServletAutoConfiguration自动配置类为例。
3. Springboot整合第三方框架。
3.1 springboot整合mybatis
引入整合的依赖jar
<!--mybatis和springboot整合: SqlSessionFactoryBean DataSource Mapp-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<!--mysql的依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
配置数据源信息
#数据源
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=Asia/Shanghai
创建实体类
@Data
public class Student {
private Integer sId;
private String stuName;
private Integer age;
private Integer classId;
}
dao接口
public interface StudentDao {
public List<Student> selectAll();
}
映射文件
<?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.aaa.dao.StudentDao">
<select id="selectAll" resultType="com.aaa.entity.Student">
select * from t_student
</select>
</mapper>
修改application配置文件 指定映射文件所在得到路径
#指定mybatis映射文件所在的路径
mybatis.mapper-locations=classpath:/mapper/*.xml
为指定dao接口生成代理实现 @MapperScan(basePackage="")
测试
@Autowired
private StudentDao studentDao;
@Test
void contextLoads() throws SQLException {
List<Student> students = studentDao.selectAll();
System.out.println(students);
}
3.2 springboot整合pagehelper分页插件
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.2</version>
</dependency>
测试
@Test
void contextLoads() throws SQLException {
PageHelper.startPage(1,3);
List<Student> students = studentDao.selectAll();
PageInfo<Student> page=new PageInfo<>(students);
System.out.println("当前页的数据:"+page.getList());
System.out.println("总页数:"+page.getPages());
System.out.println("总条数:"+page.getTotal());
}
3.3 定时器
规定的时间指向想要的代码。现实中的闹铃。
使用场景: 定时查看订单状态30分钟。---取消订单。
OSS---存在大量冗余的文件。[定时删除冗余文件]
定时更新代码。
定时器:Quartz (石英)
<!--定时器的依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency>
定时任务类
在线Cron表达式生成器
@Component public class My { //业务功能---默认spring不能识别这个注解 @Scheduled(cron = "0/1 * * * * ?") public void show(){ System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); //根据当前时间查询过期订单 //修改过期订单的状态 } }
开启定时器注解驱动
4. 综合案例
CRUD功能
sql语句
<?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.cby.mapper.UserMapper">
<insert id="addUser">
insert into user values (null,#{uName},#{age},#{uEmail},#{cId})
</insert>
<update id="updateUser">
update user set u_name=#{uName},age=#{age},u_email=#{uEmail},c_id=#{cId} where uid=#{uid}
</update>
<delete id="deleteUser">
delete from user where uid=#{uid}
</delete>
<select id="getAll" resultType="com.cby.entity.User">
select uid,u_name uName,age,u_email uEmail,c_id cId from user
</select>
<select id="getById" resultType="com.cby.entity.User">
select uid,u_name uName,age,u_email uEmail,c_id cId from user where uid=#{uid}
</select>
</mapper>
1.增加
2.删除
3.修改
4.根据主键查询
5.查询全部(分页)
完成CRUD[包含分页]---并使用postman测试---[使用restful风格