博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot访问NoSQL和简单的Thymeleaf-Spring-Spring-boot整合
阅读量:4357 次
发布时间:2019-06-07

本文共 8545 字,大约阅读时间需要 28 分钟。

SpringBoot访问NoSQL

SpringBoot访问Redis

  1. 在pom.xml添加boot-data-redis定义

    org.springframework.boot
    spring-boot-starter-parent
    2.0.1.RELEASE
    UTF-8
    UTF-8
    1.8
    org.springframework.boot
    spring-boot-starter-data-redis

     

  2. 在application.properties添加redis连接参数

    spring.redis.host=localhostspring.redis.port=6379

     

  3. 定义启动类

    @SpringBootApplicationpublic class MyBootApplication {}

     

  4. 测试程序

    public static void main(String[] args) {    ApplicationContext ac =         SpringApplication.run(MyBootApplication.class, args);    RedisTemplate
    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定义

    org.springframework.boot
    spring-boot-starter-parent
    2.0.1.RELEASE
    UTF-8
    UTF-8
    1.8
    org.springframework.boot
    spring-boot-starter-data-mongodb

     

  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. 定义主启动类

    @SpringBootApplicationpublic class MyBootApplication {}

     

  4. 测试程序

    public static void main(String[] args) {    ApplicationContext ac =         SpringApplication.run(MyBootApplication.class, args);    MongoTemplate template =         ac.getBean("mongoTemplate",MongoTemplate.class);    List
    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、驱动包定义

    org.springframework.boot
    spring-boot-starter-parent
    2.0.1.RELEASE
    UTF-8
    UTF-8
    1.8
    org.springframework.boot
    spring-boot-starter-jdbc
    com.oracle
    ojdbc6
    11.2.0.3
    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    1.3.0
    com.github.pagehelper
    pagehelper-spring-boot-starter
    1.2.3

     

  2. 在application.properties追加连接参数定义

    #serverserver.port=8888#datasourcespring.datasource.username=SCOTTspring.datasource.password=TIGERspring.datasource.url=jdbc:oracle:thin:@localhost:1521:XEspring.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
      findAll();}

       

  4. 编写实现DeptController

    @RestControllerpublic 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
    loadPage( @RequestParam(required=false,defaultValue="1",name="page")int pageNum, @RequestParam(required=false,defaultValue="5",name="size")int pageSize){ PageHelper.startPage(pageNum, pageSize); List
    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定义

    org.springframework.boot
    spring-boot-starter-parent
    2.0.1.RELEASE
    UTF-8
    UTF-8
    1.8
    org.springframework.boot
    spring-boot-starter-web
    org.apache.tomcat.embed
    tomcat-embed-jasper

     

  2. 在application.properties追加server、viewresolver定义

    #serverserver.port=8888#viewresolverspring.mvc.view.prefix=/spring.mvc.view.suffix=.jsp

     

  3. 编写HelloController

    @Controllerpublic 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

            
    Insert title here

    SpringBoot JSP应用

    ${msg}

     

  5. 编写启动类

    @SpringBootApplicationpublic 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定义

    org.springframework.boot
    spring-boot-starter-parent
    1.4.7.RELEASE
    UTF-8
    UTF-8
    1.8
    org.springframework.boot
    spring-boot-starter-web
    org.springframework.boot
    spring-boot-starter-thymeleaf

     

  2. 在application.properties追加server配置

    server.port=8888
  3. 定义Controller组件

    @Controllerpublic 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目录中

        
    Insert title here

     

  5. 定义启动类

    @SpringBootApplicationpublic class MyBootApplication {    public static void main(String[] args) {        SpringApplication.run(MyBootApplication.class, args);    }}

     

  6. 启动程序测试

    http://localhost:8888/hello
  7. 1.x版本的Boot需要取消严格的模板标记校验(开始和结束必须匹配)

    net.sourceforge.nekohtml
    nekohtml

    在application.properties添加spring.thymeleaf.mode=LEGACYHTML5

转载于:https://www.cnblogs.com/hx1098/p/9404374.html

你可能感兴趣的文章
登录之后更新导航
查看>>
spring 的单例模式
查看>>
Python学习手册
查看>>
完整的系统帮助类Utils
查看>>
Python 的语言特性
查看>>
使用PowerShell批量注册DLL到GAC
查看>>
微软职位内部推荐-Senior Development Engineer
查看>>
创建数据库的方法
查看>>
递归算法
查看>>
关于java中sendRedirect,forward和include区别
查看>>
在红帽RHEL7.0里配置网卡的四种方法
查看>>
LeetCode--二分查找相关算法
查看>>
RobotFramework自动化测试框架系统关键字之断言
查看>>
《Node.js In Action》笔记之流程控制
查看>>
通俗易懂云计算
查看>>
个人笔记 - Word2013 论文格式调整
查看>>
Python学习---Python下[字典]的学习
查看>>
Python学习---重点模块之xml
查看>>
Python学习---django之admin简介
查看>>
个人工作总结11(第二阶段)
查看>>