SpringBoot概述
SpringBoot能够简化SSM框架进行的开发过程,在我们使用SSM框架进行项目开发的时候需要配置大量的配置文件,比如配置web.xml,配置Sping,配置MyBatis等一些配置文件,而SpringBoot框架完全抛弃了这些配置xml文件的过程,采用大量的默认配置来简化项目开发过程,注意SpringBoot底层还是Spring
SpringBoot特性
1.能够直接使用Java Main函数启动内嵌的Tomcat服务器来运行SpringBoot程序,不需要部署war包文件
2.提供约定的Starter POM来简化Maven配置,让Maven配置变得简单
3.提供了程序的健康检查
4.基本可以完全不使用XML配置文件,采用注解配置
SpringBoot核心配置文件
- .properteis文件(默认:
当有多个配置文件时
可以使用如下来配置使用哪个配置文件
#当有个多配置文件的时候可以设置使用哪个配置文件
spring.profiles.active=test
#配置内嵌Tomcat的端口号
server.port=80
#配置项目访问的跟路径
server.servlet.context-path=/SpringBootProject
- .yml文件
server:
port: 80
servlet:
context-path: /SpringBootProject
注意:.yml文件的格式有点类似于Python的语法格式,注意的是冒号和属性值之间有空格否则不识别
读取自定义配置:
- 方式1:
@Value("${键值}")
private String 变量名;
-
方式2:
-
对应的JavaBean:
@Component
@ConfigurationProperties
public class config {
private String play_url;
private String pay_notifyUrl;
public String getPlay_url() {
return play_url;
}
public void setPay_notifyUrl(String pay_notifyUrl) {
this.pay_notifyUrl = pay_notifyUrl;
}
public void setPlay_url(String pay_url) {
this.play_url = pay_url;
}
@Override
public String toString() {
return "config{" +
"pay_url='" + play_url + '\'' +
", pay_notifyUrl='" + pay_notifyUrl + '\'' +
'}';
}
public String getPay_notifyUrl() {
return pay_notifyUrl;
}
}
@ConfigurationProperties可以将application.properties文件中的配置映射到JavaBean的属性中,@Component是将当前JavaBean标记为SpringBoot容器中的Bean对象。在其他地方可以使用@Autowired注解来new该JavaBean
- Controller层:
@Autowired
private config confing_1;
SpringBoot使用jsp
项目目录
properties文件:
#jsp页面在src/main/webapp目录下
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
pom.xml文件导入的依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 添加servlet依赖模块 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<!-- 添加jstl标签库依赖模块 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!--添加tomcat依赖模块.-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<!-- 使用jsp引擎,springboot内置tomcat没有此依赖 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
在Controller层
@org.springframework.stereotype.Controller
public class Controller {
@RequestMapping("/boot/jsp")
public String jsp(Model model){
model.addAttribute("data","参数传递测试");
return "index";
}
}
前端页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h4>修改成功<a href="TJCuserinfo.jsp">返回</a></h4>
<h1>${data}</h1>
</body>
</html>
需要在pom.xml文件的<build>节点中添加:
<resources>
<resource>
<!--源文件位置-->
<directory>src/main/webapp</directory>
<!--编译到META-INF/resources目录下-->
<targetPath>META-INF/resources</targetPath>
<includes>
<!--要把文件编译过去-->
<include>**/*.*</include>
</includes>
</resource>
</resources>
SpringBoot整合MyBatis
- 需要在pom.xml文件中需要导入依赖:
<!--加载MyBatis整合SpringBoot起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--引入MySQL的JDBC驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
- 在pom.xml文件的<build>节点添加:
<resources>
<!--把src/main/java目录下的xml文件也编译到class下面去-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!--把src/main/resources目录下的所有配置文件也编译到class下面去-->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
<!--springboot使用的web资源要编译到META-INF/resources-->
<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
- 项目目录:
- application.properties文件中:
server.port=8080
server.servlet.context-path=/springboot
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC
#指定mapper.xml的位置,如果mapper.xml和接口类在一个包下就不需要指定了
#mybatis.mapper-locations=classpath:mapper/*.xml
- 在StudentService的实现类中:
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Override
public Student queryStudent(String id) {
return studentDao.selectStudent(id);
}
}
注意:这里@Service注解只有在Service实现类中,不能使用在接口中否则找不到Service
- StudentDao.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.example.demo.dao.StudentDao">
<select id="selectStudent" resultType="com.example.demo.entity.Student" parameterType="java.lang.String">
select * from student where id=#{id}
</select>
</mapper>
注意:当MyBatis的映射文件和Dao层中的XXXXDao同在一个目录下时需要和该文件同名,如果MyBaties的映射文件在resource目录下时需要在application.properties文件指定mapper.xml的位置
在Controller层:
@Controller
public class MyController {
@Autowired
private StudentService studentService;
@RequestMapping("/student")
@ResponseBody
public Student queryOneStudent(){
return studentService.queryStudent("2ec27dc8b0a946e98f19eede983aee84");
}
}
- 在main函数中:
@MapperScan(basePackages = "com.example.demo.dao")
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
注意:这里建议使用@MapperScan注解来来处理Dao层中的XXDao和Mapper.xml文件的映射关系,使用@Mapper的话可能因org.apache.ibatis.annotations.Mapper包无法识别而导致@Mapper无法使用
- 使用@Mapper注解:
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface StudentDao {
Student selectStudent(String id);
}
注意:这里必须是org.apache.ibatis.annotations.Mapper,有时候可能因为导入的包不对导致该注解无法正常使用
执行插入操作
StudentDao:
@Mapper
public interface StudentDao {
void addStudent(@Param("id")String id,@Param("name")String name,@Param("age")String age);
}
StudentDao.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.example.test03.dao.StudentDao">
<insert id="addStudent" parameterType="java.lang.String">
insert into student(id,name,age) value(#{id},#{name},#{age})
</insert>
</mapper>
使用MyBatis的别名机制
#使用MyBatis的别名机制
mybatis.type-aliases-package=com.example.demo.entity