SpringBoot与MyBatis搭建环境,底层数据库为mysql,页面使用JSP(官网上不推荐使用jsp),完成从数据库中查询出数据,在jsp页面中显示,并且实现页面的跳转功能。
项目下载链接: https://github.com/DFX339/springbootJsp.git
(1)新建数据库 springboot_mybatis
新建表 user,表的结构如下:
(2)新建maven项目---web项目 springbootJsp,项目目录结构如下
ApplicationMain.java : SpringBoot项目的启动类
UserController.java : 前端控制类
UserMapper.java :持久层的接口设计
User.java:实体类
UserIService.java:业务层接口
UserService.java: 业务层接口实现类
mapper/UserMapper.xml:UserMapper接口对应的xml文件,里面是sql语句
application.properties :这是SpringBoot的默认配置文件名,存放在resources目录下。 必须命名为application.properties,否则读取不了 需要另外配置
showUser.jsp : 访问findById方法返回的页面
skipped.jsp: 从showUser.jsp页面请求/skip方法跳转到此页面
pom.xml:maven项目的配置文件
(3)springboot的配置文件: application.properties (放在src/main/resources目录下,会自动读取)
#指定跳转的前缀,这里表示所有的页面都存放在/WEB-INF/jsp文件下
spring.mvc.view.prefix=/WEB-INF/jsp/
# 指定跳转页面的后缀,这里表示所有的页面都是jsp页面
spring.mvc.view.suffix=.jsp
#指定跳转的前缀 spring.mvc.view.prefix=/WEB-INF/jsp/ # 指定跳转页面的后缀 spring.mvc.view.suffix=.jsp #访问的项目名 server.context-path=/SpringbootJsp #数据库的配置 spring.datasource.driver-class-name =com.mysql.jdbc.Driver spring.datasource.url =jdbc:mysql://localhost:3306/springboot_mybatis spring.datasource.username =root spring.datasource.password =root #mybatis配置文件的配置 mybatis.typeAliasesPackage=SpringBoot_jsp.springbootJsp.pojo mybatis.mapperLocations=classpath:/mapper/UserMapper.xml
(4)ApplicationMain.java : SpringBoot项目的启动类
package SpringBoot_jsp.springbootJsp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * SpringBoot的启动类 * 自动启动内置的tomcat * @author Administrator * */ @SpringBootApplication @EnableAutoConfiguration public class ApplicationMain { public static void main(String[] args) { SpringApplication.run(ApplicationMain.class,args); } }
(5)UserController.java : 前端控制类
使用@Controller注解,可以跳转到jsp、html页面,也可以返回json数据
需要返回json数据的时候,在方法上添加@ResponseBody注解
package SpringBoot_jsp.springbootJsp.controller; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.ComponentScan; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import SpringBoot_jsp.springbootJsp.pojo.User; import SpringBoot_jsp.springbootJsp.service.UserService; /** * 前端控制器类,调用业务层方法处理请求 * @author Administrator * */ @Controller @ComponentScan({"SpringBoot_jsp.springbootJsp.service"}) @MapperScan("SpringBoot_jsp.springbootJsp.mapper") public class UserController { @Resource private UserService userService; /** * 根据id查询出对应的用户 * @param id * @param request * @return */ @RequestMapping("/findById") public String findById(HttpServletRequest request){ User user = userService.find(18); request.setAttribute("user", user); return "first/showUser"; }
/** * 跳转到skipped.jsp
* @param request
* @return
*/ @RequestMapping("/skip") public String skip(HttpServletRequest request){ User user = userService.find(18); request.setAttribute("user", user); return "skip/skipped"; } }
(6) UserMapper.java :持久层的接口设计,使用@Mapper注解标明这是个mapper接口
package SpringBoot_jsp.springbootJsp.mapper; import org.apache.ibatis.annotations.Mapper; import SpringBoot_jsp.springbootJsp.pojo.User; /** * 持久层接口,定义增删改查方法 * @author Administrator * */ @Mapper public interface UserMapper { void insert(User user); void delete(int id); void edit(User user); User find(int id); }
(7)User.java:实体类 ,与数据库中的字段一 一对应。
package SpringBoot_jsp.springbootJsp.pojo; /** * 实体类 * @author Administrator * */ public class User { private int id; private String username; private String password; private int age; public User(){} /** setter and getter 方法 */ }
(8) UserIService.java:业务层接口
package SpringBoot_jsp.springbootJsp.service; import SpringBoot_jsp.springbootJsp.pojo.User; /** * 业务层接口 * @author Administrator * */ public interface UserIService { void insert(User user); void delete(int id); void edit(User user); User find(int id); }
(9) UserService.java: 业务层接口实现类 ,调用mapper接口中的方法完成对数据库的操作
package SpringBoot_jsp.springbootJsp.service; import javax.annotation.Resource; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.ComponentScan; import org.springframework.stereotype.Service; import SpringBoot_jsp.springbootJsp.mapper.UserMapper; import SpringBoot_jsp.springbootJsp.pojo.User; /** * 业务层实现类 * @author Administrator * */ @Service("userService") @ComponentScan({"SpringBoot_jsp.springbootJsp.mapper"}) public class UserService implements UserIService { @Resource private UserMapper userMapper; @Override public void insert(User user) { userMapper.insert(user); } @Override public void delete(int id) { userMapper.delete(id); } @Override public void edit(User user) { userMapper.edit(user); } @Override public User find(int id) { return userMapper.find(id); } }
(10)mapper/UserMapper.xml:UserMapper接口对应的mapper.xml文件,里面是sql语句
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 指定工作空间,要与接口名相同,源代码没有去看,猜测应该是通过"这里的namespace.下边方法的id"来定位方法的 --> <mapper namespace="SpringBoot_jsp.springbootJsp.mapper.UserMapper"> <!-- insert user --> <insert id = "insert" parameterType="SpringBoot_jsp.springbootJsp.pojo.User"> INSERT INTO USER(ID,USERNAME,PASSWORD,AGE) VALUES(#{id},#{username},#{password},#{age}); </insert> <!-- update user --> <update id="edit" parameterType="SpringBoot_jsp.springbootJsp.pojo.User"> UPDATE USER SET USERNAME=#{username} , PASSWORD=#{password} , AGE = #{age} WHERE ID=#{id} </update> <!-- find user --> <select id="find" parameterType="int" resultType="SpringBoot_jsp.springbootJsp.pojo.User"> SELECT ID,USERNAME,PASSWORD,AGE FROM USER WHERE ID=#{0} </select> <!-- delete user --> <delete id="delete" parameterType="int"> DELETE FROM USER WHERE ID=#{0} </delete> </mapper>
(11) showUser.jsp : 访问findById方法返回的页面
在页面发送请求使用 url="/项目名/请求名.do"
例如:<a href="/SpringbootJsp/skip.do">转到skip.jsp</a>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>显示用户信息</title>
</head>
<body>
<h1>HELLO THIS IS SHOWUSER JSP.</h1>
<h2>${user.id }</h2>
<h2>${user.username }</h2>
<h2>${user.password }</h2>
<a href="/SpringbootJsp/skip.do">转到skip.jsp</a>
</body>
</html>
(12)skipped.jsp: 从showUser.jsp页面请求/skip方法跳转到此页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>jsp跳转</title>
</head>
<body>
<h1>Skip jsp</h1>
<a href="/SpringbootJsp/findById.do">转到showUser.jsp</a>
</body>
</html>
(13)pom.xml:maven项目的配置文件
<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>SpringBoot_jsp</groupId> <artifactId>springbootJsp</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>springbootJsp Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <build> <finalName>bootdemo</finalName> <plugins> <plugin> <inherited>true</inherited> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>${compiler.source}</source> <target>${compiler.target}</target> <encoding>${project.build.sourceEncoding}</encoding> <compilerArguments> <extdirs>${project.basedir}/src/main/webapp/WEB-INF/lib</extdirs> </compilerArguments> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <compiler.source>1.7</compiler.source> <compiler.target>1.7</compiler.target> <!-- servlet/jsp/EL (2.4/2.0/?)(2.5/2.1/2.1),(3.0/2.2/2.2),(3.1/2.3/3.0) --> <servlet.version>3.1.0</servlet.version> <jsp.version>2.3.1</jsp.version> <jstl.version>1.2</jstl.version> <junit.version>4.12</junit.version> </properties> <dependencies> <!-- Springboot添加视图解析器的依赖包 --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>${jsp.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>org.objenesis</groupId> <artifactId>objenesis</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> <scope>runtime</scope> </dependency> <!-- freemarker jar包导入 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> </dependencies> </project>
(14)启动项目。
进入 ApplicationMain.java
右击 --》 Run As --》 Java Application
启动成功会出现如图结果:
(15)访问项目。
在浏览器中通过URL访问项目。 格式: http://主机IP:8080/application.properties中指定的server.context-path的值/@Controller标识的类指定的RequestMapping值
例如: http://127.0.0.1:8080/SpringbootJsp/findById
访问结果如下:
点击页面中的超链接 转到skip.jsp,进入skipped.jsp页面,结果如下: