mysql 页面跳转_Springboot+MyBatis+mysql+jsp页面跳转详细示例
SpringBoot與MyBatis搭建環(huán)境,底層數(shù)據(jù)庫為mysql,頁面使用JSP(官網(wǎng)上不推薦使用jsp),完成從數(shù)據(jù)庫中查詢出數(shù)據(jù),在jsp頁面中顯示,并且實現(xiàn)頁面的跳轉功能。
項目下載鏈接: https://github.com/DFX339/springbootJsp.git
(1)新建數(shù)據(jù)庫 springboot_mybatis
新建表 user,表的結構如下:
(2)新建maven項目---web項目 springbootJsp,項目目錄結構如下
ApplicationMain.java : SpringBoot項目的啟動類
UserController.java : 前端控制類
UserMapper.java :持久層的接口設計
User.java:實體類
UserIService.java:業(yè)務層接口
UserService.java: 業(yè)務層接口實現(xiàn)類
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
#數(shù)據(jù)庫的配置
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數(shù)據(jù)
需要返回json數(shù)據(jù)的時候,在方法上添加@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;/*** 前端控制器類,調用業(yè)務層方法處理請求
*@authorAdministrator
**/@Controller
@ComponentScan({"SpringBoot_jsp.springbootJsp.service"})
@MapperScan("SpringBoot_jsp.springbootJsp.mapper")public classUserController {
@ResourceprivateUserService userService;/*** 根據(jù)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:實體類 ,與數(shù)據(jù)庫中的字段一 一對應。
packageSpringBoot_jsp.springbootJsp.pojo;/*** 實體類
*@authorAdministrator
**/
public classUser {private intid;privateString username;privateString password;private intage;publicUser(){}/** setter and getter 方法 */
}
(8) UserIService.java:業(yè)務層接口
packageSpringBoot_jsp.springbootJsp.service;importSpringBoot_jsp.springbootJsp.pojo.User;/*** 業(yè)務層接口
*@authorAdministrator
**/
public interfaceUserIService {voidinsert(User user);void delete(intid);voidedit(User user);
User find(intid);
}
(9) UserService.java: 業(yè)務層接口實現(xiàn)類 ,調用mapper接口中的方法完成對數(shù)據(jù)庫的操作
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;/*** 業(yè)務層實現(xiàn)類
*@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方法返回的頁面
在頁面發(fā)送請求使用? url="/項目名/請求名.do"
例如:轉到skip.jsp
顯示用戶信息HELLO THIS IS SHOWUSER JSP.
${user.id }
${user.username }
${user.password }
轉到skip.jsp
(12)skipped.jsp: 從showUser.jsp頁面請求/skip方法跳轉到此頁面
jsp跳轉Skip jsp
轉到showUser.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
啟動成功會出現(xiàn)如圖結果:
(15)訪問項目。
在瀏覽器中通過URL訪問項目。 格式:? http://主機IP:8080/application.properties中指定的server.context-path的值/@Controller標識的類指定的RequestMapping值
例如:?http://127.0.0.1:8080/SpringbootJsp/findById
訪問結果如下:
點擊頁面中的超鏈接? 轉到skip.jsp,進入skipped.jsp頁面,結果如下:
總結
以上是生活随笔為你收集整理的mysql 页面跳转_Springboot+MyBatis+mysql+jsp页面跳转详细示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [云炬创业基础笔记]第五章创业机会评估测
- 下一篇: [云炬创业基础笔记]第五章创业机会评估测