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 的事务管理。