mysql 页面跳转_Springboot+MyBatis+mysql+jsp页面跳转详细示例
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项目的启动类
packageSpringBoot_jsp.springbootJsp;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.EnableAutoConfiguration;importorg.springframework.boot.autoconfigure.SpringBootApplication;/*** SpringBoot的启动类
* 自动启动内置的tomcat
*@authorAdministrator
**/@SpringBootApplication
@EnableAutoConfigurationpublic classApplicationMain {public static voidmain(String[] args) {
SpringApplication.run(ApplicationMain.class,args);
}
}
(5)UserController.java : 前端控制类
使用@Controller注解,可以跳转到jsp、html页面,也可以返回json数据
需要返回json数据的时候,在方法上添加@ResponseBody注解
packageSpringBoot_jsp.springbootJsp.controller;importjavax.annotation.Resource;importjavax.servlet.http.HttpServletRequest;importorg.mybatis.spring.annotation.MapperScan;importorg.springframework.context.annotation.ComponentScan;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importSpringBoot_jsp.springbootJsp.pojo.User;importSpringBoot_jsp.springbootJsp.service.UserService;/*** 前端控制器类,调用业务层方法处理请求
*@authorAdministrator
**/@Controller
@ComponentScan({"SpringBoot_jsp.springbootJsp.service"})
@MapperScan("SpringBoot_jsp.springbootJsp.mapper")public classUserController {
@ResourceprivateUserService userService;/*** 根据id查询出对应的用户
*@paramid
*@paramrequest
*@return
*/@RequestMapping("/findById")publicString findById(HttpServletRequest request){
User user= userService.find(18);
request.setAttribute("user", user);return "first/showUser";
}
/*** 跳转到skipped.jsp
* @paramrequest
* @return
*/
@RequestMapping("/skip")publicString skip(HttpServletRequest request){
User user= userService.find(18);
request.setAttribute("user", user);return "skip/skipped";
}
}
(6) UserMapper.java :持久层的接口设计,使用@Mapper注解标明这是个mapper接口
packageSpringBoot_jsp.springbootJsp.mapper;importorg.apache.ibatis.annotations.Mapper;importSpringBoot_jsp.springbootJsp.pojo.User;/*** 持久层接口,定义增删改查方法
*@authorAdministrator
**/@Mapperpublic interfaceUserMapper {voidinsert(User user);void delete(intid);voidedit(User user);
User find(intid);
}
(7)User.java:实体类 ,与数据库中的字段一 一对应。
packageSpringBoot_jsp.springbootJsp.pojo;/*** 实体类
*@authorAdministrator
**/
public classUser {private intid;privateString username;privateString password;private intage;publicUser(){}/** setter and getter 方法 */
}
(8) UserIService.java:业务层接口
packageSpringBoot_jsp.springbootJsp.service;importSpringBoot_jsp.springbootJsp.pojo.User;/*** 业务层接口
*@authorAdministrator
**/
public interfaceUserIService {voidinsert(User user);void delete(intid);voidedit(User user);
User find(intid);
}
(9) UserService.java: 业务层接口实现类 ,调用mapper接口中的方法完成对数据库的操作
packageSpringBoot_jsp.springbootJsp.service;importjavax.annotation.Resource;importorg.mybatis.spring.annotation.MapperScan;importorg.springframework.context.annotation.ComponentScan;importorg.springframework.stereotype.Service;importSpringBoot_jsp.springbootJsp.mapper.UserMapper;importSpringBoot_jsp.springbootJsp.pojo.User;/*** 业务层实现类
*@authorAdministrator
**/@Service("userService")
@ComponentScan({"SpringBoot_jsp.springbootJsp.mapper"})public class UserService implementsUserIService {
@ResourceprivateUserMapper userMapper;
@Overridepublic voidinsert(User user) {
userMapper.insert(user);
}
@Overridepublic void delete(intid) {
userMapper.delete(id);
}
@Overridepublic voidedit(User user) {
userMapper.edit(user);
}
@Overridepublic User find(intid) {returnuserMapper.find(id);
}
}
(10)mapper/UserMapper.xml:UserMapper接口对应的mapper.xml文件,里面是sql语句
INSERT INTO USER(ID,USERNAME,PASSWORD,AGE) VALUES(#{id},#{username},#{password},#{age});
UPDATE USER SET USERNAME=#{username} , PASSWORD=#{password} , AGE = #{age} WHERE ID=#{id}
SELECT ID,USERNAME,PASSWORD,AGE FROM USER WHERE ID=#{0}
DELETE FROM USER WHERE ID=#{0}
(11) showUser.jsp : 访问findById方法返回的页面
在页面发送请求使用 url="/项目名/请求名.do"
例如:显示用户信息
HELLO THIS IS SHOWUSER JSP.
${user.id }
${user.username }
${user.password }
(12)skipped.jsp: 从showUser.jsp页面请求/skip方法跳转到此页面jsp跳转
Skip jsp
(13)pom.xml:maven项目的配置文件
4.0.0
SpringBoot_jsp
springbootJsp
war
0.0.1-SNAPSHOT
springbootJsp Maven Webapp
http://maven.apache.org
org.springframework.boot
spring-boot-starter-parent
1.5.6.RELEASE
bootdemo
true
org.apache.maven.plugins
maven-compiler-plugin
3.5.1
${compiler.source}
${compiler.target}
${project.build.sourceEncoding}
${project.basedir}/src/main/webapp/WEB-INF/lib
org.springframework.boot
spring-boot-maven-plugin
UTF-8
1.7
1.7
3.1.0
2.3.1
1.2
4.12
org.apache.tomcat.embed
tomcat-embed-jasper
provided
junit
junit
${junit.version}
test
javax.servlet
javax.servlet-api
${servlet.version}
provided
javax.servlet.jsp
javax.servlet.jsp-api
${jsp.version}
provided
javax.servlet
jstl
${jstl.version}
org.objenesis
objenesis
1.2
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.2.0
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat
provided
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-configuration-processor
true
mysql
mysql-connector-java
5.1.6
runtime
org.springframework.boot
spring-boot-starter-freemarker
(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页面,结果如下: