SpringBoot访问NoSQL和简单的Thymeleaf-Spring-Spring-boot整合
SpringBoot访问NoSQL
SpringBoot访问Redis
在pom.xml添加boot-data-redis定义
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency> </dependencies>在application.properties添加redis连接参数
spring.redis.host=localhost
spring.redis.port=6379定义启动类
@SpringBootApplication
public class MyBootApplication { }测试程序
public static void main(String[] args) {
ApplicationContext ac =
SpringApplication.run(MyBootApplication.class, args);
RedisTemplate<Object,Object> redis =
ac.getBean("redisTemplate",RedisTemplate.class);
redis.opsForValue().set("name", "SpringBoot");
Object name = redis.opsForValue().get("name");
System.out.println(name);
Dept dept = new Dept();
dept.setDeptno(10);
dept.setDname("JAVA");
dept.setLoc("北京");
redis.opsForValue().set("dept", dept);
Dept dept1 = (Dept)redis.opsForValue().get("dept");
System.out.println(
dept1.getDeptno()+" "+dept1.getDname()+" "+dept1.getLoc()); }
SpringBoot访问Mongodb
在pom.xml追加boot-starter-data-mongodb定义
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency> </dependencies>在application.properties追加连接参数定义
spring.data.mongodb.uri=mongodb://localhost:27017/java20 #spring.data.mongodb.host=localhost
#spring.data.mongodb.port=27017
#spring.data.mongodb.database=java20定义主启动类
@SpringBootApplication
public class MyBootApplication { }测试程序
public static void main(String[] args) {
ApplicationContext ac =
SpringApplication.run(MyBootApplication.class, args);
MongoTemplate template =
ac.getBean("mongoTemplate",MongoTemplate.class);
List<Dept> list = template.findAll(Dept.class);
for(Dept dept:list){
System.out.println(
dept.getDeptno()+" "+dept.getDname()+" "+dept.getLoc());
}
}
SpringBoot MVC
主要封装了SpringMVC、Restful、内置tomcat等功能。
restful服务
SSM:SpringMVC、IOC、MyBatis
/dept/get GET 查询部门信息
/dept/list GET 分页查询部门信息
请求-->DispatcherServlet-->HandlerMapping-->DeptController-->DeptDao-->返回JSON结果
在pom.xml追加web、jdbc、mybatis-spring、驱动包定义
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency> <dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency> <!-- mybatis、mybatis-spring、autocofigurer -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency> <dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency> </dependencies>在application.properties追加连接参数定义
#server
server.port=8888 #datasource
spring.datasource.username=SCOTT
spring.datasource.password=TIGER
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:XE
spring.datasource.driverClassName=oracle.jdbc.OracleDriver编写实现DeptDao
- 定义实体类Dept(同上)
定义Mapper映射器+注解SQL
public interface DeptDao { @Select("select * from dept where deptno=#{no}")
public Dept findById(int no); @Select("select * from dept order by deptno")
public List<Dept> findAll(); }
编写实现DeptController
@RestController
public class DeptController { @Autowired
private DeptDao deptDao; @GetMapping("/dept/get")// dept/get?no=xx
public Dept load(int no){
Dept dept = deptDao.findById(no);
return dept;
} @GetMapping("/dept/list")// dept/list?page=xx&size=xx
public List<Dept> loadPage(
@RequestParam(required=false,defaultValue="1",name="page")int pageNum,
@RequestParam(required=false,defaultValue="5",name="size")int pageSize){
PageHelper.startPage(pageNum, pageSize);
List<Dept> list = deptDao.findAll();
return list;
} }定义启动类、追加@SpringBootApplication、@MapperScan标记
@SpringBootApplication
@MapperScan(basePackages={"cn.xdl.dao"})
public class MyBootApplication { public static void main(String[] args) {
SpringApplication.run(MyBootApplication.class, args);
} }启动Boot程序测试
JSP响应界面
/hello-->DispatcherServlet-->HandlerMapping-->HelloController-->ModelAndView-->ViewResolver-->/hello.jsp
在pom.xml追加web、jasper定义
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency> </dependencies>在application.properties追加server、viewresolver定义
#server
server.port=8888 #viewresolver
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp编写HelloController
@Controller
public class HelloController { @GetMapping("/hello")
public ModelAndView execute(){
System.out.println("进入HelloController.execute处理");
ModelAndView mav = new ModelAndView();
mav.setViewName("hello");//hello.jsp
mav.getModel().put("msg", "你好");
return mav;
} @GetMapping("/hello1")
public String execute1(ModelMap model){
System.out.println("进入HelloController.execute1处理");
model.put("msg", "Hello");
return "hello";
} }编写hello.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>SpringBoot JSP应用</h1>
<h2>${msg}</h2>
</body>
</html>编写启动类
@SpringBootApplication
public class MyBootApplication { public static void main(String[] args) {
SpringApplication.run(MyBootApplication.class, args);
} }启动测试
http://localhost:8888/hello
Thymeleaf模板响应界面
模板技术是对JSP技术的一个替代品,优点如下:
- 使用简单、方便(JSP复杂)
- 运行机制简单、效率高(JSP-->Servlet-->.class-->HTML输出)
Velocity : hello.vm + VTL
Freemarker:hello.ftl + FTL
Thymeleaf:hello.html + THTL
在pom.xml中追加boot-starter-thymeleaf定义
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.7.RELEASE</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!-- thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> </dependencies>在application.properties追加server配置
server.port=8888
定义Controller组件
@Controller
public class HelloController { @GetMapping("/hello")
public ModelAndView execute(){
ModelAndView mav = new ModelAndView();
mav.setViewName("hello");///templates/hello.html
mav.getModel().put("msg", "SpringBoot Thymeleaf");
return mav;
} }定义html模板文件,放在src/main/resources/templates目录中
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1 th:text="${msg}"></h1>
</body>
</html>定义启动类
@SpringBootApplication
public class MyBootApplication { public static void main(String[] args) {
SpringApplication.run(MyBootApplication.class, args);
} }启动程序测试
http://localhost:8888/hello
1.x版本的Boot需要取消严格的模板标记校验(开始和结束必须匹配)
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
</dependency>
在application.properties添加spring.thymeleaf.mode=LEGACYHTML5
SpringBoot访问NoSQL和简单的Thymeleaf-Spring-Spring-boot整合的更多相关文章
- 第一篇 Springboot + Web MVC + MyBatis + 简单UI + Thymeleaf实现
源码链接:https://pan.baidu.com/s/1-LtF56dnCM277v5lILRM7g 提取码:c374 第二篇 Springboot mybatis generate根据数据库表自 ...
- SpringBoot学习笔记(二):SpringBoot访问静态文件、捕获全局异常、集成Thymeleaf、集成JSP
SpringBoot访问静态文件 什么是静态文件? 不需要通过web容器去得到的文件,直接通过路径就能得到的文件,比如项目的css,js,img等文件. 所有的资源文件都应该在src/main/res ...
- Java结合SpringBoot拦截器实现简单的登录认证模块
Java结合SpringBoot拦截器实现简单的登录认证模块 之前在做项目时需要实现一个简单的登录认证的功能,就寻思着使用Spring Boot的拦截器来实现,在此记录一下我的整个实现过程,源码见文章 ...
- Spring Boot2 系列教程(九)Spring Boot 整合 Thymeleaf
虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...
- 极简 Spring Boot 整合 Thymeleaf 页面模板
虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...
- SpringBoot + Layui +Mybatis-plus实现简单后台管理系统(内置安全过滤器)
1. 简介 layui(谐音:类UI)是一款采用自身模块规范编写的前端UI框架,遵循原生HTML/CSS/JS的书写与组织形式,门槛极低,拿来即用.其外在极简,却又不失饱满的内在,体积轻盈,组件丰 ...
- spring boot整合Thymeleaf的那些坑(spring boot 学习笔记之四)
这里简单记录一下Thymeleaf配置和使用的步骤 1.修改pom文件,添加依赖 <dependency> <groupId>org.springframework.boot& ...
- springboot学习之构建简单项目搭建
概述 相信对于Java开发者而言,spring和springMvc两个框架一定不陌生,这两个框架需要我们手动配置的地方非常多,各种的xml文件,properties文件,构建一个项目还是挺复杂的,在这 ...
- 一站式SpringBoot for NoSQL Study Tutorial 开发教程学习手册
SpringBoot2.0 + NoSQL使用教程,项目名称:“SpringBoot2NoSQL” 项目地址: https://gitee.com/475660/SpringBoot2NoSQL 项目 ...
随机推荐
- laravel5的Bcrypt加密方式对系统保存密码的小结
laravel5文档介绍 //对 A 密码使用Bcrypt 加密 $password = Hash::make('secret'); //你也可直接使用 bcrypt 的 function $pass ...
- [5.19 线下活动]Docker Meetup杭州站—拥抱Kubernetes,容器深度实践
对本次线下活动感兴趣的朋友,欢迎点击此处报名,领取免费票. 今年3月,Docker刚刚过完5岁生日,五年期间,Docker也逐渐在技术和实践方面趋于成熟,更是在去年年底主动拥抱Kubernetes. ...
- linux常用命令大全(linux基础命令入门到精通+命令备忘录+面试复习+实例)
作者:蓝藻(罗蓝国度) 创建时间:2018.7.3 编辑时间:2019.4.29 前言 本文特点 授之以渔:了解命令学习方法.用途:不再死记硬背,拒绝漫无目的: 准确无误:所有命令执行通过(环境为ce ...
- Nginx + uWSGI 部署Django 项目,并实现负载均衡
一.uWSGI服务器 uWSGI是一个Web服务器,它实现了WSGI协议.uwsgi.http等协议.Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换. 要注意 WSGI ...
- C++中new申请动态数组
C++中数组分为静态数组和动态数组,静态数组必须确定数组的大小,不然编译错误:而动态数组大小可以不必固定,用多少申请多少.静态数组类于与我们去餐馆吃饭,餐馆会把菜做好.而动态数组类似于我们自己买菜做饭 ...
- robot framework —— 变量文件
何为变量文件? 变量文件中包含了测试数据中的变量.虽然变量可以通过变量表格中创建, 或者通过命令行设置, 不过这些方法有所局限, 而变量文件可以动态地创建任意类型的变量. 变量文件一般由Python模 ...
- 程序猿的日常——JVM内存模型与垃圾回收
Java开发有个很基础的问题,虽然我们平时接触的不多,但是了解它却成为Java开发的必备基础--这就是JVM.在C++中我们需要手动申请内存然后释放内存,否则就会出现对象已经不再使用内存却仍被占用的情 ...
- JVM概念总结:数据类型、堆与栈
Java虚拟机中,数据类型可以分为两类:基本类型和引用类型.基本类型的变量保存原始值,即:他代表的值就是数值本身: 引用类型的变量保存引用值,引用值代表了某个对象的引用而不是对象的本身,对象的本身存放 ...
- springMvc的执行流程(源码分析)
1.在springMvc中负责处理请求的类为DispatcherServlet,这个类与我们传统的Servlet是一样的.我们来看看它的继承图 2. 我们发现DispatcherServlet也继承了 ...
- 渐进增强与优雅降级 && css3中普通属性和前缀属性的书写顺序
什么是渐进增强与优雅降级? 服务器和浏览器是不同的.当服务器有新版本时,开发人员直接使用新版本的服务器提供服务即可:但是浏览器端,不同的用户使用的浏览器版本不同,型号差异大,我们不可能让用户强制更新 ...