1.springboot自动装配原理
在主启动类上的@SpringBootApplication注解上,该注解是一个复合组件,这个注解包括了@SpringBootConfiguration、@EnableAutoConfiguration和@ComponentScan等注解。其中@EnableAutoConfiguration注解是启动自动配置的关键,也是一个复合注解,里面包含了@Import---导入了一个AutoConfigurationImportSelector(自动配置选择器),在该类中有一个方法getAutoConfigurationEntry--作用:得到自动配置类的实体。而这些自动配置类会完成相应的自动装配
2.springboot整合数据源--链接数据库
2.1设置默认数据源
引入相关依赖
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--mysql springboot2.7.8以上版本只用用mysql-connector-j-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
application.properties文件中配置数据源
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/qy168?serverTimezone=Asia/Shanghai
测试:
@SpringBootTest
class SpringbootDemo01ApplicationTests {
@Autowired
private DataSource dataSource;
@Test
void contextLoads() throws SQLException {
System.out.println(dataSource.getConnection());
}
}
运行结果:
2.2设置Druid的数据源
依赖
<!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
修改application.properties
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.username=root
spring.datasource.druid.password=root
spring.datasource.druid.url=jdbc:mysql://localhost:3306/qy168?serverTimezone=Asia/Shanghai
#连接池默认连接数
spring.datasource.druid.initial-size=5
#连接池最大连接数
spring.datasource.druid.max-active=10
测试:
3.springboot整合mybatis
依赖:
<!--mybatis整合springboot-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
配置文件:
#端口号
server.port=8082
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.username=root
spring.datasource.druid.password=root
spring.datasource.druid.url=jdbc:mysql://localhost:3306/qy168?serverTimezone=Asia/Shanghai
#连接池默认连接数
spring.datasource.druid.initial-size=5
#连接池最大连接数
spring.datasource.druid.max-active=10
#映射xml文件
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
创建实体类:
@Data
public class Student {
//学生d
private Integer id;
//学生姓名
private String name;
//学生性别
private String sex;
//学生班级id
private String classId;
}
创建mapper接口以及xml
注意:给每个dao接口上都加上@Mapper,它就会将mapper自动注入进spring容器。
或者在主启动类上加入@MapperScan,指定要扫描(dao接口)包的路径。(批量)
@Mapper
public interface StudentDao {
//添加
int insert(Student student);
}
<?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.gjc.dao.StudentDao">
<!--添加之后返回主键到id上-->
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
insert into tbl_student values(null,#{name},#{sex},#{classId})
</insert>
</mapper>
测试
4.springboot整合pageHelper分页插件
依赖:
<!--mybatis整合分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.5</version>
</dependency>
controller
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private UserService userService;
@PostMapping()
public Result insert(@RequestBody Student student){
return userService.insert(student);
}
@GetMapping("/getAll1")
public Result getAll1(Integer currentPage,Integer pageSize){
return userService.selectAll1(currentPage,pageSize);
}
}
service
public interface UserService {
//添加
public Result insert(Student student);
//分页查询
Result selectAll1(Integer currentPage, Integer pageSize);
}
@Service
public class UserServiceImpl implements UserService{
@Autowired
private StudentDao studentDao;
@Override
public Result insert(Student student) {
int i = studentDao.insert(student);
return i>0?new Result<Student>(200,"添加成功",student):new Result(500,"添加失败",null);
}
@Override
public Result selectAll1(Integer currentPage, Integer pageSize) {
PageHelper.startPage(currentPage,pageSize);
List<Student> students= studentDao.selectAll1();
PageInfo<Student> studentPageInfo = new PageInfo<>(students);
return new Result(200,"查询成功",studentPageInfo);
}
}
mapper
@Mapper
public interface StudentDao {
//添加
int insert(Student student);
//查询全部
List<Student> selectAll1();
}
<?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.gjc.dao.StudentDao">
<resultMap id="student" type="com.gjc.entity.Student" autoMapping="true">
<id property="id" column="id"/>
<result property="name" column="s_name"/>
<result property="classId" column="cid"/>
<association property="clazz" javaType="com.gjc.entity.Clazz" autoMapping="true">
<id property="id" column="cid"/>
</association>
</resultMap>
<!--添加之后返回主键到id上-->
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
insert into tbl_student values(null,#{name},#{sex},#{classId})
</insert>
<select id="selectAll1" resultMap="student">
select s.*,c.id cid,c.cname from tbl_student s join stl_classes c on s.cid=c.id
</select>
</mapper>
5.springboot整合定时器
依赖:
<!--引入定时器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
设置定时任务
@Component
public class MyQuartz {
@Scheduled(cron = "0/3 * * * * ? ")//设置时间每三秒执行一次
public void show(){
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = dateTimeFormatter.format(LocalDateTime.now());
System.out.println("执行了"+format);
}
}
cron生成器:在线Cron表达式生成器
开启定时器注解驱动:
主启动类添加:@EnableScheduling
测试:
6 .增删改查案例
pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--继承父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
<groupId>com.gjc</groupId>
<artifactId>springboot_demo01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--springboot-web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springboot单元测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--lombok 实体类-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--mysql springboot2.7.8版本只用用mysql-connector-j-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
<!--mybatis整合springboot-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!--mybatis整合分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.5</version>
</dependency>
</dependencies>
</project>
配置文件:
#设置端口号
server.port=8082
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.username=root
spring.datasource.druid.password=root
spring.datasource.druid.url=jdbc:mysql://localhost:3306/qy168?serverTimezone=Asia/Shanghai
#连接池默认连接数
spring.datasource.druid.initial-size=5
#连接池最大连接数
spring.datasource.druid.max-active=10
#映射xml文件
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
实体类:
@Data
public class Student {
//学生d
private Integer id;
//学生姓名
private String name;
//学生性别
private String sex;
//学生班级id
private String classId;
//学生班级
private Clazz clazz;
}
@Data
public class Clazz {
//班级id
private Integer id;
//班级名
private String name;
}
封装返回类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T>{
private Integer code;
private String msg;
private T data;
}
mapper
@Mapper
public interface StudentDao {
//添加
int insert(Student student);
//修改
int update(Student student);
//删除
int deleteById(Integer id);
//根据id查询
Student selectById(Integer id);
//根据name条件查询全部
List<Student> selectAll(String name);
}
<?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.gjc.dao.StudentDao">
<resultMap id="student" type="com.gjc.entity.Student" autoMapping="true">
<id property="id" column="id"/>
<result property="name" column="s_name"/>
<result property="classId" column="cid"/>
<!--一对一-->
<association property="clazz" javaType="com.gjc.entity.Clazz" autoMapping="true">
<id property="id" column="cid"/>
</association>
</resultMap>
<!--添加之后返回主键到id上-->
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
insert into tbl_student values(null,#{name},#{sex},#{classId})
</insert>
<update id="update">
update tbl_student set s_name=#{name},sex=#{sex},cid=#{classId} where id=#{id}
</update>
<delete id="deleteById">
delete from tbl_student where id=#{id}
</delete>
<select id="selectById" resultMap="student">
select * from tbl_student where id=#{id}
</select>
<select id="selectAll" resultMap="student">
select s.*,c.id cid,c.cname from tbl_student s join stl_classes c on s.cid=c.id
<where>
<if test="name!=null and name!=''">
s.s_name like concat("%",#{name},"%")
</if>
</where>
</select>
</mapper>
serice
public interface UserService {
//添加
public Result insert(Student student);
//修改
public Result update(Student student);
//根据id删除
public Result deleteById(Integer id);
//根据id查询
public Result selectById(Integer id);
//查询全部
public Result selectAll(Integer currentPage,Integer pageSize,String name);
}
@Service
public class UserServiceImpl implements UserService{
@Autowired
private StudentDao studentDao;
@Override
public Result insert(Student student) {
int i = studentDao.insert(student);
return i>0?new Result<Student>(200,"添加成功",student):new Result(500,"添加失败",null);
}
@Override
public Result update(Student student) {
int i = studentDao.update(student);
return i>0?new Result<Student>(200,"修改成功",null):new Result(500,"修改失败",null);
}
@Override
public Result deleteById(Integer id) {
int i = studentDao.deleteById(id);
return i>0?new Result<Student>(200,"删除成功",null):new Result(500,"删除失败",null);
}
@Override
public Result selectById(Integer id) {
Student student = studentDao.selectById(id);
return new Result<Student>(200,"查询成功",student);
}
@Override
public Result selectAll(Integer currentPage, Integer pageSize, String name) {
PageHelper.startPage(currentPage,pageSize);
List<Student> students = studentDao.selectAll(name);
PageInfo<Student> pageInfo = new PageInfo<>(students);
return new Result(200,"查询成功",pageInfo);
}
}
controller
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private UserService userService;
@PostMapping()
public Result insert(@RequestBody Student student){
return userService.insert(student);
}
@PutMapping()
public Result update(@RequestBody Student student){
return userService.update(student);
}
@DeleteMapping()
public Result deleteById(Integer id){
return userService.deleteById(id);
}
@GetMapping()
public Result selectById(Integer id){
return userService.selectById(id);
}
@GetMapping("/getAll")
public Result getAll(Integer currentPage,Integer pageSize,String name){
System.out.println(currentPage+"--"+pageSize+"--"+name);
return userService.selectAll(currentPage,pageSize,name);
}
}