Spring Boot 學習筆記(三)Spring boot 中的SSM
阿新 • • 發佈:2019-01-10
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=.jsp
pom.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/*.xml
3、在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=false
4、在MyBatis的Mapper介面中新增@Mapper註解 或者 在執行的主類上新增 @MapperScan("com.bjpowernode.springboot.mapper") 註解包掃描
Spring boot 事務支援
1、在入口類中使用註解 @EnableTransactionManagement 開啟事務支援;
2、在訪問資料庫的Service方法上添加註解 @Transactional 即可;
3、其實還是用spring 的事務管理。