Spring Boot是spring家族中的一个全新的框架,它用来简化Spring应用程序的创建和开发过程,也可以说Spring Boot可以简化我们SSM框架进行开发的过程
1、我这里使用的是阿里云的镜像
2、勾选需要的起步依赖
大体目录:
resource下的目录结构
pom中的部分依赖
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-maven-plugin 2.3.7.RELEASE
Springboot的第一个java文件
//SpringBoot项目启动入口类
@SpringBootApplication//springboot核心注解,主要用于开启spring自动配置
public class Springboot001Application {public static void main(String[] args) {SpringApplication.run(Springboot001Application.class, args);}}
编写Controller类
必须放在Springboot启动类所在的同级目录或者下级目录
@Controller
public class IndexController {@RequestMapping(value = "/springboot/say")@ResponseBodypublic String say(){return "nihao";}}
启动并访问
输出结果:
设置端口号和上下文根
# 应用名称
spring.application.name=springboot_003
# 应用服务 WEB 访问端口/设置内嵌Tomcat端口号
server.port=8080#设置上下文根
server.servlet.context-path=/springboot
测试:
添加配置,注意格式
正确配置:
server:port: 8081servlet:context-path: /
测试:
添加配置
server:port: 8080servlet:context-path: /666
测试
如果两个文件同时存在,优先取的是properties文件内的
如果项目出现问题,可以把maven镜像修改成我的(本地)
maven寻找依赖的时候,首先会去本地找,然后再到镜像仓库
nexus-aliyun * Nexus aliyun http://maven.aliyun.com/nexus/content/groups/public
一般情况我们的环境有:1开发环境 、2 测试环境、3 准生产环境 4 生产环境
有时候我们会在不同的环境对项目进行操作。但每个环境的配置不一样,比如IP端口、数据库等等。如果我们通过单一的配置文件进行反复的修改,会显得很麻烦。因此我们需要使用使用多环境开发模式对配置文件进行切换。
步骤:
1️⃣ 首先创建所需要的开发环境配置文件
我这里有dev:开发、product:生产、ready:准生产环境。你也可以根据自己的需求进行添加
下面是各个配置文件的详细内容
Dev:
#开发环境
# 应用名称
spring.application.name=springboot-properties-yal-yaml
# 应用服务 WEB 访问端口
server.port=8080
server.servlet.context-path=/dev
product:
#生产环境
server.port=9090
server.servlet.context-path=/product
ready:
#准生产环境
server.port=8082
server.servlet.context-path=/ready
test:
#测试环境
server.port=8081
server.servlet.context-path=/test
2️⃣我们还需要一个核心配置文件——application.properties
我们需要在这个文件里面激活需要的具体的环境配置文件
里面具体的操作:
使用spring.profiles.active激活我们想要使用的配置文件,“=”号后面只需要填写application-后面且,properties前面的内容。注意格式标准
#springboot主核心配置文件
#激活使用的配置文件
spring.profiles.active=dev
这是我们使用的是dev开发环境,开发环境的端口号是8080,此时我们启动程序查看日志
此时我们可以看到已经成功的切换到了开发环境,端口是8080
我们在换一个环境试试:换一个测试环境test,同样我们需要在核心配置文件里去激活
#springboot主核心配置文件
#激活使用的配置文件
spring.profiles.active=test
启动程序并查看日志
我们可以看到成功切换了
基本步骤和properties的配置相差不大
1️⃣
首先创建所需要的配置文件,这里我就只举一个例子了
dev
配置内容:
#开发环境
server:port: 8080servlet:context-path: /dev
2️⃣创建一个核心配置文件,这里的核心配置文件不是以.properties结尾的,而是以.yaml结尾的
激活配置文件
spring:profiles:active: dev
启动程序查看结果
OK
比如我们在配置文件中定义了数据
然后我们需要在外部获取
1️⃣我们首先在外部定义好数据
school.name=Sichuan Dachuan Middle School
websit=http://www.scdczx.en
2️⃣然后我们通过@Value注解来获取数据
@Controller
public class IndexController {@Value("${school.name}")private String schoolName;@Value("${websit}")private String websit;@RequestMapping(value = "/say")@ResponseBodypublic String say(){return "msg:"+schoolName+":"+websit;}
}
运行结果:
1️⃣首先我们也设置好数据源
school.name=四川省达川中学
school.master=ObjectMan
school.websit=http://www.scdczx.en
2️⃣使用@ConfigurationProperties(prefix = “”) 进行数据注入prefix的值为前数据源名称的前缀
@Component//将此类交给Spring容器管理
@ConfigurationProperties(prefix = "school")
public class School {private String name;private String master;private String websit;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getMaster() {return master;}public void setMaster(String master) {this.master = master;}public String getWebsit() {return websit;}public void setWebsit(String websit) {this.websit = websit;}
}
获取对象
@Controller
public class IndexController {@Autowiredprivate School school;@RequestMapping(value = "/say")@ResponseBodypublic String say(){return "学校:"+school.getName()+" "+"校长:"+school.getMaster()+" "+"学校官网:"+school.getWebsit();}
1、添加mybatis依赖
2、添加Oracle驱动
3、使用Mybatis提供的逆向工程生成实体bean,映射文件,dao接口
mysql mysql-connector-java org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.2
我们可以通过配置文件和插件来自动生成我们需要的模块
插件
org.mybatis.generator mybatis-generator-maven-plugin 1.3.6 GeneratorMapper.xml true true
配置文件(在项目根目录创建)
运行:
我们可以看到已经生成了
关于生成的Mapper映射文件
id, name, age delete from t_studentwhere id = #{id,jdbcType=INTEGER} insert into t_student (id, name, age)values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}) insert into t_studentid, name, age, #{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}, update t_studentname = #{name,jdbcType=VARCHAR}, age = #{age,jdbcType=INTEGER}, where id = #{id,jdbcType=INTEGER} update t_studentset name = #{name,jdbcType=VARCHAR},age = #{age,jdbcType=INTEGER}where id = #{id,jdbcType=INTEGER}
1、准备好实体类、mapper接口、mapper映射文件(由GeneratorMapper.xml使用mybatis逆向工程生成)
2、创建service层以及其实现层
service
package com.dunv.study.service;import com.dunv.study.model.Student;/*** @author ZS* @Description* @date 2022/11/29 13:48*/
public interface StudentService {/*** 根据学生ID查询详情* @param id* @return*/Student queryStudentById(Integer id);
}
serviceImpl
为StudentServiceImpl 添加@Service注解
package com.dunv.study.service.impl;import com.dunv.study.mapper.StudentMapper;
import com.dunv.study.model.Student;
import com.dunv.study.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** @author ZS* @Description* @date 2022/11/29 13:49*/
@Service //为StudentServiceImpl 添加@Service注解
public class StudentServiceImpl implements StudentService {@Autowiredprivate StudentMapper studentMapper;@Overridepublic Student queryStudentById(Integer id) {Student student = studentMapper.selectByPrimaryKey(id);return student;}
}
2、创建控制层的StudentController
package com.dunv.study.controller;import com.dunv.study.model.Student;
import com.dunv.study.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;/*** @author ZS* @Description* @date 2022/11/29 13:45*/@Controller
public class StudentController {@Autowiredprivate StudentService studentService;@RequestMapping(value = "/student")@ResponseBodypublic Object student(Integer id){Student student = studentService.queryStudentById(id);return student;}
}
3、为mapper接口添加mapper扫描
package com.dunv.study;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @author 28259*/
@SpringBootApplication
@MapperScan("com.dunv.study.mapper")//开启扫描mapper接口的包以及子目录
class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}
4、在pom.xml的build中指定文件夹为resource
src/main/java **/*.xml
5、运行并访问
数据库信息
查询结果
1、把mapper.xml映射文件放到resource/mapper目录下
2、在核心配置文件application.properties中指定
#设置数据库连接
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/dunv?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true
spring.datasource.username=root
spring.datasource.password=654321#指定mybatis映射文件的位置
mybatis.mapper-locations=classpath:mapper/*.xml
3、运行并访问
1️⃣Springboot集成Mybatis
2️⃣Springboot集成mybatis,最主要的两个注解是@Mapper和@MapperScan
3️⃣关于映射文件存放的位置
1、将Mapper接口和Mapper映射文件存放到src.mian/java同一目录下,
还需要在pom文件中手动指定资源文件夹的路径resource
2、将Mapper接口和Mapper映射文件分开存放,需要在核心配置文件中指定mapper映射文件存放的位置
事物是一个完整的功能,也叫一个完整的业务
事物只跟什么SQL语句有关系?(DML【数据操纵语言】:增、删、改)
上面说了,事物是一个完整的业务,里面包含了多个执行单元。由于事物的一致性原则——这些执行单元要么全部执行,要么全不执行
我们以数据修改为例
public int updateStudentById(Student student) {//修改成功int i = studentMapper.updateByPrimaryKey(student);//失败int a = 10/0;return i;}
在上面的方法中,就可以把这个方法看成是一个事务,里面包含了两个执行单位
第二个 int a = 10/0 肯定会报错
现在我们要把数据库里面的哈哈修改成啦啦,如果不添加事务,我们可以看看结果是什么样的。
我们可以看到页面上报错了
后端控制台也报错了,正是第二个错误
此时我们再看数据库
数据被修改了!,也就是说int i = studentMapper.updateByPrimaryKey(student);执行成功了,但是int a = 10/0执行失败了。这违反了事务的一致性原则
此时我们应该添加事务
在对应的业务上添加 @Transactional(Springboot2.0以上)注解即可,这样就不会保持事务的一致性原则
@Transactional@Overridepublic int updateStudentById(Student student) {//修改成功int i = studentMapper.updateByPrimaryKey(student);//失败int a = 10/0;return i;}
}