springboot+jsp+mybatis项目实例(后台成功,但是无法跳转jsp页面,没有实体类的注解,看springboot+jsp第二弹相关配置,即可成功配置jsp)
SpringBoot是用来简化SpringMvc开发的项目,这里自然要整合mybatis等持久化框架!
先看看项目目录:
一、在pom.xml中配置依赖jar包:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mfc</groupId>
<artifactId>springboot</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springboot Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
提示:springboot热部署配置:
热部署:修改java代码后直接保存即可在浏览器上运行,不需要手动重启服务器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
二、项目入口:
package com.mfc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
三、控制器类:
package com.mfc.ctrl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.mfc.dao.UserMapper;
import com.mfc.entity.User;
@RestController
@RequestMapping("/userController")
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping(value={"/selectUserById"}, method=RequestMethod.GET)
public ModelAndView selectUserById(String id){
User user = userMapper.selectUserById(Integer.parseInt(id));
ModelAndView mav = new ModelAndView("updateUser");
mav.addObject("user", user);
return mav;
}
@RequestMapping(value={"/selectAllUser"}, method=RequestMethod.GET)
public ModelAndView selectAllUser(){
List<User> list = userMapper.selectAllUser();
ModelAndView mav = new ModelAndView("alluser");
mav.addObject("users", list);
return mav;
}
@RequestMapping(value={"/addUserPage"}, method=RequestMethod.GET)
public ModelAndView addUserPage(){
ModelAndView mav = new ModelAndView("adduser");
return mav;
}
@RequestMapping(value={"/addUser"}, method=RequestMethod.POST)
public ModelAndView addUser(User user){
userMapper.addUser(user);
ModelAndView mav = new ModelAndView("redirect:selectAllUser");
return mav;
}
@RequestMapping(value={"/updateUser"}, method=RequestMethod.POST)
public ModelAndView updateUser(User user){
userMapper.updateUser(user);
ModelAndView mav = new ModelAndView("redirect:selectAllUser");
return mav;
}
@RequestMapping(value={"/deleteUser"}, method=RequestMethod.GET)
public ModelAndView deleteUser(String id){
userMapper.deleteUser(Integer.parseInt(id));
ModelAndView mav = new ModelAndView("redirect:selectAllUser");
return mav;
}
}
四、资源配置文件:application.properties
#配置数据库连接数据
spring.datasource.url=jdbc:mysql://localhost:3306/springboot
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#配置控制器里面返回页面的前后缀
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
五、dao层以及service层和springmvc中一样使用,实体类省略!
这里的dao层使用注解实现:
package com.mfc.dao;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.mfc.entity.User;
@Mapper
public interface UserMapper {
@Select("select * from user where id = #{id}")
public User selectUserById(int id);
@Select("select * from user")
public List<User> selectAllUser();
@Insert("insert into user(userName,userAge,userAddress) values (#{userName},#{userAge},#{userAddress})")
public void addUser(User user);
@Update("update user set userName=#{userName},userAge=#{userAge},userAddress=#{userAddress} where id=#{id}")
public void updateUser(User user);
@Delete("delete from user where id=#{id}")
public void deleteUser(int id);
}