java整合html_springBoot整合mybatis、jsp 或 HTML
springBoot整合mybatis、jsp
Spring Boot的主要優(yōu)點:
1:? 為所有Spring開發(fā)者更快的入門;
2:開箱即用,提供各種默認(rèn)配置來簡化項目配置;
3:? 內(nèi)嵌式容器簡化Web項目;
4:? 沒有冗余代碼生成和XML配置的要求
本項目使用到的工具:
開發(fā)工具:Intellij IDEA 2018.1.4
springboot:2.0.1.RELEASE
jdk:1.8.0_40
maven:3.3.9
開始搭建:
項目創(chuàng)建
finish即可。
建好后的 項目結(jié)構(gòu):
pom.xml:
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 4.0.0
5
6 com.dengwei
7 springdemo
8 0.0.1-SNAPSHOT
9 jar
10
11 springdemo
12 Demo project for Spring Boot
13
14
15 org.springframework.boot
16 spring-boot-starter-parent
17 2.0.1.RELEASE
18
19
20
21
22 UTF-8
23 UTF-8
24 1.8
25
26
27
28
29 org.springframework.boot
30 spring-boot-starter-jdbc
31
32
33 org.springframework.boot
34 spring-boot-starter-web
35
36
37 org.mybatis.spring.boot
38 mybatis-spring-boot-starter
39 1.3.2
40
41
42
43 mysql
44 mysql-connector-java
45 runtime
46
47
48 org.springframework.boot
49 spring-boot-starter-test
50 test
51
52
53
54
55 org.springframework.boot
56 spring-boot-starter-tomcat
57
58
59 org.apache.tomcat.embed
60 tomcat-embed-jasper
61
62
63
64
65
66
67 org.springframework.boot
68 spring-boot-maven-plugin
69
70
71
72
73
74
我們先建一個controller層,寫一個簡單的類訪問一下:
HelloSpringBootController:
1 packagecom.dengwei.springdemo.controller;2
3 importorg.springframework.web.bind.annotation.RequestMapping;4 importorg.springframework.web.bind.annotation.RestController;5
6 importjava.util.HashMap;7 importjava.util.Map;8
9 @RestController10 public classHelloSpringBootController {11 @RequestMapping("/index")12 publicString hello(){13 return "hello springBoot";14 }15
16 @RequestMapping("/hello")17 public MapgetMap(){18 HashMap map = new HashMap();19 map.put("key1","姓名");20 map.put("key2","年齡");21 map.put("key3","性別");22 returnmap;23 }24
25 }
下面我們啟動一下:
每一個springBoot項目中都有一個XXXAplication類,這個類就是springBoot的啟動類。
注意:因為我們前面添加了數(shù)據(jù)庫相關(guān)的依賴,但是我們還沒有具體配置,如果直接運行的話會報錯:
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
解決:
在@EnableAutoConfiguretion中添加? ?exclude= {DataSourceAutoConfiguration.class},排除此類的autoconfig。啟動以后就可以正常運行。
好的,啟動沒問題,我們下面正式進(jìn)入mybatis與jsp的整合:
建model層:
1 packagecom.dengwei.springdemo.model;2
3 public classUser {4 privateInteger id;5 privateString userName;6 privateString password;7
8 publicInteger getId() {9 returnid;10 }11
12 public voidsetId(Integer id) {13 this.id =id;14 }15
16 publicString getUserName() {17 returnuserName;18 }19
20 public voidsetUserName(String userName) {21 this.userName =userName;22 }23
24 publicString getPassword() {25 returnpassword;26 }27
28 public voidsetPassword(String password) {29 this.password =password;30 }31
32 @Override33 publicString toString() {34 return "User{" +
35 "id=" + id +
36 ", userName='" + userName + '\'' +
37 ", password='" + password + '\'' +
38 '}';39 }40 }
View Code
2:建mapper層:
注意:我們這里的sql語句是通過注解的形式和接口寫在一起的,也可以通過xml的形式配置,可以見另外一篇博客:
1 packagecom.dengwei.springdemo.mapper;2
3
4 importcom.dengwei.springdemo.model.User;5 importorg.apache.ibatis.annotations.Param;6 importorg.apache.ibatis.annotations.Select;7
8
9 public interfaceIUserMapper {10
11 @Select("SELECT id,user_name userName, pass_word password FROM user WHERE id = #{id}")12 User queryById(@Param("id") Integer id);13 }
View Code
3:建Service層:
1 packagecom.dengwei.springdemo.Service;2
3
4 importcom.dengwei.springdemo.mapper.IUserMapper;5 importcom.dengwei.springdemo.model.User;6 importorg.springframework.beans.factory.annotation.Autowired;7 importorg.springframework.stereotype.Service;8
9 @Service10 public classUserService {11 @Autowired12 privateIUserMapper userMapper;13 publicUser queryUser(Integer id){14 returnuserMapper.queryById(id);15 }16 }
View Code
4:控制層訪問:
1 packagecom.dengwei.springdemo.controller;2
3
4 importcom.dengwei.springdemo.Service.UserService;5 importcom.dengwei.springdemo.model.User;6 importorg.springframework.beans.factory.annotation.Autowired;7 importorg.springframework.stereotype.Controller;8 importorg.springframework.web.bind.annotation.RequestMapping;9 importorg.springframework.web.bind.annotation.ResponseBody;10
11 @Controller12 @RequestMapping("/controller")13 public classUserController {14 @Autowired15 privateUserService userService;16
17 @RequestMapping("/user")18 @ResponseBody19 publicUser getUser(Integer id){20 User user =userService.queryUser(id);21 returnuser;22 }23
24 }
View Code
數(shù)據(jù)庫連接配置文件:
1 spring.mvc.view.prefix=/WEB-INF/jsp/
2 spring.mvc.view.suffix=.jsp3 #jdbc相關(guān)4 spring.datasource.url=jdbc:mysql://localhost:3306/floor_shop
5 spring.datasource.username=root6 spring.datasource.password=admin7 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
啟動springBoot:
注意:前面我們排除了對數(shù)據(jù)庫相關(guān)的自動配置,現(xiàn)在我們配置了數(shù)據(jù)庫實體配置,所以把之前的排除要刪掉,并且多添加@MapperScan("mapper映射文件的地址")
1 packagecom.dengwei.springdemo;2
3 importorg.mybatis.spring.annotation.MapperScan;4 importorg.springframework.boot.SpringApplication;5 importorg.springframework.boot.autoconfigure.EnableAutoConfiguration;6 importorg.springframework.boot.autoconfigure.SpringBootApplication;7 importorg.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;8
9 @SpringBootApplication10 @EnableAutoConfiguration11 @MapperScan("com.dengwei.springdemo.mapper")12 public classSpringdemoApplication {13
14 public static voidmain(String[] args) {15 SpringApplication.run(SpringdemoApplication.class, args);16 }17 }
View Code
好的直接啟動即可。上面我們簡單實現(xiàn)了數(shù)據(jù)庫的查詢,增刪改就自己取寫一寫吧。
下面我們看看springBoot整合jsp:
1、在原來的依賴中添加依賴:
org.apache.tomcat.embed
tomcat-embed-jasper
2、跳轉(zhuǎn)頁面:
springBoot整合thymeleaf:
org.springframework.boot
spring-boot-starter-thymeleaf
跳轉(zhuǎn)頁面:
不用加前后綴,可以直接跳轉(zhuǎn)頁面:
關(guān)于thymeleaf的基礎(chǔ)使用參考:
在我們第一次新建的HelloSpringBootController中 新建一個helloJsp()方法,注意:我們之前用的注解是@RestController ,這個注解相當(dāng)于@Controller? +? @ResponseBody
表示標(biāo)注的類或則方法返回的都是json格式的,而我們這次需要訪問jsp頁面所以需要換成@Controller注解。在需要的返回json格式的方法上添加@ResponseBody,而不是整個類的所有方法都返回json格式。
下面我們看一下springBoot中的全局異常捕獲:
異常捕獲的核心標(biāo)簽:@ControllerAdvice? ?+? ?@ExceptionHandler(RuntimeException.class)
1 packagecom.dengwei.springdemo.controller;2
3 importorg.springframework.web.bind.annotation.ControllerAdvice;4 importorg.springframework.web.bind.annotation.ExceptionHandler;5 importorg.springframework.web.bind.annotation.RequestMapping;6
7 @ControllerAdvice8 public classGlobalExceptionHandler {9 @ExceptionHandler(RuntimeException.class)10 @RequestMapping11 publicString errorPage(){12 return "index";13 }14 }
View Code
好了,就先到這兒吧!
總結(jié)
以上是生活随笔為你收集整理的java整合html_springBoot整合mybatis、jsp 或 HTML的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java有 号_JAVA揭竿而起总要有名
- 下一篇: 时区缩写 UTC, CST, GMT,