springboot简写笔记二
SpringBoot 整合 Mybatis
参考文档。。。
SpringBoot 数据访问操作
增删改查的操作与springmvc中大致一样,在usermapper.xml中写sql语句的时候我们可以直接用*查询而不用别名,因为yml配置中加了下划线转驼峰配置。
这里强调分页查询操作流程:
UserQuery
package com.xxxx.springboot.query;
public class UserQuery {
//当前页,就是传过来的json数据中这个参数是显示查询到的数据中第几页,比如我传过来的是pageNum=2,pageSize=10,userName=‘aa’
// 那么就是查到aa对应的数据中显示第11到20个的数据,对应下面的1,2,3,4,5.。。。页按钮就是在当前查询中搜索框传userName值过来拿
//到总共的数据数量,传过来pageNum显示查到的数据中的第几页来实现的,而pageSize的数量一般就是默认的。
private Integer pageNum=1;
//每页显示的数量 显示查询到的数据中每一页最多有几个数据
private Integer pageSize=10;
//查询条件 通过用户名查询 通过数据查询
private String userName;
public Integer getPageNum() {
return pageNum;
}
public void setPageNum(Integer pageNum) {
this.pageNum = pageNum;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
dao层定义
//分页查询用户列表
public List<User> selectByParams(UserQuery userQuery);
service层定义
public PageInfo<User> queryByParams(UserQuery userQuery){
PageHelper.startPage(userQuery.getPageNum(),userQuery.getPageSize());
return new PageInfo(userDao.selectByParams(userQuery));
}
controller层定义
@GetMapping("user/list")
public ResultInfo queryByParams(@RequestBody UserQuery userQuery){
ResultInfo resultInfo = new ResultInfo();
PageInfo<User> userPageInfo=null;
/*try {
userPageInfo=userService.queryByParams(userQuery);
resultInfo.setResult(userPageInfo);
} catch (Exception e) {
e.printStackTrace();
}*/
userPageInfo=userService.queryByParams(userQuery);
resultInfo.setResult(userPageInfo);
return resultInfo;
}
select语句
<select id="selectByParams" parameterType="com.xxxx.springboot.query.UserQuery" resultType="com.xxxx.springboot.po.User">
select
*
from
tb_user2
<where>
<if test="null !=userName and userName !=''">
and user_name like concat('%',#{userName},'%')
</if>
</where>
</select>
当前页,就是传过来的json数据中这个参数是显示查询到的数据中第几页,比如我传过来的是pageNum=2,pageSize=10,userName=‘aa’那么就是查到aa对应的数据中显示第11到20个的数据,对应下面的1,2,3,4,5.。。。页按钮就是在当前查询中搜索框传userName值过来拿到总共的数据数量,传过来pageNum显示查到的数据中的第几页来实现的,而pageSize的数量一般就是默认的。
SpringBoot 应⽤热部署
热部署的作用就是可以让程序在补重启的情况下进行代码更新,当项目大了的时候,重启是非常需要时间的,所以热部署起到了关键的作用。
步骤:
加依赖
<!-- DevTools 的坐标 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<!--当前这个项⽬被继承之后,这个不向下传递-->
<optional>true</optional>
</dependency>
加坐标
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork><!-- 如果没有该配置,热部署的devtools不⽣效 -->
</configuration>
</plugin>
yml配置
## 热部署配置
devtools:
restart:
enabled: true
# 设置重启的⽬录,添加⽬录的⽂件需要restart
additional-paths: src/main/java
# 解决项⽬⾃动重新编译后接⼝报404的问题
poll-interval: 3000
quiet-period: 1000
IDEA 配置
ctrl + shift + alt + /,选择Registry,勾上 Compiler autoMake allow when app running
就ok了。
SpringBoot 单元测试
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
在mian下面创建文件夹
test.com.xxxx.springboot并设置成test包
import com.xxxx.springboot.Starter;
import com.xxxx.springboot.po.User;
import com.xxxx.springboot.service.UserService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
//此测试类的作用和之前普通的测试一样,都是调用service层的方法来测试
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Starter.class})
public class UserServiceTest {
private Logger logger= LoggerFactory.getLogger(UserServiceTest.class);
@Resource
private UserService userService;
@Before
public void test1(){
logger.info("测试开始前。。。");
}
@Test
public void test2(){
logger.info("测试中。。。");
User user = userService.queryUserById(1);
System.out.println(user);
}
@After
public void test3(){
logger.info("测试结束。。。");
}
}