JAVA分布式——Springboot(二)Springboot集成JSP,Springboot集成事务,Springboot集成SSM框架
一、集成JSP1.新建一个目录webapp,并修改文件夹属性第一步:创建一个Springboot-web项目第二步:将该文件夹进行设置2. 在pom.xml文件中添加依赖<dependencies><!--springboot框架web项目起步依赖--><dependency><groupId>org.springframework.boot<
·
一、集成JSP
1.新建一个目录webapp,并修改文件夹属性
-
第一步:创建一个Springboot-web项目
-
第二步:将该文件夹进行设置
2. 在pom.xml文件中添加依赖
<dependencies>
<!--springboot框架web项目起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入Springboot内嵌Tomcat对jsp的解析依赖-->
<!--如果只是使用JSP页面,添加以下依赖-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!--使用jsp依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<!--如果使用 JSTL 必须添加该依赖-->
<!--jstl 标签依赖的 jar 包 start-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
<build>
<!--springboot默认推荐使用前端引擎是thymeleaf
现在要使用springboot集成jsp,手动指定jsp最后编译的路径
SpringBoot 要求 jsp 文件必须编译到指定的 META-INF/resources
目录下才能访问,否则访问不到。
而且springboot集成jsp编译jsp的路径是spring boot规定好的位置
-->
<resources>
<resource>
<!--原文件夹-->
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<!--指定原文件夹中的哪个资源要编译进行-->
<includes>
<include>*.*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- 注意几点
- SpringBoot 要求 jsp 文件必须编译到指定的
META-INF/resources
目录才能访问,否则访问不到。(需要在build中指定) - 不仅要对springboot中的jsp添加也来,更重要的是让spring boot内置的Tomcat对jsp的解析依赖
3.添加视图解析器
- 在springboot的properties 文件中进行添加
#配置视图解析器
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
4. 测试并进行编写
- 写一个Controller
@Controller
public class hello {
@RequestMapping(value = "/hello")
public ModelAndView hello(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg","hello!springboot-jsp");
modelAndView.setViewName("hello");
return modelAndView;
}
}
- 新建一个jsp,并用el进行获取
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<H1>${msg}</H1>
</body>
</html>
- 测试结果
二、集成Mybatis
1. 引入依赖
整合Mybatis,我们只需要在spring boot的pom.xml文件中添加依赖即可
- mybatis整合Springboot的依赖和MySQL依赖
<properties>
<java.version>1.8</java.version>
<!--修改父工程管理的依赖号-->
<mysql.version>5.1.9</mysql.version>
</properties>
<dependencies>
<!--起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis整合springboot框架的起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
</dependencies>
2. 核心配置文件application.properties 中配置数据源
#配置数据库的连接信息
#注意这里的驱动类有变化
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.195.128:3306/springboot?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=122217gui
- 如果使用MySQL8的话,
driver-class-name
使用如下属性:spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
3.(可忽略)使用Mybatis逆向工程
- 1. 先创建一个数据库
- 2. 在项目中引入配置文件GeneratorMapper.xml
根据自己项目进行修改
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 指定连接数据库的 JDBC 驱动包所在位置,指定到你本机的完整路径 -->
<classPathEntry location="D:\a_JAVAEE常用jar包\mysql-connector-java-5.1.39"/>
<!-- 配置 table 表信息内容体,targetRuntime 指定采用 MyBatis3 的版本 -->
<context id="tables" targetRuntime="MyBatis3">
<!-- 抑制生成注释,由于生成的注释都是英文的,可以不让它生成 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 配置数据库连接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://192.168.195.131:3306/springboot"
userId="root"
password="122217gui">
</jdbcConnection>
<!-- 生成 model 类,targetPackage 指定 model 类的包名, targetProject 指定
生成的 model 放在 eclipse 的哪个工程下面-->
<javaModelGenerator targetPackage="com.aiit.springboot.model"
targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="false" />
</javaModelGenerator>
<!-- 生成 MyBatis 的 Mapper.xml 文件,targetPackage 指定 mapper.xml 文件的
包名, targetProject 指定生成的 mapper.xml 放在 eclipse 的哪个工程下面 -->
<sqlMapGenerator targetPackage="com.aiit.springboot.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- 生成 MyBatis 的 Mapper 接口类文件,targetPackage 指定 Mapper 接口类的包
名, targetProject 指定生成的 Mapper 接口放在 eclipse 的哪个工程下面 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.aiit.springboot.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 数据库表名及对应的 Java 模型类名 -->
<table tableName="t_student" domainObjectName="Student"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>
- 3. 在项目的pom.xml中引入逆向工程插件
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>GeneratorMapper.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
- 4. 双击插件
4.(可忽略)装编译文件插件
- 如果我们的mapper文件和dao层接口放在一起了,那么我们需要进行指定编译文件。
<build>
<!--手动指定文件夹为resources-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<build>
5 .Dao层接口上使用@mapper注解
- 作用:mybatis 自动扫描数据持久层的映射文件及 DAO 接口的关系
6. 使用@mapperScan注解
- 因使用@mapper注解过于复杂一个dao层接口就需要一个@mapper注解,所以使用@mapperScan注解
@SpringBootApplication
@MapperScan(basePackages = "com.aiit.springboot.dao")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
7. 第一种mapper存放位置手动指定资源文件夹
- 因为我们的mapper.xml文件放在了Dao下面,该方式在Springboot中需要指定文件配置。所以我们需要在添加扫描
- pom.xml文件中引入该段
<build>
<!--手动指定文件夹为resources-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<build>
8. 第二种mapper文件位置新建mapper文件夹
- 第一步:现在resources下建一个mapper文件夹
- 第二步:在核心配置文件指定mapper的文件位置
#设置连接数据库的配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.195.128:3306/springboot?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=122217gui
#指定Mybatis映射文件路径
mybatis.config-location=classpath:mapper/*.xml
三、集成事务
- 如果,我们在对增删改,需要进行事务控制,只需要加上
@Transactional
即可 - 如果错误就会回滚,如果正确就会继续往下执行
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Override
@Transactional
public int updateStudentById(Student student) {
//修改成功
int i =studentDao.updateByID(student);
//失败
int a = 10/0;
return i;
}
}
四、集成SpringMVC
- SpringBoot集成SpringMVC和以往使用SpringMVC是一样的
- 多介绍几个注解
4.1 @Controller
- Spring MVC 的注解,处理 http 请求
4.2 @RestController
- Spring 4 后新增注解,是@Controller 注解功能的增强是
@Controller
与@ResponseBody
的组合注解 - 如果一个 Controller 类添加了
@RestController
,那么该 Controller 类下的所有方法都相当于添加了@ResponseBody
注解用于返回字符串或 json 数据
4.3 @RequestMapping
- 支持 Get 请求,也支持 Post 请求
4.4 @GetMapping
- RequestMapping 和 Get 请求方法的组合
- 只支持 Get 请求
- Get 请求主要用于查询操作
4.5 @PostMapping
- RequestMapping 和 Post 请求方法的组合
- 只支持 Post 请求
- Post 请求主要用户新增数据
4.6 @PutMapping
- RequestMapping 和 Put 请求方法的组合
- 只支持 Put 请求
- Put 通常用于修改数据
4.7 @DeleteMapping
- RequestMapping 和 Delete 请求方法的组合
- 只支持 Delete 请求
- 通常用于删除数据
更多推荐
所有评论(0)