Spring-boot
一. 第一个Spring-boot的应用
1.1 maven配置
在maven文件中加入如下的依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
1.2 编写主配置文件
spring-boot的主配置文件名字按照约定叫application.yml或者application.properties,推荐使用application.yml,文件的内容如下:
server:
port: 8080
spring:
application:
name: first-spring-boot
1.3 编写入口程序
@SpringBootApplication
public class FirstBootApplication {
public static void main( String[] args ) {
SpringApplication.run(FirstBootApplication.class, args);
}
}
spring-boot的启动类上必须要加上@SpringBootApplication这个注解。
1.4 编写Controller
@RestController
@RequestMapping("first")
public class FirstController {
@RequestMapping
public Object print() {
return Arrays.asList("hello", "world");
}
}
1.5 测试
启动主程序,然后在浏览器中输入:http://localhost:8080/first
二. banner的设置
在spring-boot应用启动的时候,会出现一个很大的“Spring”字样,那么我们如何修改呢?
在resources资源文件下创建一个名为banner.txt的文件,然后在这个文件中去定义我们自己想要的内容。目前网上有很多可以定制各种字样的网站。
三. Logback
日志任何一个项目都必须要存在的,它可以记录我们的很多信息 ,例如报错信息,访问信息等,方便我们进行系统的调试和数据的分析,那么我们就得选择一个好的日志框架。我们这节主要讲解Logback这个日志框架,它的执行速度上比Log4j可以快上最高10倍以上。
使用Logback我们首先得定义名为logback-spring.xml文件,文件的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- scan: 当配置文件被修改后, 将会被重新载入。
scanPeriod: 置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒。当scan为true时,此属性生效。默认的时间间隔为1分钟。
debug: 当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false。 -->
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<!-- 输出到控制台 -->
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<!-- 配置日志输出到控制台的格式 -->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} -- %-4relative %-5level %logger{32} %thread -- %msg%n</pattern>
<charset>utf-8</charset>
</encoder>
</appender>
<!-- 将日志记录到文件当中 -->
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 基于时间和大小的的滚动策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!--日志文件输出的文件名, 必须包含%i, 从1开始-->
<FileNamePattern>D:/logs/logback.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<!--日志文件保留天数-->
<MaxHistory>30</MaxHistory>
<!-- 最大20KB 超过最大值,会重新建一个文件-->
<maxFileSize>20MB</maxFileSize>
<!-- 所有的日志加起来最大的大小 -->
<totalSizeCap>400MB</totalSizeCap>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %thread -- %msg%n</pattern>
<charset>utf-8</charset>
</encoder>
</appender>
<!-- root节点是必选节点,用来指定最基础的日志输出级别,只有一个level属性。 -->
<root level="INFO">
<!-- 标识这个appender将会添加到这个loger。 -->
<appender-ref ref="stdout"/>
<appender-ref ref="file"/>
</root>
</configuration>
%d: 日期转换,花括号中指定日期的格式。
%-4relative: 该条日志输出的时间,这个时间是相对于服务器启动到打印出这条日志的相对时间,4表示时间占用的宽度。
%-5level: 日志的级别,日志总共5个级别,分别是debug,info,warn,error,fatal,从左往右日志级别越高,5表示日志级别占用的字符宽度。
%thread:该日志所属的线程。
%msg:日志信息。
%logger{32}:输出该日志信息的类,32表示包的层级。
%n: 换行。
四. yaml语法
4.1基本语法
k:(空格)v: 表示一对键值对,空格是必须的。
以空格的缩进控制层级关系,空多少个格子没有特定的要求,只要左端对齐,都是同一级的数据。
spring:
datasource:
username: root
password: 123456
4.2 值的写法
A. 字面量:普通的值(数字,字符串,布尔值)
person:
name: zhangsan
male: false
age: 20
B. 数组或者集合(类似于java中的List、Set)
pets:`
- cat
- dog
- pig
五. thymeleaf模板引擎
Thymeleaf是面向Web和独立环境的现代服务器端Java模板引擎,能够处理HTML, XML, JavaScript, CSS甚至纯文本。Thymeleaf的主要目标是提供一个优雅和高度可维护的创建模板的方式。为了实现这一点,它建立在自然模板的概念上,将其逻辑注入到模板文件中,不会影响模板被用作设计原型。这改善了设计的沟通,弥合了设计和开发团队之间的差距。Thymeleaf也从一开始就设计了Web标准 - 特别是HTML5 - 允许您创建完全验证的模板,如果这是您需要的。下面我们就开始学习spring-boot是如何整合thymeleaf.
5.1 配置依赖
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
5.2 application.yml配置
spring:
resources:
#静态资源的存放位置,名字也可以叫/public
static-locations: classpath:/static/
thymeleaf:
enabled: true #是否使用thymeleaf模板引擎
cache: false #是否缓存,在开发阶段设置false, 生产上为true
encoding: utf-8
mode: html
prefix: classpath:/templates/ #模板存放的位置
suffix: .html #后缀
5.3 thymeleaf的用法
A. 引入命名空间
<html xmlns:th="http://www.thymeleaf.org" lang="en">
B. 资源引入与普通链接
th:src="@{}"
<script type="text/javascript" th:src="@{js/jquery-1.12.4.js}"></script>
<img th:src="@{images/ly.jpg}" style="height: 230px; width: 150px;">
th:href="@{}"
<link rel="stylesheet" type="text/css" th:href="@{css/bootstrap.min.css}" />
<a th:href="@{http://www.baidu.com}">百度</a>
C. 普通取值
<span th:text="${user.name}"></span>
D. 字符串拼接
<span th:text="${'你好: ' + user.name + ', 欢迎您!'}"></span>
<span th:text="|你好: ${user.name}, 欢迎您!|"></span>
E. 链接取值
<a href="delete?id=${user.id}">删除</a> <!-- 获取不到user.id的值 --> <br>
<a th:href="@{'delete?id=' + ${user.id} + '&name=' + ${user.name}}">删除</a> <br>
<a th:href="@{delte(id=${user.id}, name=${user.name})}">删除</a>
F. 条件判断
<span th:if="${user.id < 10}">hello</span>
<span th:unless="${user.id > 10}">hello</span>
G. 其他形式的条件判断
if-then
<span th:text="${age} > 10 ? '年龄大于10岁'"></span>
if-then-else
<span th:text="${age > 20 ? '年龄大于20岁' : '年龄小于或等于20岁'}"></span>
if-else
<span th:text="${name} ?: '其他人'"></span> <!-- ${name}没有值,直接取'其他人' -->
H. 等值判断比较
<div th:switch="${user.name}">
<p th:case="${admin}">超级管理员</p>
<p th:case="'zhangsan'">普通用户</p>
<p th:case="*">其他用户</p>
</div>
I. 循环遍历
<table border="1" width="20%">
<tr>
<th>ID</th>
<th>用户名</th>
</tr>
<tr th:each="u,stat:${list}">
<td th:text="${u.id}"></td>
<td th:text="${u.name}"></td>
</tr>
</table>
其中u为每次遍历的数据,stat为状态,状态总共有以下几种方式:
状态码 | 描述信息 |
---|---|
first | 是否为第一条数据 |
last | 是否为最后一条数据 |
even | 是否为偶数条数据 |
odd | 是否为奇数条数据 |
index | 数据的索引 |
count | 数据是第几条 |
size | 遍历的数据的总量 |
current | 当前遍历的数据 |
J. map
<p th:each="m:${mps}">
<span th:text="${m.key}"></span> ----
<span th:text="${m.value}"></span><br>
</p>
K. 模板表达式
thymeleaf提供了丰富的模板表达式,而且有很多的方法用以使用,主要包括以下模板表达式:
- #lists
- ${#lists.size() }
- ${#lists.isEmpty()}
- ${#lists.contains()}
- #maps
- ${#maps.size()}
- ${#maps.isEmpty()}
- ${#maps.containsKey()}
- ${#maps.containsValue()}
3)#strings
-
${#strings.isEmpty(name)}
-
${#strings.contains(name,‘ez’)}
-
${#strings.containsIgnoreCase(name,‘ez’)}
-
${#strings.startsWith(name,‘Don’)}
-
${#strings.endsWith(name,endingFragment)}
-
${#strings.indexOf(name,frag)}
-
${#strings.substring(name,3,5)}
-
${#strings.replace(name,‘las’,‘ler’)}
-
${#strings.listSplit(namesStr,’,’)}
-
${#strings.trim(str)}
-
${#strings.equals(str)}
-
${#strings.equalsIgnoreCase(str)}
-
${#strings.prepend(str,prefix)}
-
${#strings.append(str,suffix)}
-
${#strings.toUpperCase(name)}
-
${#strings.toLowerCase(name)}
- #dates
- ${#dates.format()}
六. spring-boot与mybatis的整合
6.1 配置依赖
<!-- alibaba的druid数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.17</version>
</dependency>
<!-- mybatis代码自动生成 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<!-- mybatis分页支持 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
<!-- tk.mybatis对mybatis做了二次封装 -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.1.5</version>
</dependency>
<!-- tk.mybatis与spring boot整合 -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
6.2 代码自动生成配置
6.2.1 插件配置
<!-- mybatis代码自动生成 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
</dependencies>
<configuration>
<!-- 配置文件的名字 -->
<configurationFile>src/main/resources/mybatis-generator-config.xml</configurationFile>
<overwrite>true</overwrite>
</configuration>
</plugin>
6.2.1 配置文件编写
jdbc.properties,该文件与application.yml并不同,它仅仅是用于mybatis生成代码用。
mysql.url=jdbc:mysql://localhost:3306/qf?useSSL=false&serverTimezone=UTC
mysql.driver=com.mysql.cj.jdbc.Driver
mysql.username=root
mysql.password=123456
mybatis-generator-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--加载配置文件,为下面读取数据库信息准备-->
<properties resource="jdbc.properties"/>
<!--
context:生成一组对象的环境
id:必选,上下文id,用于在生成错误时提示
defaultModelType=flat 就是将所有的属性都生成在一个类中。
targetRuntime:
1,MyBatis3:默认的值,生成基于MyBatis3.x以上版本的内容,包括XXXBySample;
2,MyBatis3Simple:类似MyBatis3,只是不生成XXXBySample;
-->
<context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<!-- 自动识别数据库关键字,默认false,如果设置为true,根据SqlReservedWords中定义的关键字列表;
一般保留默认值,遇到数据库关键字(Java关键字),使用columnOverride覆盖
-->
<property name="autoDelimitKeywords" value="true" />
<!-- beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,比如ORACLE就是双引号,MYSQL默认是`反引号; -->
<property name="beginningDelimiter" value="`" />
<property name="endingDelimiter" value="`" />
<property name="javaFileEncoding" value="utf-8" />
<!-- 格式化java代码 -->
<property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
<!-- 格式化XML代码 -->
<property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
<!-- 注释 -->
<commentGenerator >
<property name="suppressAllComments" value="false"/><!-- 是否取消注释 -->
<property name="suppressDate" value="false" /> <!-- 是否生成注释代时间戳-->
</commentGenerator>
<!--数据库链接地址账号密码-->
<jdbcConnection driverClass="${mysql.driver}"
connectionURL="${mysql.url}"
userId="${mysql.username}"
password="${mysql.password}">
</jdbcConnection>
<!-- 类型转换 -->
<javaTypeResolver>
<!--
true:使用BigDecimal对应DECIMAL和 NUMERIC数据类型
false:默认,
scale>0;length>18:使用BigDecimal;
scale=0;length[10,18]:使用Long;
scale=0;length[5,9]:使用Integer;
scale=0;length<5:使用Short;
-->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- java模型创建器,是必须要的元素
负责:1,key类(见context的defaultModelType);2,java类;3,查询类
targetPackage:生成的类要放的包,真实的包受enableSubPackages属性控制;
targetProject:目标项目,指定一个存在的目录下,生成的内容会放到指定目录中,如果目录不存在,MBG不会自动建目录
-->
<javaModelGenerator targetPackage="com.qf.pojo" targetProject="src/main/java">
<!-- for MyBatis3/MyBatis3Simple
自动为每一个生成的类创建一个构造方法,构造方法包含了所有的field;而不是使用setter;
-->
<property name="constructorBased" value="true"/>
<!-- 在targetPackage的基础上,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false -->
<property name="enableSubPackages" value="false"/>
<!-- 设置是否在getter方法中,对String类型字段调用trim()方法 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成的mapper文件存放的位置 -->
<sqlMapGenerator targetPackage="mybatis.mapper" targetProject="src/main/resources">
<!-- 在targetPackage的基础上,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false -->
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 对于mybatis来说,即生成Mapper接口,注意,如果没有配置该元素,那么默认不会生成Mapper接口
targetPackage/targetProject:同javaModelGenerator
type:选择怎么生成mapper接口(在MyBatis3/MyBatis3Simple下):
1,ANNOTATEDMAPPER:会生成使用Mapper接口+Annotation的方式创建(SQL生成在annotation中),不会生成对应的XML;
2,MIXEDMAPPER:使用混合配置,会生成Mapper接口,并适当添加合适的Annotation,但是XML会生成在XML中;
3,XMLMAPPER:会生成Mapper接口,接口完全依赖XML;
注意,如果context是MyBatis3Simple:只支持ANNOTATEDMAPPER和XMLMAPPER
-->
<javaClientGenerator targetPackage="com.qf.dao" type="XMLMAPPER" targetProject="src/main/java">
<!-- 在targetPackage的基础上,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false -->
<property name="enableSubPackages" value="false"/>
<!-- 可以为所有生成的接口添加一个父接口,但是MBG只负责生成,不负责检查
<property name="rootInterface" value=""/>
-->
</javaClientGenerator>
<!-- 选择一个table来生成相关文件,可以有一个或多个table,必须要有table元素
选择的table会生成一下文件:
1,SQL map文件
2,生成一个主键类;
3,除了BLOB和主键的其他字段的类;
4,包含BLOB的类;
5,一个用户生成动态查询的条件类(selectByExample, deleteByExample),可选;
6,Mapper接口(可选)
tableName(必要):要生成对象的表名;
注意:大小写敏感问题。正常情况下,MBG会自动的去识别数据库标识符的大小写敏感度,在一般情况下,MBG会
根据设置的schema,catalog或tablename去查询数据表,按照下面的流程:
1,如果schema,catalog或tablename中有空格,那么设置的是什么格式,就精确的使用指定的大小写格式去查询;
2,否则,如果数据库的标识符使用大写的,那么MBG自动把表名变成大写再查找;
3,否则,如果数据库的标识符使用小写的,那么MBG自动把表名变成小写再查找;
4,否则,使用指定的大小写格式查询;
另外的,如果在创建表的时候,使用的""把数据库对象规定大小写,就算数据库标识符是使用的大写,在这种情况下也会使用给定的大小写来创建表名;
这个时候,请设置delimitIdentifiers="true"即可保留大小写格式;
可选:
1,domainObjectName:生成的domain类的名字,如果不设置,直接使用表名作为domain类的名字;可以设置为somepck.domainName,那么会自动把domainName类再放到somepck包里面;
2,enableInsert(默认true):指定是否生成insert语句;
3,enableSelectByPrimaryKey(默认true):指定是否生成按照主键查询对象的语句(就是getById或get);
4,enableSelectByExample(默认true):MyBatis3Simple为false,指定是否生成动态查询语句;
5,enableUpdateByPrimaryKey(默认true):指定是否生成按照主键修改对象的语句(即update);
6,enableDeleteByPrimaryKey(默认true):指定是否生成按照主键删除对象的语句(即delete);
7,enableDeleteByExample(默认true):MyBatis3Simple为false,指定是否生成动态删除语句;
8,enableCountByExample(默认true):MyBatis3Simple为false,指定是否生成动态查询总条数语句(用于分页的总条数查询);
9,enableUpdateByExample(默认true):MyBatis3Simple为false,指定是否生成动态修改语句(只修改对象中不为空的属性);
-->
<table schema="qf" tableName="user" domainObjectName="MyUser" enableInsert="true">
<!-- 注意,该属性只针对MyBatis3Simple有用;
如果选择的runtime是MyBatis3Simple,那么会生成一个SelectAll方法,如果指定了selectAllOrderByClause,那么会在该SQL中添加指定的这个order条件;
-->
<property name="selectAllOrderByClause" value="id desc"/>
<!-- 如果设置为true,生成的model类会直接使用column本身的名字,而不会再使用驼峰命名方法,比如BORN_DATE,生成的属性名字就是BORN_DATE,而不会是bornDate -->
<property name="useActualColumnNames" value="true"/>
<!-- generatedKey用于生成生成主键的方法,
如果设置了该元素,MBG会在生成的<insert>元素中生成一条正确的<selectKey>元素,该元素可选
column:主键的列名;
sqlStatement:要生成的selectKey语句,有以下可选项:
Cloudscape:相当于selectKey的SQL为: VALUES IDENTITY_VAL_LOCAL()
DB2 :相当于selectKey的SQL为: VALUES IDENTITY_VAL_LOCAL()
DB2_MF :相当于selectKey的SQL为:SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1
Derby :相当于selectKey的SQL为:VALUES IDENTITY_VAL_LOCAL()
HSQLDB :相当于selectKey的SQL为:CALL IDENTITY()
Informix :相当于selectKey的SQL为:select dbinfo('sqlca.sqlerrd1') from systables where tabid=1
MySql :相当于selectKey的SQL为:SELECT LAST_INSERT_ID()
SqlServer :相当于selectKey的SQL为:SELECT SCOPE_IDENTITY()
SYBASE :相当于selectKey的SQL为:SELECT @@IDENTITY
JDBC :相当于在生成的insert元素上添加useGeneratedKeys="true"和keyProperty属性
<generatedKey column="id" sqlStatement="MySql" identity="true" />
-->
<generatedKey column="id" sqlStatement="MySql" identity="true" />
<!-- 用来修改表中某个列的属性,MBG会使用修改后的列来生成domain的属性;
column:要重新设置的列名;
注意,一个table元素中可以有多个columnOverride元素哈~
使用property属性来指定列要生成的属性名称
-->
<!--
<columnOverride column="username">
<property name="property" value="userName"/>
</columnOverride>
-->
<!-- ignoreColumn设置一个MGB忽略的列,如果设置了该列,那么在生成的domain中,生成的SQL中,都不会有该列出现
column:指定要忽略的列的名字;
delimitedColumnName:参考table元素的delimitAllColumns配置,默认为false
注意,一个table元素中可以有多个ignoreColumn元素
<ignoreColumn column="deptId" delimitedColumnName=""/>
-->
</table>
</context>
</generatorConfiguration>
6.2.3 执行生成代码
6.3 分页插件的使用
6.3.1 application.yml文件的配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/qf?useSSL=false&serverTimezone=UTC
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
druid:
initial-size: 5
max-active: 8
min-idle: 2
validation-query: 'select 1'
test-on-borrow: false
test-on-return: false
test-while-idle: true
# psCache, 缓存preparedStatement, 对支持游标的数据库性能有巨大的提升,oracle开启,mysql建议关闭
pool-prepared-statements: false
# psCache开启的时候有效
max-open-prepared-statements: 100
# 一个连接在被驱逐出连接池的时候,在连接池中最小的空闲时间,单位为毫秒
min-evictable-idle-time-millis: 30000
# 距离上次释放空闲连接的时间间隔
time-between-eviction-runs-millis: 30000
pagehelper:
dialect: mysql #配置数据库的方言
# 分页合理化参数,默认值为false。当该参数设置为 true 时,pageNum<=0 时会查询第一页, pageNum>pages(超过总数时),会查询最后一页
reasonable: true
# 支持通过 Mapper 接口参数来传递分页参数
support-methods-arguments: true
mybatis:
mapper-locations: classpath:/mybatis.mapper/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
6.3.2 通用接口的编写
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {
}
6.3.3 接口的编写
import com.github.pagehelper.Page;
import com.qf.pojo.MyUser;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface MyUserMapper extends MyMapper<MyUser> {
List<MyUser> selectAll();
Page<MyUser> selectPage();
}
6.3.4 服务层编写
import com.github.pagehelper.PageHelper;
import com.qf.dao.MyUserMapper;
import com.qf.pojo.MyUser;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UserService {
@Resource
private MyUserMapper myUserMapper;
public List<MyUser> getList() {
PageHelper.startPage(1, 2);
Example example = new Example(MyUser.class);
List<MyUser> list = myUserMapper.selectByExample(example);
return list;
}
}
参考地址:
mybatis官网参考地址:http://www.mybatis.org/spring-boot-starter/
mybatis生成代码官网参考地址:http://www.mybatis.org/generator/
##### 6.3.4 服务层编写
import com.github.pagehelper.PageHelper;
import com.qf.dao.MyUserMapper;
import com.qf.pojo.MyUser;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UserService {
@Resource
private MyUserMapper myUserMapper;
public List<MyUser> getList() {
PageHelper.startPage(1, 2);
Example example = new Example(MyUser.class);
List<MyUser> list = myUserMapper.selectByExample(example);
return list;
}
}
参考地址:
**mybatis官网参考地址:<http://www.mybatis.org/spring-boot-starter/>**
**mybatis生成代码官网参考地址:<http://www.mybatis.org/generator/>**
**配置文件编写参考地址:<https://www.jianshu.com/p/e09d2370b796>**