SpringBoot2.0完美整合Mybatis并使用thymeleaf展示数据

第一步:创建一个springboot的maven项目(pom文件)



    4.0.0

    com.itpengwei.springboot.one
    demo
    0.0.1-SNAPSHOT
    jar
    demo
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.3.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        
        
        
            mysql
            mysql-connector-java
            runtime
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.2.5
        
        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.9
        
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    




第二步:创建包结构以及编写一个启动类

SpringBoot2.0完美整合Mybatis并使用thymeleaf展示数据_第1张图片

第三步:使用逆向工程生成mapper和实体类(或者自己创建),并将mapper.xml放在resource下面新建一个目录mapper(这个可以自定义放名字以及位置,看后面配置文件怎么配置)存放

SpringBoot2.0完美整合Mybatis并使用thymeleaf展示数据_第2张图片

第五步:编写application.properties配置文件(相信里面的配置只要会SSM的都能看懂就不多解释了)

#配置端口号以及拦截策略
server.port=8080
server.servlet.path=*.html
#开始配置mysql连接驱动以及数据库连接池参数
spring.datasource.name=mysql_test
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.filters=stat
spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://localhost:3306/crm-bos?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
spring.datasource.druid.username=root
spring.datasource.druid.password=19980311
#这里可以不用配置,有默认参数,根据自己需求
spring.datasource.druid.initial-size=1
spring.datasource.druid.min-idle=1
spring.datasource.druid.max-active=20
spring.datasource.druid.max-wait=6000
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.min-evictable-idle-time-millis=300000
spring.datasource.druid.validation-query=SELECT 'x'
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
spring.datasource.druid.pool-prepared-statements=false
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
#开始配置mybatis
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.itpengwei.springboot.one.demo.pojo
#配置thymeleaf缓存开发期间先关闭,否则影响测试
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8

yml版配置

server:
  port: 8080
spring:
  datasource:
    name: mysql_test
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      filters: stat
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/crm-bos?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
      username: root
      password: 19980311
      initial-size: 1
      min-idle: 1
      max-active: 20
      max-wait: 60000
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 300000
      validation-query: SELECT 'x'
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      pool-prepared-statements: false
      max-pool-prepared-statement-per-connection-size: 20
  thymeleaf:
    cache: false
    prefix: classpath:/templates/
    suffix: .html
    mode: HTML5
    encoding: UTF-8
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.itpengwei.pojo
pagehelper:
  helperDialect: mysql
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql
  returnPageInfo: check

第六步:编写一个Controller进行测试

package com.itpengwei.springboot.one.demo.controller;

import com.itpengwei.springboot.one.demo.dao.TCustomerMapper;
import com.itpengwei.springboot.one.demo.pojo.TCustomer;
import com.itpengwei.springboot.one.demo.pojo.TCustomerExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class HelloController {
    @Autowired
    private TCustomerMapper customerMapper;

    @RequestMapping(value = "/findAll.html")
    public String findAll(Model model) {
        TCustomerExample example = new TCustomerExample();
        List list = customerMapper.selectByExample(example);
        model.addAttribute("hello", list);
        return "hello";
    }

    @RequestMapping(value = "/findList.html")
    @ResponseBody
    public List findList() {
        TCustomerExample example = new TCustomerExample();
        List list = customerMapper.selectByExample(example);
        return list;
    }
}

第七步:创建一个html5的文件(注意需要导入thymeleaf的约束)




    Hello World!


Hello.v.2

id name station telephone address decidedzoneId

第八步:运行main方法

SpringBoot2.0完美整合Mybatis并使用thymeleaf展示数据_第3张图片

SpringBoot2.0完美整合Mybatis并使用thymeleaf展示数据_第4张图片

测试成功

你可能感兴趣的:(springboot,mybatis)