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
开始搭建:
项目创建
finish即可。
建好后的 项目结构:
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的启动类。
注意:因为我们前面添加了数据库相关的依赖,但是我们还没有具体配置,如果直接运行的话会报错:
***************************
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。启动以后就可以正常运行。
好的,启动没问题,我们下面正式进入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
数据库连接配置文件:
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映射文件的地址")
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、跳转页面:
springBoot整合thymeleaf:
org.springframework.boot
spring-boot-starter-thymeleaf
跳转页面:
不用加前后缀,可以直接跳转页面:
关于thymeleaf的基础使用参考:
在我们第一次新建的HelloSpringBootController中 新建一个helloJsp()方法,注意:我们之前用的注解是@RestController ,这个注解相当于@Controller + @ResponseBody
表示标注的类或则方法返回的都是json格式的,而我们这次需要访问jsp页面所以需要换成@Controller注解。在需要的返回json格式的方法上添加@ResponseBody,而不是整个类的所有方法都返回json格式。
下面我们看一下springBoot中的全局异常捕获:
异常捕获的核心标签:@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
好了,就先到这儿吧!
java整合html_springBoot整合mybatis、jsp 或 HTML相关推荐
- SSM:Spring整合MyBatis框架时出现 java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManaged
QUESTION:出现异常:java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction ...
- Java SSM6——SSM整合
Java SSM6--SSM整合 1.Mybatis 1.1.数据库准备 CREATE TABLE person(id INT PRIMARY KEY,NAME VARCHAR(10),PASSWOR ...
- 震撼,java面试题整合(良心制作)11万多字拿去。持续更新【可以收藏】
一.javaSE面试题整合 Java部分 JDK中哪些类是不能继承的?[信雅达面试题] [参考答案] 不能继承的是类是那些用final关键字修饰的类.一般比较基本的类型或防止扩展类无意间破坏原来方法的 ...
- 使用IDEA整合SpringMVC和Mybatis(SSM框架)(二)
上一篇已经搭建了一个基础的springMVC项目,现在加入mybatis的相关配置. 写代码之前先在数据库中建一张表,如下sql: DROP TABLE IF EXISTS `user_info`; ...
- SpringBoot实战:整合Redis、mybatis,封装RedisUtils工具类等(附源码)
创建SpringBoot项目 在线创建方式 网址:https://start.spring.io/ 然后创建Controller.Mapper.Service包 SpringBoot整合Redis 引 ...
- Spring Boot实战:整合Redis、MyBatis,封装RedisUtils工具类
创建 Spring Boot 项目 在线创建方式 网址:https://start.spring.io/ 然后创建Controller.Mapper.Service包 SpringBoot整合Redi ...
- activiti+5.21+mysql_ydl-workflow基于SAAS服务,完美整合springboot + activiti5 + MyBatis
workflow V1.0 ydl-workflow基于SAAS服务,完美整合springboot + activiti5 + MyBatis 通用 Mapper + 分页插件 PageInfo! 说 ...
- SpringBoot实战:整合Redis、mybatis,封装RedisUtils工具类等
创建SpringBoot项目 在线创建方式 网址:https://start.spring.io/ 然后创建Controller.Mapper.Service包 SpringBoot整合Redis 引 ...
- app登录界面背景 css_计算机毕业设计中Java web实现简登录页面(MyBatis+jsp+servlet+html+css+javascript)...
点击上方"蓝字",关注我们. 本文利用MyBatis+jsp+servlet+html+css+javascript实现了一个简单的登录页面.对用户输入的用户名和密码就行校验,校验 ...
最新文章
- Photoshop CS5
- python中的控制流
- Visual Studio 2017开发linux程序使用实例及原理简析
- LambdaNetworks解读
- JBPM4.4总结-嵌入自己的用户体系(集成自定义用户表)
- WPF Splash Screen 和启动速度相关资料
- RocketMQ Message hasn‘t been sent. Caused by No route info of this topic, Pr
- python和java哪个好-Python和Java对比,全面解读哪个语言最赚钱,前景最好?
- 地域微信平台自媒体,原创视频如何插入腾讯地图
- 每日一练20210706
- 这篇文章告诉你时光穿梭机特效从年轻变老制作软件
- 如何完成上传图片到腾讯云
- Python模块查找路径
- Android 架构之长连接技术
- Zend Optimizer
- 牛牛手中有三根木棍,长度分别是a,b,c。牛牛可以把任一一根木棍长度削短,牛牛的目标是让这三根木棍构成一个三角形,并且牛牛还希望这个三角形的周长越大越好。
- 设置chrome浏览器的开发调试工具主题为dark模式
- MFC视图窗口(CView子类)初始化消息的调用顺序
- python读取文件需要的异常处理_Python基础:文件的简单读取和操作以及异常处理...
- 变异系数法之python
热门文章
- c语言中的素数定理,素数定理
- css 大于号 标签_web前端教程之怎样学好css?
- As Simple as One and Two(思维)
- python plt画半对数坐标_特征工程大传:对数变换
- PAT_B_1026_Java(15分)
- 【计算机组成原理】定点运算器的基本结构
- PAT_B_1002_Java(20分)
- 树莓派实时(30fps)手势识别,从数据集采集开始,全部流程开源
- [机器学习] 分类 --- Support Vector Machine (SVM)
- Pedestrian Identification (1) ——前景目标检测