java整合html_springBoot整合mybatis、jsp 或 HTML

springBoot整合mybatis、jsp

Spring Boot的主要优点:

1:  为所有Spring开发者更快的入门;

2:开箱即用,提供各种默认配置来简化项目配置;

3:  内嵌式容器简化Web项目;

4:  没有冗余代码生成和XML配置的要求

本项目使用到的工具:

开发工具:Intellij IDEA 2018.1.4

springboot:2.0.1.RELEASE

jdk:1.8.0_40

maven:3.3.9

开始搭建:

项目创建

2478c69afce1f34a0432052757ae611a.png

e9e695d955d8c9f270c4c36102313e2f.png

b261a85b3ec1eb8ae0a0cb65b2b25c0e.png

65b2fdc49c380e753b14f1e4f028a8d4.png

finish即可。

建好后的 项目结构:

5f9ac520dba75288c9286512d3bc4e9b.png

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层,写一个简单的类访问一下:

8d05561ea1d4bb105c9b7d91f11f3a02.png

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的启动类。

注意:因为我们前面添加了数据库相关的依赖,但是我们还没有具体配置,如果直接运行的话会报错:

***************************

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。启动以后就可以正常运行。

1afae31ee4e7a5ebc14a51e822ef88db.png

好的,启动没问题,我们下面正式进入mybatis与jsp的整合:

建model层:

8da59e71d7efd5181386555b50950b4e.png

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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的形式配置,可以见另外一篇博客:

6b71500421c36ab4cb428e9f17385ec3.png

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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层:

509656496adc3c6216a021a6a501c189.png

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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:控制层访问:

6f35756d8a99f4a0d786b187e88e7cc8.png

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

数据库连接配置文件:

e083fb48cf19ae283de6a67343bcf788.png

1 spring.mvc.view.prefix=/WEB-INF/jsp/

2 spring.mvc.view.suffix=.jsp3 #jdbc相关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:

注意:前面我们排除了对数据库相关的自动配置,现在我们配置了数据库实体配置,所以把之前的排除要删掉,并且多添加@MapperScan("mapper映射文件的地址")

97d0ea62f8b82dfd33c70f38878d6c1b.png

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

好的直接启动即可。上面我们简单实现了数据库的查询,增删改就自己取写一写吧。

下面我们看看springBoot整合jsp:

1、在原来的依赖中添加依赖:

org.apache.tomcat.embed

tomcat-embed-jasper

2、跳转页面:

6f99a7e58a5248ea50cdad3f35c1108b.png

springBoot整合thymeleaf:

org.springframework.boot

spring-boot-starter-thymeleaf

跳转页面:

不用加前后缀,可以直接跳转页面:

3a8e8ce170e66fd8c08acc82996fdab7.png

关于thymeleaf的基础使用参考:

在我们第一次新建的HelloSpringBootController中 新建一个helloJsp()方法,注意:我们之前用的注解是@RestController ,这个注解相当于@Controller  +  @ResponseBody

表示标注的类或则方法返回的都是json格式的,而我们这次需要访问jsp页面所以需要换成@Controller注解。在需要的返回json格式的方法上添加@ResponseBody,而不是整个类的所有方法都返回json格式。

d532a26fdb28bf8c7db7bf71abfe64ab.png

下面我们看一下springBoot中的全局异常捕获:

异常捕获的核心标签:@ControllerAdvice   +   @ExceptionHandler(RuntimeException.class)

6f5739c7115136682125941c1ed2e175.png

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

好了,就先到这儿吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值