SpringBoot基本使用 框架整合 mybatis mybatis-plus 模板引擎Thymeleaf

1.SpringBoot

1.1 SpringBoot概述

1.1.1 什么是SpringBoot

谈到SpringBoot,首先谈一下Spring。Spring框架为Java应用程序开发提供全面基础架构支持的框架,它简化了大量的重复代码的操作,比如数据库连接,让我们的开发时间大大缩短。它的主要模块如下 :

    Spring JDBC:简化JDBC编码,使代码更加健壮
    Spring MVC:提供对Web程序的支持
    Spring Security:提供对安全访问控制的支持  shiro
    Spring AOP:基础IOC/DI,提供面向切面编程的实现
    Spring ORM:提供对当前市面上流行的ORM框架的支持
    Spring Test:提供对单元测试、集成测试等的支持

随着动态语言的流行(Ruby、Node.js),java开发显得格外笨重,繁多的配置、低下的开发效率、复杂的部署流程以及第三方技术集成的难度大。

在上述情况下,SpringBoot应运而生,它使用“习惯优于配置”,使我们不用或很少的配置。

1.1.2 SpringBoot核心功能

1)内嵌的Servlet容器

SpringBoot可以内嵌Tomcat、Jetty等,这样我们无需以war形式部署项目

2)提供starer简化Maven配置

Spring提供了一系列的starer pom 来简化Maven的依赖加载,常用starer依赖:

    spring-boot-starter-data-jpa
    spring-boot-starter-security
    spring-boot-starter-test
    spring-boot-starter-web
    spring-boot-starter-thymeleaf

1.1.3 SpringBoot项目快速搭建

1)网站搭建(https://start.spring.io/)

浏览器访问https://start.spring.io/,填写Group、artifact、dependencies,点击创建按钮。

2)使用Spring Tool Suite(STS)

即eclipse工具安装SpringBoot插件,创建项目

3)使用Intellij IDEA工具创建项目

第二和第三种创建方式,本质还是访问https://start.spring.io/创建项目,所以需要有网络

1.1.4 SpringBoot基本配置

1)定制Banner

即修改SpringBoot默认启动图案

1)在src/main/resources下创建一个banner.txt
2)可以通过http://patorjk.com/software/taag/网站生成指定字符串
	                        _ooOoo_  
                           o8888888o  
                           88" . "88  
                           (| -_- |)  
                            O\ = /O  
                        ____/`---'\____  
                     .   ' \\| |// `.  
                       / \\||| : |||// \  
                     / _||||| -:- |||||- \  
                       | | \\\ - /// | |  
                     | \_| ''\---/'' | |  
                      \ .-\__ `-` ___/-. /  
                    ___`. .' /--.--\ `. . __  
                 ."" '< `.___\_<|>_/___.' >'"".  
                | | : `- \`.;`\ _ /`;.`/ - ` : | |  
                  \ \ `-. \_ __\ /__ _/ .-` / /  
          ======`-.____`-.___\_____/___.-`____.-'======  
                            `=---='  
  
         .............................................  
                  佛祖保佑             永无BUG 
          佛曰:  
                  写字楼里写字间,写字间里程序员;  
                  程序人员写程序,又拿程序换酒钱。  
                  酒醒只在网上坐,酒醉还来网下眠;  
                  酒醉酒醒日复日,网上网下年复年。  
                  但愿老死电脑间,不愿鞠躬老板前;  
                  奔驰宝马贵者趣,公交自行程序员。  
                  别人笑我忒疯癫,我笑自己命太贱;  
                  不见满街漂亮妹,哪个归得程序员?

2)SpringBoot的配置文件

全局配置文件:application.properties或application.yml

常用简单配置:
    server.port= 8080
    server.context-path= /helloboot

    自定义属性配置:
    属性定义
        book.author= xm
        book.name= SpringBoot
    属性使用
        @Value(“${book.author}”)
        String bookAuthor;
        @Value(“${book.name}”)
        String bookName;

1.1.5 SpringBoot常用注解

类注解

@RestController,等同于@Controller + @responseBody

url注解

@RequestMapping

参数注解

@GetMapping 组合注解(get请求方式的RequestMapping)

@PostMapping 组合注解(post请求方式的RequestMapping)

@PathVariable 获取url中的数据

@RequestParam 获取参数中的数据

1.1.6 常见问题

IDEA不能创建class

第一种方案:

1)找到Idea安装目录: 如 : C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.6\bin
2)修改idea.exe.vmoptions 或 idea64.exe.vmoptions
3)加入如下配置保存后重启Idea即可:
-Djdk.util.zip.ensureTrailingSlash=false

第二种方案:

file -> setting -> Editor->File and Code Templates -> Class,添加

#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
#parse("File Header.java")
public class ${NAME} {  

}

如果是接口,把class改为interface

IDEA不能创建HTML

file -> setting -> Editor->File and Code Templates->HTML File,添加
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>#[[$Title$]]#</title>
</head>
<body>
#[[$END$]]#
</body>
</html>
​

autowire注解红线问题 

问题:
    Could not autowire
解决方案:
    mapper接口上加@Repository或@Component注解,把普通pojo实例化到spring容器中

 

1.2 整合mybatis框架

1.2.1 pom.xml

 <!-- web starter依赖 -->
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
<!-- mybatis starter依赖 -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>
<!-- mysql依赖 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

1.2.2 application.yml

# 数据源配置
spring:
	datasource:
  	driver-class-name: com.mysql.jdbc.Driver								          		 
    url: jdbc:mysql://localhost:3306/bookwebuseUnicode=true&characterEncoding=utf8
                &serverTimezone=GMT%2B8
    username: root
    password: root
        
# mybatis相关配置
mybatis:
  typeAliasesPackage: cn.bdqn.entity        			# 实体类别名
  mapperLocations: classpath:mapper/*.xml 				# 映射文件路径,放置在resources下
  config-location=classpath:mybatis/mybatis-config.xml  # 配置mybatis配置文件
  configuration:
    auto-mapping-behavior: full     					# 自动映射级别

1.3 整合mybatis-plus

为简化开发而生 , MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

润物无声

只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑。

效率至上

只需简单配置,即可快速进行 CRUD 操作,从而节省大量时间。

丰富功能

热加载、代码生成、分页、性能分析等功能一应俱全。

1.3.1 引入依赖

引入 Spring Boot Starter 父工程:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.1.RELEASE</version>
    <relativePath/>
</parent>

引入相关依赖:

<!-- Springboot项目依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<!-- mybatis-plus相关依赖 -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.2.0</version>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>

1.3.2 编码

启动类

@SpringBootApplication
@MapperScan("cn.bdqn.mapper")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(QuickStartApplication.class, args);
    }
}

Mapper类

public interface UserMapper extends BaseMapper<User> {

}

1.3.3 代码生成器

添加依赖

<!-- 代码生成器依赖 -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.2.0</version>
</dependency>
<!-- 模板引擎依赖Freemarker -->
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.29</version>
</dependency>

代码生成器

AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。

// 演示例子,执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
public class CodeGenerator {
    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
 public static String scanner(String tip) {
     Scanner scanner = new Scanner(System.in);
     StringBuilder help = new StringBuilder();
     help.append("请输入" + tip + ":");
     System.out.println(help.toString());
     if (scanner.hasNext()) {
     		String ipt = scanner.next();
       	if (StringUtils.isNotEmpty(ipt)) {
       			return ipt;
       	}
     }
     throw new MybatisPlusException("请输入正确的" + tip + "!");
}

public static void main(String[] args) {
        // 代码生成器
	AutoGenerator mpg = new AutoGenerator();

  // 全局配置
  GlobalConfig gc = new GlobalConfig();
  String projectPath = System.getProperty("user.dir");
  gc.setOutputDir(projectPath + "/src/main/java");
  gc.setAuthor("jobob");
  gc.setOpen(false);
  // gc.setSwagger2(true); 实体属性 Swagger2 注解
  mpg.setGlobalConfig(gc);

  // 数据源配置
  DataSourceConfig dsc = new DataSourceConfig();
  dsc.setUrl("jdbc:mysql://localhost:3306/ant?									                     				useUnicode=true&useSSL=false&characterEncoding=utf8");
  // dsc.setSchemaName("public");
  dsc.setDriverName("com.mysql.jdbc.Driver");
  dsc.setUsername("root");
  dsc.setPassword("密码");
  mpg.setDataSource(dsc);

  // 包配置
  PackageConfig pc = new PackageConfig();
  pc.setModuleName(scanner("模块名"));
  pc.setParent("com.baomidou.ant");
  mpg.setPackageInfo(pc);

  // 自定义配置
  InjectionConfig cfg = new InjectionConfig() {
    @Override
    public void initMap() {
    // to do nothing
    }
  };

  // 如果模板引擎是 freemarker
  String templatePath = "/templates/mapper.xml.ftl";
  // 如果模板引擎是 velocity
  // String templatePath = "/templates/mapper.xml.vm";

  // 自定义输出配置
  List<FileOutConfig> focList = new ArrayList<>();
  // 自定义配置会被优先输出
  focList.add(new FileOutConfig(templatePath) {
    @Override
    public String outputFile(TableInfo tableInfo) {
   		 // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
   		 return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
   			 + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
    }
  });
  /*
  cfg.setFileCreate(new IFileCreate() {
    @Override
    public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, 									String filePath) {
      // 判断自定义文件夹是否需要创建
      checkDir("调用默认方法创建的目录");
      return false;
    }
  });
    */
    cfg.setFileOutConfigList(focList);
    mpg.setCfg(cfg);

  // 配置模板
  TemplateConfig templateConfig = new TemplateConfig();

  // 配置自定义输出模板
  //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
  // templateConfig.setEntity("templates/entity2.java");
  // templateConfig.setService();
  // templateConfig.setController();

  templateConfig.setXml(null);
  mpg.setTemplate(templateConfig);

  // 策略配置
  StrategyConfig strategy = new StrategyConfig();
  strategy.setNaming(NamingStrategy.underline_to_camel);
  strategy.setColumnNaming(NamingStrategy.underline_to_camel);
  strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");
  strategy.setEntityLombokModel(true);
  strategy.setRestControllerStyle(true);
  // 公共父类
  strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
  // 写于父类中的公共字段
  strategy.setSuperEntityColumns("id");
  strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
  strategy.setControllerMappingHyphenStyle(true);
  strategy.setTablePrefix(pc.getModuleName() + "_");
  mpg.setStrategy(strategy);
  mpg.setTemplateEngine(new FreemarkerTemplateEngine());
  mpg.execute();
}
}

1.3.4 CRUD接口

泛型 T 为任意实体对象 ;

参数 Serializable 为任意类型主键

对象 Wrapper条件构造器

Mapper CRUD 接口

/**
 * 插入一条记录
 * @param entity 实体对象
 * @return 插入成功记录数
 */
int insert(T entity);

/** 
 * 根据 ID 删除  
 * @param id 主键ID
 * @return 删除成功记录数
 */
int deleteById(Serializable id);

/**
 * 根据 whereEntity 条件,更新记录
 * @param entity 实体对象 (set 条件值,可为 null)
 * @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
 * @return 修改成功记录数
 */
int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);
 

/**
 * 根据 entity 条件,查询全部记录
 * @param queryWrapper 实体对象封装操作类(可以为 null)
 * @return 实体集合
 */
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
 

/**
 * 根据 entity 条件,查询全部记录(并翻页)
 * @param page 分页查询条件(可以为 RowBounds.DEFAULT)
 * @param queryWrapper 实体对象封装操作类(可以为 null)
 * @return 实体分页对象
 */
IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

Service CRUD 接口

 /**
 * 插入一条记录(选择字段,策略插入)
 * @param entity 实体对象
 */
boolean save(T entity);


/**
 * 插入(批量)
 * @param entityList 实体对象集合
 * @param batchSize  插入批次数量
 */
boolean saveBatch(Collection<T> entityList);


/**
 * 根据 entity 条件,删除记录
 * @param queryWrapper 实体包装类 {@link 		     											com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
 */
boolean remove(Wrapper<T> queryWrapper);
 

/**
 * 根据 whereEntity 条件,更新记录
 * @param entity 实体对象
 * @param updateWrapper 实体对象封装操作类 {@link 													 
 com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper}
 */
boolean update(T entity, Wrapper<T> updateWrapper);
 

/**
 * 查询列表
 * @param queryWrapper 实体对象封装操作类 {@link 													com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
 */
List<T> list(Wrapper<T> queryWrapper);
page

/**
 * 翻页查询
 * @param page         翻页对象
 * @param queryWrapper 实体对象封装操作类 {@link 												com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
 */
IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper);

1.3.5 条件构造器

AbstractWrapper

    QueryWrapper 和 UpdateWrapper 的父类,用于生成 sql 的 where 条件, entity 属性也用于生成 sql 的 where 条件
     常用方法:eq、like、gt大于、lt小于、isNull、in、

QueryWrapper

继承自 AbstractWrapper ,自身的内部属性 entity 也用于生成 where 条件

UpdateWrapper

继承自 AbstractWrapper ,自身的内部属性 entity 也用于生成 where 条件

1.3.6 分页插件

 @Bean
 public PaginationInterceptor paginationInterceptor() {
     PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
     // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
     // paginationInterceptor.setOverflow(false);
     // 设置最大单页限制数量,默认 500 条,-1 不受限制
     // paginationInterceptor.setLimit(500);
     return paginationInterceptor;
 }

 

1.4 thymeleaf模板引擎

SpringBoot对JSP的支持很差,但是提供了一些其他模板引擎:FreeMarker、Thymeleaf等,我们推荐使用thymeleaf作为模板引擎。它本身是一个Java类库,嵌入HTML之后,可以作为Web应用的MVC结构的View层。

1.4.1 模板引入

thymeleaf官网:https://www.thymeleaf.org/

thymeleaf模板标签引用

<html xmlns:th="http://www.thymeleaf.org"> 通过使用命名空间(<html xmlns:th="http://www.thymeleaf.org">),可以将页面转为动态视图,动态处理数据时,使用“th:”作为前缀。

引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    <version>2.2.1.RELEASE</version>
</dependency>

1.4.2 thymeleaf语法

    变量表达式: ${...}
    选择变量表达式: *{...}
    消息表达: #{...}
    链接URL表达式: @{...}
    片段表达式: ~{...}

1.4.3基本语法

变量表达式: ${...}
	<span th:text="${today}">13 february 2011</span>
	<span th:text="${session.user.firstName}">Sebastian</span>
	<span th:text="'The name of the user is ' + ${str} "><br>
	<span th:text="${#dates.format(emp.birth, 'yyyy-MM-dd HH:mm')}"></span> 显示日期
链接URL表达式: @{...} 
	<a href="details.html" th:href="@{/order/details(orderId=${o.id})}">view</a>
	<link  th:href=”@{bootstrap/css/bootstrap.min.css}” rel=”stylesheet”>
	<script th:src=”@{jquery-1.10.2.min.js}” type=”text/javascript”></script>	

1.4.4 条件评估

if语法:
	<div th:if="${user.isAdmin()} == false"></div> 
	<div th:if="${user.isAdmin()}"></div>  
	<a href="comments.html" 
		 th:if="${not #lists.isEmpty(prod.comments)}">view</a>
	
switch语法:
	<div th:switch="${user.role}">
    <p th:case="'admin'">User is an administrator</p>
    <p th:case="#{roles.manager}">User is a manager</p>
    <p th:case="*">User is some other thing</p>
  </div>

1.4.5 迭代循环

	<tr th:each="prod : ${prods}">
        <td th:text="${prod.name}">Onions</td>
        <td th:text="${prod.price}">2.41</td>
        <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
    </tr>
    
迭代状态 iterStat:
    <tr th:each="prod,iterStat : ${prods}" th:class="${iterStat.odd}? 'odd'">
        <td th:text="${prod.name}">Onions</td>
        <td th:text="${prod.price}">2.41</td>
        <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
    </tr>
	未明确设置状态变量,则Thymeleaf将始终通过Stat为迭代变量的名称添加后缀来为您创建一个    
	<tr th:each="prod : ${prods}" th:class="${prodStat.odd}? 'odd'">
        <td th:text="${prod.name}">Onions</td>
        <td th:text="${prod.price}">2.41</td>
        <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
  	</tr>

1.4.6 星号语法*{}

变量表达式不仅可以写成${...},而且还可以写成*{...}。但是,有一个重要的区别:星号语法在选定对象而不是整个上下文上评估表达式。也就是说,只要没有选定的对象,美元和星号的语法就完全一样。

什么是选定对象?使用该th:object属性的表达式的结果

<div th:object="${session.user}">
    <p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
    <p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
    <p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div>
等同于
<div>
    <p>Name: <span th:text="${session.user.firstName}">Sebastian</span>.</p>
    <p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p>
    <p>Nationality: <span th:text="${session.user.nationality}">Saturn</span>.</p>
</div>

当然,美元和星号语法可以混合使用, 选择对象后,选定的对象也可以作为#object表达式变量用于美元表达式 。

<div th:object="${session.user}">
    <p>Name:<span th:text="${#object.firstName}">Sebastian</span></p>
    <p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p>
    <p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div>

1.4.7 模板布局

在我们的模板中,我们经常需要包含其他模板中的部分,例如页脚,页眉,菜单等部分 。 为了做到这一点,Thymeleaf需要我们定义这些要包含的部分“片段”,可以使用该th:fragment属性来完成。

定义片段footer.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
  <body>
    <div th:fragment="copy">
      &copy; 2011 The Good Thymes Virtual Grocery
    </div> 
  </body> 
</html

引用片段

我们可以使用th:insertth:replaceth:include 属性之一轻松地将其包含在主页中 , 从Thymeleaf 3.0开始不再建议使用 th:include

<div th:insert="footer :: copy"></div>

<div th:replace="footer :: copy"></div>

<div th:include="footer :: copy"></div>

th:insertth:replace(和th:include)之间的差异

  • th:insert 最简单:它将简单地将指定的片段作为其host标签的主体插入

  • th:replace实际上将其主机标签替换为指定的片段。

  • th:include与相似th:insert,但不插入片段,而是仅插入该片段的内容。

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值