- 什么是Springboot以及Springboot的特点。
- 快速搭建springboot项目
- springboot常用的配置文件类型.
- 读取springboot配置文件的内容
- 多环境配置
- springboot整合数据源。
- springboot整合mybatis.
- springboot整合定时器。
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程 .
理解:spring框架搭建的步骤:[1]依赖 [2]配置文件。 使用springboot可以简化上面的两个步骤。
① 创建独立的 Spring 应用程序
② 嵌入的 Tomcat,无需部署 WAR 文件
③ 简化 Maven 配置
④ 自动配置 Spring
⑤ 提供生产就绪型功能,如指标,健康检查和外部配置
⑥ 开箱即用,没有代码生成,也无需 XML 配置。
创建一个controller包
> @RestController
public class HelloWorldController {
@GetMapping("index")
public Map<String,Object> hello(){
Map<String,Object> map = new HashMap<>();
map.put("name","马群超");
map.put("age",18);
return map;
}
@Autowired
private Student student;
@GetMapping("info")
public Student info(){
return student;
}
@Value("${student.name}")
private String n;
@GetMapping("a")
public String a(){
return n;
}
//使用@Value 读取属性 他只能读取基本类型和String类型。加在属性上
/*@Value("${student.map}")
private Map map;
@GetMapping("a")
public Map a(){
return map;
}*/
}
properties和yml格式。他们的区别就是格式上不同。
properties格式如下:
#修改端口号 server.port=8081 #修改上下文路径 server.servlet.context-path=/aaa #数据源 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.druid.url=jdbc:mysql://localhost:3306/homework1?serverTimezone=Asia/Shanghai spring.datasource.druid.username=root spring.datasource.druid.password=123456 spring.datasource.druid.initial-size=5 #信息 student.name=lmy student.age=18 student.address=yx student.hobby[0]=sing student.hobby[1]=dance student.hobby[2]=rap student.map.a=1 student.map.b=2 student.map.c=3
yml结构
context-path: /bbb # 信息 student: name: mqc age: 21 address: zmd hobby: - eat - drink - play map: a: 1 b: 2 c: 3 ```
不管使用哪个配置文件,他们的名字必须叫application.
如果上面两个配置文件同时存在,而且里面有相同的配置。则properties优先级高于yml优先级。
java为什么需要读取配置文件的内容,我们开发时需要把哪些内容放入配置文件。
OSS:上传文件。accessKeyId,accessKeySecret等,这些内容能写在java源代码中。硬编码文件,不利维护。 我们需要把信息写入配置文件。
读取方式有两种:
第一种方式: 在类上
@Data
@Component
@ConfigurationProperties(prefix = "student")
public class Student {
private String name;
private Integer age;
private String address;
private List<String> hobby;
private Map<String,Object> map;
}
student.name=lmy
student.age=18
student.address=yx
student.hobby[0]=sing
student.hobby[1]=dance
student.hobby[2]=rap
student.map.a=1
student.map.b=2
student.map.c=3
# 信息
student:
name: mqc
age: 21
address: zmd
hobby:
- eat
- drink
- play
map:
a: 1
b: 2
c: 3
第二种 使用@Value读取属性:—他只能读取基本类型和String类型。加在属性
@Value("${student.map}") private Map<String,Object> map; @GetMapping("a") public Map<String,Object> a(){ return map; }
druid数据源: ----连接数据库
<!--mysql的驱动依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.21</version> </dependency>
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://localhost:3306/homework1?serverTimezone=Asia/Shanghai
spring.datasource.druid.username=root
spring.datasource.druid.password=123456
spring.datasource.druid.initial-size=5
@SpringBootTest
class SpringBoot1ApplicationTests {
@Autowired
private DataSource dataSource;
@Test
void contextLoads() throws SQLException {
/*System.out.println(dataSource.getConnection());*/
System.out.println(dataSource);
}
}
<?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.7.10</version>--> <!--这个版本mysql用加版本号-->
<version>2.3.12.RELEASE</version> <!--这个版本mysql不用加版本号-->
</parent>
<groupId>com.lmy</groupId>
<artifactId>springBoot2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springBoot2</name>
<description>springBoot2</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId> <!--<version>2.3.12.RELEASE</version> 这个版本mysql不用加版本号-->
<!--<version>8.0.28</version>--> <!--<version>2.7.10</version> 这个版本mysql用加版本号-->
</dependency>
<!--mybatis和springboot整合的依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
#数据源
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.username=root
spring.datasource.druid.password=123456
spring.datasource.druid.url=jdbc:mysql://localhost:3306/homework1
#指定映射文件所在的路径
mybatis.mapper-locations=classpath:mapper/*.xml
#mybatis日志文件
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
org.apache.ibatis.binding.BindingException: Invalid bound statement
(not found): com.ykq.mapper.EmpMapper.findAll
@Data
public class Student implements Serializable {
private String studentno;
private Integer gradeid;
private String name;
private String sex;
private Integer age;
private String city;
private Integer phone;
private Grade grade;
//省略get set toString方法
}
@Data
public class Grade implements Serializable {
private Integer gradeid;
private String gradename;
}
<?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">
<!--namespace必须和接口名对应-->
<mapper namespace="com.lmy.springboot2.mapper.StudentMapper">
<resultMap id="StudentResultMap" type="com.lmy.springboot2.pojo.Student" autoMapping="true">
<id property="studentno" column="studentno" />
<result property="gradeid" column="gradeid" />
<result property="name" column="name" />
<result property="sex" column="sex" />
<result property="age" column="age" />
<result property="city" column="city" />
<result property="phone" column="phone" />
<association property="grade" javaType="com.lmy.springboot2.pojo.Grade" autoMapping="true">
<id property="gradeid" column="gradeid"></id>
<id property="gradename" column="gradename"></id>
</association>
</resultMap>
<!--查询所有学生信息-->
<select id="findAll" resultMap="StudentResultMap">
select * from student s left join grade g on s.gradeid=g.gradeid
</select>
<!--通过studentno查询学生信息-->
<select id="findById" resultType="com.lmy.springboot2.pojo.Student">
select * from student where studentno = #{studentno}
</select>
<!--增加学生信息-->
<insert id="save" parameterType="com.lmy.springboot2.pojo.Student">
insert into student (studentno,gradeid,name,sex,age,city,phone) values (#{studentno},#{gradeid},#{name},#{sex},#{age},#{city},#{phone})
</insert>
<!--通过studentno删除学生信息-->
<delete id="delete">
delete from student where studentno = #{studentno}
</delete>
<!--修改学生信息-->
<update id="update">
update student
<set>
<!-- <if test="studentno != null and studentno != ''">
studentno = #{studentno},
</if>-->
<if test="gradeid != null and gradeid != ''">
gradeid = #{gradeid},
</if>
<if test="name != null and name != ''">
name = #{name},
</if>
<if test="sex != null and sex != ''">
sex = #{sex},
</if>
<if test="age != null and age != ''">
age = #{age},
</if>
<if test="city != null and city != ''">
city = #{city},
</if>
<if test="phone != null and phone != ''">
phone = #{phone}
</if>
</set>
where studentno = #{studentno}
</update>
</mapper>
public interface StudentMapper {
//查询所有学生信息
public List<Student> findAll();
//通过studentno查询学生信息
public Student findById(Integer studentno);
//增加学生信息
public int save (Student student);
//通过studentno删除学生信息
public int delete (Integer studentno);
//修改学生信息
public int update(Student student);
}
@SpringBootApplication
@MapperScan(basePackages = "com.lmy.springboot2.mapper")
public class SpringBoot2Application {
public static void main(String[] args) {
SpringApplication.run(SpringBoot2Application.class, args);
}
}
@SpringBootTest
class SpringBoot2ApplicationTests {
@Autowired
StudentMapper studentMapper;
@Test
void findAll() {
System.out.println(studentMapper.findAll());
}
@Test
void findById() {
System.out.println(studentMapper.findById(1003));
}
//"1007",3,"沸羊羊","男",19,"羊村",123123
@Test
void save() {
Student student = new Student();
student.setStudentno("1008");
student.setGradeid(3);
student.setName("沸羊羊2");
student.setSex("男");
student.setAge(19);
student.setCity("羊村");
student.setPhone(123123);
System.out.println(studentMapper.save(student));
}
@Test
void delete() {
System.out.println(studentMapper.delete(1008));
}
@Test
void update() {
Student student = new Student();
student.setStudentno("1008");
student.setGradeid(3);
student.setName("沸羊羊3");
student.setSex("女");
student.setAge(19);
student.setCity("羊村");
student.setPhone(123123);
System.out.println(studentMapper.update(student));
}
}
public interface StudentService {
//查询所有学生信息
public Result findAll();
//通过studentno查询学生信息
public Result findById(Integer studentno);
//增加学生信息
public Result save(Student student);
//通过studentno删除学生信息
public Result delete(Integer studentno);
//修改学生信息
public Result update(Student student);
}
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
StudentMapper studentMapper;
//查询所有学生信息
@Override
public Result findAll() {
List<Student> students = studentMapper.findAll();
return new Result(200, "查询所有学生信息成功", students);
}
//通过studentno查询学生信息
@Override
public Result findById(Integer studentno) {
Student student = studentMapper.findById(studentno);
return new Result(200, "查询员工信息成功", student);
}
//增加学生信息
@Override
public Result save(Student student) {
int i = studentMapper.save(student);
System.out.println(i);
if (i > 0) {
return new Result(200, "添加成功");
} else {
return new Result(500, "添加失败");
}
}
//通过studentno删除学生信息
@Override
public Result delete(Integer studentno) {
int i = studentMapper.delete(studentno);
System.out.println(i);
if (i > 0) {
return new Result(200, "删除成功");
} else {
return new Result(500, "删除失败");
}
}
//修改学生信息
@Override
public Result update(Student student) {
int i = studentMapper.update(student);
System.out.println(i);
if (i > 0) {
return new Result(200, "修改成功");
} else {
return new Result(500, "修改失败");
}
}
}
@RestController //该类种所有方法的返回都是json格式
public class StudentController {
@Autowired
StudentService studentService;
// @PutMapping 查询 get提交 删除 delete 修改 PUT提交 添加 post提交
//关于:CRUD 增删改
//查询所有学生信息
@GetMapping("findAll") //restful风格 PathVariable请求地址种{}中的内容
public Result findAll(){
return studentService.findAll();
}
//通过studentno查询学生信息
@GetMapping ("findById/{studentno}") //restful风格 PathVariable请求地址种{}中的内容
public Result findById(@PathVariable Integer studentno){
return studentService.findById(studentno);
}
//增加学生信息
@PostMapping("save")
public Result save(@RequestBody Student student){
return studentService.save(student);
}
//通过studentno删除学生信息
@DeleteMapping ("delete/{studentno}")
public Result delete(@PathVariable Integer studentno){
return studentService.delete(studentno);
}
//修改学生信息
@PutMapping ("update")
public Result update(@RequestBody Student student){
return studentService.update(student);
}
}