springboot+thymeleaf+mybatis 基础学习


秉承有输入就要有输出的学习态度,编写博客记录。毕竟好的记忆是需要反复练习的。

springboot + thymeleaf + myBatis 

springboot 创建项目。

可视化界面操作  :  File --New priject --> Spring Initializr --> 选择依赖(web/thymeleaf/mybatis)  创建成功

官网一键操作 : 进入 spring 官网 下的 快速创建项目  纯净版

解压出来后,用Interllij idea打开文件夹。删除几个没作用的文件,简化页面。目录结构如下

src/main/java:主程序入口 Application,可以通过直接运行该类来 启动 Spring Boot应用
src/main/resources:配置目录,该目录用来存放应用的一些配置信息,比如应用名、服务端口、数据库配置等。由于我们应用了Web模块,因此产生了 static目录与templates目录,前者用于存放静态资源,如图片、CSS、JavaScript等;后者用于存放Web页面的模板文件。
src/test:单元测试目录,生成的 ApplicationTests 通过 JUnit4实现,可以直接用运行 Spring Boot应用的测试。
application.properties/application.yml 用于存放程序的各种依赖模块的配置信息,比如 服务端口,数据库连接配置等。。。

1)代码层的结构

  根目录:com.springboot.demo

    1.工程启动类(Application.java)

    2.实体类(domain)置于com.springboot.domain

    3.数据访问层(Dao)置于com.springboot.repository

    4.数据传输类(vo)置于com.springboot.vo

    5.前端控制器(Controller)置于com.springboot.controller

    6.工具类(utils)置于com.springboot.utils

    7.配置信息类(config)置于com.springboot.config

    8.数据服务层(Service)置于com,springboot.service,数据服务的实现接口(serviceImpl)至于com.springboot.service.impl

(2)资源文件的结构

  根目录:src/main/resources

    1.配置文件(.properties/.xml等) 置于 根目录文件夹下

    2.html 页面文件存放在templates 文件夹下

    3.页面以及js/css/image等置于static文件夹下的各自文件下

    4.Mapper 映射文件,自定义sql 语句服务的实现接口(mapperImpl)至于impl 。

其中的 application.properties 配置文件中: 

server.port= 8080

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.url=jdbc:mysql://0.0.0.0:3306/book_test?useUnicode=true&characterEncoding=utf-8
spring.datasource.druid.username=root
spring.datasource.druid.password=123456
spring.datasource.druid.max-active=20
spring.datasource.druid.max-wait=10000
spring.datasource.druid.initial-size=8

# 上传文件总的最大值
spring.servlet.multipart.max-request-size=10MB
# 单个文件的最大值
spring.servlet.multipart.max-file-size=10MB

## jsp
#spring.mvc.view.prefix=/WEB-INF/jsp/
#spring.mvc.view.suffix=.jsp

# thymeleaf 模板配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html

#热部署文件,页面不产生缓存,及时更新
spring.thymeleaf.cache=false
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**

# mybatis 标注SQL映射文件位置
mybatis.mapper-locations=classpath:mapper/**/*.xml
mybatis.type-aliases-package=com.example.demo.entity
#mybatis.config-location=classpath:mybatis.xml
mybatis.config-location=classpath:mybatis-config.xml

Mybatis 生成 

generatorConfig.xml  配置映射文件的路径地址+映射数据库实体名(图中是逆向生成的插件,在文件中配置好映射的文件位置,点击生成)

log4j2.xml (日志配置文件),mybatus-config.xml (mybatis 分页)  

配置文件 pom.xml 的配置

<?xml version="1.0" encoding="UTF-8"?>
"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.7.RELEASE
         
    

    com.example
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
        
        
        
        
        
        
        
        

        
            
            
            
            
                
                    
                    
                
            
        

        
            org.springframework.boot
            spring-boot-starter-thymeleaf
            2.1.4.RELEASE
        


        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.1
        

        
            mysql
            mysql-connector-java
            
        

        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.10
        
        

        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.2.12
        

    

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

            
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.2
                
                    
                    
                        ${project.basedir}/src/main/resources/generatorConfig.xml
                    
                    
                    true
                    
                    true
                
                
                    
                    
                        mysql
                        mysql-connector-java
                        5.1.30
                    
                
            
        

    

在pom.xml 中的 阿里云中的 druid 数据库连接池 依赖 ,还有mybatis 逆向生成插件依赖 需要多注意。

spring-boot-starter-parent 父依赖 自带适配的插件依赖版本,不需要额外的配置插件版本。查看方法,按住Ctrl +鼠标点击 

 

实例地址传送门