1. 程式人生 > >SpringBoot訪問NoSQL和簡單的Thymeleaf-Spring-Spring-boot整合

SpringBoot訪問NoSQL和簡單的Thymeleaf-Spring-Spring-boot整合

spring context 優點 dna jdbc sys hand local tid

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整合