javascript
Spring Boot 学习笔记(三)Spring boot 中的SSM
Spring boot 下的 Spring mvc
@Controller:即為Spring mvc的注解,處理http請求;
@RestController:Spring4后新增注解,是@Controller與@ResponseBody的組合注解,用于返回字符串或json數據;
package com.example.springbootweb.model;public class User {private int id;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;} } package com.example.springbootweb.controller;import com.example.springbootweb.model.User; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@RestController public class MVCController {@RequestMapping("/boot/getUser")public Object getUser(){User user =new User();user.setId(100);user.setName("王衛");return user;} }@GetMapping:RequestMapping 和 Get請求方法的組合;
//舊@RequestMapping(value = "/boot/getUser", method = RequestMethod.GET)//新@GetMapping("/boot/getUser")@PostMapping:RequestMapping 和 Post請求方法的組合;
@PutMapping:RequestMapping 和 Put請求方法的組合;
@DeleteMapping:RequestMapping 和 Delete請求方法的組合;
?
Spring boot 使用 JSP
pom.xml 添加依賴項
<!--引入Spring Boot內嵌的Tomcat對JSP的解析包--><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId></dependency><!-- servlet依賴的jar包start --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId></dependency><!-- jsp依賴jar包 --><dependency><groupId>javax.servlet.jsp</groupId><artifactId>javax.servlet.jsp-api</artifactId><version>2.3.1</version></dependency><!--jstl標簽依賴的jar包 --><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId></dependency></dependencies>application.properties 添加配置
#前端視圖展示jsp spring.mvc.view.prefix=/ spring.mvc.view.suffix=.jsppom.xml build中要配置備注中的配置信息?
<build><plugins><!--springboot提供的項目編譯打包插件--><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins><!--jsp打包編譯--><resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes></resource><resource><directory>src/main/resources</directory><includes><include>**/*.*</include></includes></resource><resource><directory>src/main/webapp</directory><targetPath>META-INF/resources</targetPath><includes><include>**/*.*</include></includes></resource></resources></build>新建?Controller
package com.example.springbootweb.controller;import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping;@Controller public class JSPController {@GetMapping("/boot/index")public String index(Model model){model.addAttribute("msg","springboot 集成 jsp");return "index";} }在main文件下建立webapp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body> ${msg} </body> </html>?
Spring boot 集成 MyBatis
1、在pom.xml中配置相關jar依賴。
<!-- 加載mybatis整合springboot --> <dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.1</version> </dependency><!-- MySQL的jdbc驅動包 --> <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId> </dependency>2、在Springboot的核心配置文件 application.properties 中配置MyBatis的Mapper.xml文件所在位置:
#配置MyBatis的Mapper.xml文件所在位置 mybatis.mapper-locations=classpath:com/example/springbootweb/mapper/*.xml3、在Springboot的核心配置文件application.properties中配置數據源:
spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test_db?useUnicode=true&characterEncoding=utf8&useSSL=false4、在MyBatis的Mapper接口中添加@Mapper注解 或者 在運行的主類上添加 @MapperScan("com.bjpowernode.springboot.mapper") 注解包掃描
?
Spring boot 事務支持
1、在入口類中使用注解 @EnableTransactionManagement 開啟事務支持;
2、在訪問數據庫的Service方法上添加注解 @Transactional 即可;
3、其實還是用spring 的事務管理。
轉載于:https://www.cnblogs.com/lilb/p/10251579.html
總結
以上是生活随笔為你收集整理的Spring Boot 学习笔记(三)Spring boot 中的SSM的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 爱聊APP如何发联系方式(汉典爱字的基本
- 下一篇: MySQL问题汇总