SpringBoot访问NoSQL

SpringBoot访问Redis

  1. 在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>
  2. 在application.properties添加redis连接参数

    spring.redis.host=localhost
    spring.redis.port=6379
  3. 定义启动类

    @SpringBootApplication
    public class MyBootApplication { }
  4. 测试程序

    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

  1. 在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>
  2. 在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
  3. 定义主启动类

    @SpringBootApplication
    public class MyBootApplication { }
  4. 测试程序

    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结果

  1. 在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>
  2. 在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
  3. 编写实现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(); }
  4. 编写实现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;
    } }
  5. 定义启动类、追加@SpringBootApplication、@MapperScan标记

    @SpringBootApplication
    @MapperScan(basePackages={"cn.xdl.dao"})
    public class MyBootApplication { public static void main(String[] args) {
    SpringApplication.run(MyBootApplication.class, args);
    } }
  6. 启动Boot程序测试

JSP响应界面

/hello-->DispatcherServlet-->HandlerMapping-->HelloController-->ModelAndView-->ViewResolver-->/hello.jsp

  1. 在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>
  2. 在application.properties追加server、viewresolver定义

    #server
    server.port=8888 #viewresolver
    spring.mvc.view.prefix=/
    spring.mvc.view.suffix=.jsp
  3. 编写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";
    } }
  4. 编写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>
  5. 编写启动类

    @SpringBootApplication
    public class MyBootApplication { public static void main(String[] args) {
    SpringApplication.run(MyBootApplication.class, args);
    } }
  6. 启动测试

    http://localhost:8888/hello

Thymeleaf模板响应界面

模板技术是对JSP技术的一个替代品,优点如下:

  • 使用简单、方便(JSP复杂)
  • 运行机制简单、效率高(JSP-->Servlet-->.class-->HTML输出)

Velocity : hello.vm + VTL

Freemarker:hello.ftl + FTL

Thymeleaf:hello.html + THTL

  1. 在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>
  2. 在application.properties追加server配置

    server.port=8888
  3. 定义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;
    } }
  4. 定义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>
  5. 定义启动类

    @SpringBootApplication
    public class MyBootApplication { public static void main(String[] args) {
    SpringApplication.run(MyBootApplication.class, args);
    } }
  6. 启动程序测试

    http://localhost:8888/hello
  7. 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整合的更多相关文章

  1. 第一篇 Springboot + Web MVC + MyBatis + 简单UI + Thymeleaf实现

    源码链接:https://pan.baidu.com/s/1-LtF56dnCM277v5lILRM7g 提取码:c374 第二篇 Springboot mybatis generate根据数据库表自 ...

  2. SpringBoot学习笔记(二):SpringBoot访问静态文件、捕获全局异常、集成Thymeleaf、集成JSP

    SpringBoot访问静态文件 什么是静态文件? 不需要通过web容器去得到的文件,直接通过路径就能得到的文件,比如项目的css,js,img等文件. 所有的资源文件都应该在src/main/res ...

  3. Java结合SpringBoot拦截器实现简单的登录认证模块

    Java结合SpringBoot拦截器实现简单的登录认证模块 之前在做项目时需要实现一个简单的登录认证的功能,就寻思着使用Spring Boot的拦截器来实现,在此记录一下我的整个实现过程,源码见文章 ...

  4. Spring Boot2 系列教程(九)Spring Boot 整合 Thymeleaf

    虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...

  5. 极简 Spring Boot 整合 Thymeleaf 页面模板

    虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...

  6. SpringBoot + Layui +Mybatis-plus实现简单后台管理系统(内置安全过滤器)

    1. 简介   layui(谐音:类UI)是一款采用自身模块规范编写的前端UI框架,遵循原生HTML/CSS/JS的书写与组织形式,门槛极低,拿来即用.其外在极简,却又不失饱满的内在,体积轻盈,组件丰 ...

  7. spring boot整合Thymeleaf的那些坑(spring boot 学习笔记之四)

    这里简单记录一下Thymeleaf配置和使用的步骤 1.修改pom文件,添加依赖 <dependency> <groupId>org.springframework.boot& ...

  8. springboot学习之构建简单项目搭建

    概述 相信对于Java开发者而言,spring和springMvc两个框架一定不陌生,这两个框架需要我们手动配置的地方非常多,各种的xml文件,properties文件,构建一个项目还是挺复杂的,在这 ...

  9. 一站式SpringBoot for NoSQL Study Tutorial 开发教程学习手册

    SpringBoot2.0 + NoSQL使用教程,项目名称:“SpringBoot2NoSQL” 项目地址: https://gitee.com/475660/SpringBoot2NoSQL 项目 ...

随机推荐

  1. 《JavaScript高级程序设计》5.5 Function类型

    5.5 Function类型 函数实质上是对象, 每个函数都是Function类型的实例, 并且都和其他引用类型一样具有属性和方法. 因此函数名实际上也是一个指向函数对象的指针, 不会与某个函数绑定. ...

  2. Java基础学习篇---------static

    一.static的使用 1.使用static定义的属性往往通过类名直接调用,它的属性(方法)不属于某一个的对象的.所以对象没有创建之前就可以对static的属性的调用,方法亦如此. 2.static ...

  3. jzoj5906

    我們首先發現,每一條邊都至少走1次,因為我們必須走到每一個節點按按鈕 如果我們不走一個節點,說明這個節點已經有傳送門了,但是必須走到這個節點開傳送門,矛盾 然後我們發現,每一條邊至多經過2次 如果我們 ...

  4. sql语句_2

    数据表如下 一个user_id对应多个user_name,现在要求是:如果某个用户对应的user_name中存在一个a,打印user_id,a出来:如果不存在,打印user_id,0.打印时候user ...

  5. Storm的并行度

    在Storm集群中,运行Topolopy的实体有三个:工作进程,executor(线程),task(任务),下图可以形象的说明他们之间的关系. 工作进程 Storm集群中的一台机器会为一个或则多个To ...

  6. RSA的JAVA实现 及javax.crypto.IllegalBlockSizeException

    一.背景 最近工作中涉及到RSA加密的相关需求任务,之前对加密算法了解不多,开发过程中遇到了一些坑记录一下. 二.RSA原理 RSA加密是非对称加密,公开私钥,保留私钥.通信时数据通过公开的公钥加密, ...

  7. linux查看防火墙状态及开启关闭命令

    存在以下两种方式: 一.service方式 查看防火墙状态: [root@centos6 ~]# service iptables status iptables:未运行防火墙. 开启防火墙: [ro ...

  8. 2019年北航OO第一次博客总结

    一.基于度量对程序结构的分析 1. 第一次作业 1.1 基于类的分析的度量 首先,基于类的属性个数,方法个数,每个方法的规模,每个方法的控制分支数目,类总代码规模等特征对本次作业的结构进行分析. 1. ...

  9. h5在线1v1客服|web在线客服系统|h5即时聊天

    网上有很多环信.美恰之类的客服系统,最近也使用h5+css3+fontJs+swiper+wcPop等技术架构开发了一个在线客服(1v1沟通聊天),可以应用到在线临时聊天.在线咨询等情景.实现了消息. ...

  10. 配置文件备份方案(expect+shell)

    需求描述:备份所有线上服务器squid.httpd.mysql.nginx的配置文件 环境:在公司内网采用expect+shell脚本模式,进行批量备份.expect脚本通过ssh登录服务器,从本地c ...