项目的广告管理模块和用户管理模块
在上一个博客中(70章博客),我们完成了课程管理模块,接下来我们完成广告管理模块和用户管理模块
广告管理模块:
实现以下功能:
广告位列表查询
添加广告位
回显广告位名称
修改广告位
广告分页查询
图片上传
新建广告
回显广告信息
修改广告
广告状态上下线
对应数据库表,在70章博客里有了
表关系介绍:
ER图:
对应字段信息在表里面可以看到
广告管理模块的实现:
广告位列表查询:
需求分析
需求:点击广告列表按钮进行广告列表展示
Dao层:PromotionSpaceMapper :
package com. lagou. dao ;
import com. lagou. domain. PromotionSpace ;
import java. util. List ;
public interface PromotionSpaceMapper {
public List < PromotionSpace > findAllPromotionSpace ( ) ;
}
对应映射配置文件(PromotionSpaceMapper .xml):
<?xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace = " com.lagou.dao.PromotionSpaceMapper" >
< select id = " findAllPromotionSpace" resultType = " PromotionSpace" >
select *
from promotion_space
</ select>
</ mapper>
Service层:PromotionSpaceService及其实现类 :
package com. lagou. service ;
import com. lagou. domain. PromotionSpace ;
import java. util. List ;
public interface PromotionSpaceService {
public List < PromotionSpace > findAllPromotionSpace ( ) ;
}
package com. lagou. service. impl ;
import com. lagou. dao. PromotionSpaceMapper ;
import com. lagou. domain. PromotionSpace ;
import com. lagou. service. PromotionSpaceService ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. stereotype. Service ;
import java. util. List ;
@Service
public class PromotionSpaceServiceImpl implements PromotionSpaceService {
@Autowired
private PromotionSpaceMapper promotionSpaceMapper;
@Override
public List < PromotionSpace > findAllPromotionSpace ( ) {
List < PromotionSpace > allPromotionSpace = promotionSpaceMapper. findAllPromotionSpace ( ) ;
return allPromotionSpace;
}
}
Web层:PromotionSpaceController :
package com. lagou. controller ;
import com. lagou. domain. PromotionSpace ;
import com. lagou. domain. ResponseResult ;
import com. lagou. service. PromotionSpaceService ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. web. bind. annotation. RequestBody ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. web. bind. annotation. RestController ;
import java. util. List ;
@RestController
@RequestMapping ( "/PromotionSpace" )
public class PromotionSpaceController {
@Autowired
private PromotionSpaceService promotionSpaceService;
@RequestMapping ( "/findAllPromotionSpace" )
public ResponseResult findAllPromotionSpace ( ) {
List < PromotionSpace > allPromotionSpace = promotionSpaceService. findAllPromotionSpace ( ) ;
ResponseResult responseResult = new ResponseResult ( true , 200 , "查询所有广告位成功" ,
allPromotionSpace) ;
return responseResult;
}
}
使用Postman测试接口
添加广告位
需求分析
添加:点击广告列表按钮进行广告列表展示
Dao层:添加部分PromotionSpaceMapper :
public void savePromotionSpace ( PromotionSpace promotionSpace) ;
添加部分PromotionSpaceMapper .xml:
< insert id = " savePromotionSpace" parameterType = " PromotionSpace" >
insert into promotion_space values(null, #{name}, #{spaceKey},
#{createTime}, #{updateTime}, #{isDel})
</ insert>
Service层:添加部分PromotionSpaceService及其实现类 :
public void savePromotionSpace ( PromotionSpace promotionSpace) ;
@Override
public void savePromotionSpace ( PromotionSpace promotionSpace) {
promotionSpace. setSpaceKey ( UUID . randomUUID ( ) . toString ( ) ) ;
Date date = new Date ( ) ;
promotionSpace. setCreateTime ( date) ;
promotionSpace. setUpdateTime ( date) ;
promotionSpace. setIsDel ( 0 ) ;
promotionSpaceMapper. savePromotionSpace ( promotionSpace) ;
}
Web层:添加部分PromotionSpaceController :
@RequestMapping ( "/saveOrUpdatePromotionSpace" )
public ResponseResult saveOrUpdatePromotionSpace ( @RequestBody PromotionSpace promotionSpace) {
promotionSpaceService. savePromotionSpace ( promotionSpace) ;
ResponseResult responseResult = new ResponseResult ( true , 200 , "添加广告位成功" , null ) ;
return responseResult;
}
使用Postman测试接口
在进行修改前我们需要进行回显
回显广告位名称
需求分析
需求:点击编辑按钮,进行广告位信息回显
Dao层:添加部分PromotionSpaceMapper :
public PromotionSpace findPromotionSpaceById ( Integer id) ;
添加部分PromotionSpaceMapper .xml:
< select id = " findPromotionSpaceById" parameterType = " int" resultType = " PromotionSpace" >
select id,name
from promotion_space where id = #{id}
</ select>
Service层:添加部分PromotionSpaceService及其实现类 :
public PromotionSpace findPromotionSpaceById ( Integer id) ;
@Override
public PromotionSpace findPromotionSpaceById ( Integer id) {
PromotionSpace promotionSpaceById = promotionSpaceMapper. findPromotionSpaceById ( id) ;
return promotionSpaceById;
}
Web层:添加部分PromotionSpaceController :
@RequestMapping ( "/findPromotionSpaceById" )
public ResponseResult findPromotionSpaceById ( Integer id) {
PromotionSpace promotionSpace = promotionSpaceService. findPromotionSpaceById ( id) ;
ResponseResult responseResult = new ResponseResult ( true , 200 , "查询具体广告位成功" ,
promotionSpace) ;
return responseResult;
}
使用Postman测试接口
在回显后我们进行修改操作:
修改:页面回显基础上,点击提交按钮 真正进行数据修改
Dao层:添加部分PromotionSpaceMapper :
public void updatePromotionSpace ( PromotionSpace promotionSpace) ;
添加部分PromotionSpaceMapper .xml:
< update id = " updatePromotionSpace" parameterType = " PromotionSpace" >
update promotion_space set name = #{name},updateTime = #{updateTime} where id = #{id}
</ update>
Service层:添加部分PromotionSpaceService及其实现类 :
public void updatePromotionSpace ( PromotionSpace promotionSpace) ;
@Override
public void updatePromotionSpace ( PromotionSpace promotionSpace) {
promotionSpace. setUpdateTime ( new Date ( ) ) ;
promotionSpaceMapper. updatePromotionSpace ( promotionSpace) ;
}
Web层:修改部分PromotionSpaceController (扩展saveOrUpdatePromotionSpace方法):
@RequestMapping ( "/saveOrUpdatePromotionSpace" )
public ResponseResult saveOrUpdatePromotionSpace ( @RequestBody PromotionSpace promotionSpace) {
ResponseResult responseResult;
if ( promotionSpace. getId ( ) == null ) {
promotionSpaceService. savePromotionSpace ( promotionSpace) ;
responseResult = new ResponseResult ( true , 200 , "添加广告位成功" , null ) ;
} else {
promotionSpaceService. updatePromotionSpace ( promotionSpace) ;
responseResult = new ResponseResult ( true , 200 , "修改广告位成功" , null ) ;
}
return responseResult;
}
使用Postman测试接口
广告分页查询
需求分析
需求:点击广告列表,对广告信息进行分页列表展示
在这之前,我们需要对应类(PromotionAd)进行改造一下:
添加对应变量及其get和set方法
private PromotionSpace promotionSpace;
public PromotionSpace getPromotionSpace ( ) {
return promotionSpace;
}
public void setPromotionSpace ( PromotionSpace promotionSpace) {
this . promotionSpace = promotionSpace;
}
使得出现对应关系(虽然表没有对应外键,但并不会影响操纵,只是在自己操作或者特殊情况下可能会出现多余数据)
一般前端传递数据若是没有出错的话,一般都会直接对应,而不会出现多余数据,但是没有外键联系的话对应相应查询可能会慢
若表有外键联系的话,开发效率低一点,因为不好试错(如手动添加),最主要的是改变时不好进行改变(特别是业务改变时)
那么就出现了表的维护性和效率,一般我们不会通过外键去查询,而是通过主键去查询(这个一般都会设置,因为他只联系本身)
也就是说这个效率可以不做很大要求
那么由于维护性,当我们确定以后可能要修改的话,最好不要使用外键联系
当确定知道不会进行修改时,可以尽量进行外键联系,当然也可不使用(防止突然要修改,因为业务基本都会有)
综上所述:一般我们都不会使用外键,在维护性面前
多余的数据消除显的有点多余(因为可以通过程序来实现,只要程序不会出错)
Dao层:PromotionAdMapper:
package com. lagou. dao ;
import com. lagou. domain. PromotionAd ;
import java. util. List ;
public interface PromotionAdMapper {
public List < PromotionAd > findAllPromotionAdByPage ( ) ;
}
对应映射配置文件(PromotionAdMapper.xml):
<?xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace = " com.lagou.dao.PromotionAdMapper" >
< resultMap id = " ad_space" type = " PromotionAd" >
< id property = " id" column = " id" > </ id>
< result property = " name" column = " name" > </ result>
< result property = " spaceId" column = " spaceId" > </ result>
< result property = " keyword" column = " keyword" > </ result>
< result property = " htmlContent" column = " htmlContent" > </ result>
< result property = " text" column = " text" > </ result>
< result property = " link" column = " link" > </ result>
< result property = " startTime" column = " startTime" > </ result>
< result property = " endTime" column = " endTime" > </ result>
< result property = " createTime" column = " createTime" > </ result>
< result property = " updateTime" column = " updateTime" > </ result>
< result property = " status" column = " status" > </ result>
< result property = " priority" column = " priority" > </ result>
< result property = " img" column = " img" > </ result>
< association property = " promotionSpace" javaType = " PromotionSpace"
select = " com.lagou.dao.PromotionSpaceMapper.findPromotionSpaceById"
column = " spaceId" >
</ association>
</ resultMap>
< select id = " findAllPromotionAdByPage" resultMap = " ad_space" >
select * from promotion_ad
</ select>
</ mapper>
要使用Mybatis的分页操作,我们通常都要配置插件,但在Mybatis中的那个配置文件已经被Spring整合了
所有我们需要在对应整合的地方进行配置,如下:
添加部分applicationContext-dao.xml(扩展applicationContext-dao.xml):
< bean id = " sqlSessionFactory" class = " org.mybatis.spring.SqlSessionFactoryBean" >
< property name = " dataSource" ref = " dataSource" />
< property name = " typeAliasesPackage" value = " com.lagou.domain" > </ property>
< property name = " plugins" >
< array>
< bean class = " com.github.pagehelper.PageHelper" >
< property name = " properties" >
< value> helperDialect=mysql</ value>
</ property>
</ bean>
</ array>
</ property>
< property name = " configLocation" value = " classpath:sqlMapConfig.xml" > </ property>
</ bean>
现在我们可以编写Service层了,但是前端一般会传递两个参数过来,我们可以使用两个参数来接收,也可以使用一个类来接收
这里我们使用类来接收,防止以后出现多个参数的传递(使用类,基本只需要修改Service层就可以了)
编写PromotionAdVo类
这个类不是用来存放两个表字段信息的,而是用来存放特定需要的参数的
实际上都是一样的作用
根据参数来说,前者存放两表信息,后者存放对应需要的参数,但都是变量的存放
也都实现了好维护性的操作,且没有多余变量,即少代码的同样操作
根据表来说,前者是为了实现表的参数合并,后者只是单纯的存放需要的参数
而我们使用实体类的参数时,一般直接使用实体类当参数,因为有对应类存在了(实体类存在了)
当然,若不嫌麻烦,也可以创建对应类来存放对应参数
但不管根据哪一种,都是为了解决需求以及方便维护的操作,当有更好的时,那么肯定是使用更好的:
package com. lagou. domain ;
public class PromotionAdVo {
private Integer CurrentPage ;
private Integer PageSize ;
public Integer getCurrentPage ( ) {
return CurrentPage ;
}
public void setCurrentPage ( Integer currentPage) {
CurrentPage = currentPage;
}
public Integer getPageSize ( ) {
return PageSize ;
}
public void setPageSize ( Integer pageSize) {
PageSize = pageSize;
}
}
接下来我们编写Service层
Service层:PromotionAdService及其实现类 :
package com. lagou. service ;
import com. github. pagehelper. PageInfo ;
import com. lagou. domain. PromotionAd ;
import com. lagou. domain. PromotionAdVo ;
import java. util. List ;
public interface PromotionAdService {
public PageInfo < PromotionAd > findAllPromotionAdByPage ( PromotionAdVo promotionAdVo) ;
}
package com. lagou. service. impl ;
import com. github. pagehelper. PageHelper ;
import com. github. pagehelper. PageInfo ;
import com. lagou. dao. PromotionAdMapper ;
import com. lagou. domain. PromotionAd ;
import com. lagou. domain. PromotionAdVo ;
import com. lagou. service. PromotionAdService ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. stereotype. Service ;
import java. util. List ;
@Service
public class PromotionAdServiceImpl implements PromotionAdService {
@Autowired
private PromotionAdMapper promotionAdMapper;
@Override
public PageInfo < PromotionAd > findAllPromotionAdByPage ( PromotionAdVo promotionAdVo) {
PageHelper . startPage ( promotionAdVo. getCurrentPage ( ) , promotionAdVo. getPageSize ( ) ) ;
List < PromotionAd > allPromotionAdByPage =
promotionAdMapper. findAllPromotionAdByPage ( promotionAdVo) ;
PageInfo < PromotionAd > promotionAdPageInfo = new PageInfo < > ( allPromotionAdByPage) ;
return promotionAdPageInfo;
}
}
如图(日志):
可以发现,在日志中的确进行了拼接,对应参数是0,5,由于前端传递的参数是1,5,1表示当前页,5表示的是查询条数
发现对应参数0,5的确是第一页并查询的是5条,而这样的由来是因为limit 0,5在sql里面表示从第一条(下标为0)开始,查询5条
Web层:PromotionAdController :
package com. lagou. controller ;
import com. github. pagehelper. PageInfo ;
import com. lagou. domain. PromotionAd ;
import com. lagou. domain. PromotionAdVo ;
import com. lagou. domain. ResponseResult ;
import com. lagou. service. PromotionAdService ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. web. bind. annotation. RequestBody ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. web. bind. annotation. RestController ;
@RestController
@RequestMapping ( "/PromotionAd" )
public class PromotionAdController {
@Autowired
private PromotionAdService promotionAdService;
@RequestMapping ( "/findAllPromotionAdByPage" )
public ResponseResult findAllPromotionAdByPage ( PromotionAdVo promotionAdVo) {
PageInfo < PromotionAd > allPromotionAdByPage =
promotionAdService. findAllPromotionAdByPage ( promotionAdVo) ;
ResponseResult responseResult = new ResponseResult ( true , 200 , "广告分页查询成功" ,
allPromotionAdByPage) ;
return responseResult;
}
}
使用Postman测试接口
图片上传接口
需求分析
需求:添加广告页面,点击上传按钮,需完成图片上传
在前面课程管理模块中,我们编写过一次图片的上传了,所以可以复制过来
图片上传只需要回显图片信息即可,而图片信息是直接存在服务器对应路径下,没有经过数据库,所以只需要Web层即可
课程管理模块的也是如此
Web层:添加部分PromotionAdController :
@RequestMapping ( "/PromotionAdUpload" )
public ResponseResult PromotionAdUpload ( @RequestParam ( "file" ) MultipartFile file,
HttpServletRequest request) throws IOException {
if ( file. isEmpty ( ) ) {
throw new RuntimeException ( ) ;
}
String realPath = request. getServletContext ( ) . getRealPath ( "/" ) ;
String substring = realPath. substring ( 0 , realPath. indexOf ( "ssm_web" ) ) ;
String originalFilename = file. getOriginalFilename ( ) ;
String newFileName = System . currentTimeMillis ( ) +
originalFilename. substring ( originalFilename. lastIndexOf ( "." ) ) ;
String uploadPath = substring + "upload\\" ;
File file1 = new File ( uploadPath, newFileName) ;
if ( ! file1. getParentFile ( ) . exists ( ) ) {
file1. getParentFile ( ) . mkdirs ( ) ;
System . out. println ( "创建目录" + file1) ;
}
file. transferTo ( file1) ;
HashMap < String , String > objectObjectHashMap = new HashMap < > ( ) ;
objectObjectHashMap. put ( "fileName" , newFileName) ;
objectObjectHashMap. put ( "filePath" , "http://localhost:8080/upload/" + newFileName) ;
ResponseResult responseResult = new ResponseResult ( true , 200 , "图片上传成功" ,
objectObjectHashMap) ;
return responseResult;
}
使用Postman测试接口
接下来我们操作新建和修改广告,若可以的话,可以不看后面代码自己进行编写
新建广告
需求分析
新建需求:点击提交按钮,将页面内容保存到数据库
Dao层:添加部分PromotionAdMapper:
public void savePromotionAd ( PromotionAd promotionAd) ;
添加部分PromotionAdMapper.xml:
< insert id = " savePromotionAd" parameterType = " PromotionAd" >
INSERT INTO promotion_ad
VALUES(NULL,#{name},#{spaceId},#{keyword},
#{htmlContent},#{text},#{link},#{startTime},#{endTime},#{createTime},
#{updateTime},#{status},#{priority},#{img});
</ insert>
Service层:添加部分PromotionAdService及其实现类 :
public void savePromotionAd ( PromotionAd promotionAd) ;
@Override
public void savePromotionAd ( PromotionAd promotionAd) {
Date date = new Date ( ) ;
promotionAd. setCreateTime ( date) ;
promotionAd. setUpdateTime ( date) ;
promotionAdMapper. savePromotionAd ( promotionAd) ;
}
Web层:添加部分PromotionAdController :
@RequestMapping ( "/saveOrUpdatePromotionAd" )
public ResponseResult saveOrUpdatePromotionAd ( @RequestBody PromotionAd promotionAd) {
promotionAdService. savePromotionAd ( promotionAd) ;
ResponseResult responseResult = new ResponseResult ( true , 200 , "添加广告成功" , null ) ;
return responseResult;
}
使用Postman测试接口
接下来我们进行修改操作
但在修改操作之前,我们需要进行回显
Dao层:添加部分PromotionAdMapper:
public PromotionAd findPromotionAdById ( Integer id) ;
添加部分PromotionAdMapper.xml:
< select id = " findPromotionAdById" resultType = " PromotionAd" >
select * from promotion_ad where id=#{id}
</ select>
Service层:添加部分PromotionAdService及其实现类 :
public PromotionAd findPromotionAdById ( Integer id) ;
@Override
public PromotionAd findPromotionAdById ( Integer id) {
PromotionAd promotionAdById = promotionAdMapper. findPromotionAdById ( id) ;
return promotionAdById;
}
Web层:添加部分PromotionAdController :
@RequestMapping ( "/findPromotionAdById" )
public ResponseResult findPromotionAdById ( Integer id) {
PromotionAd promotionAdById = promotionAdService. findPromotionAdById ( id) ;
ResponseResult responseResult = new ResponseResult ( true , 200 , "查询具体广告成功" ,
promotionAdById) ;
return responseResult;
}
回显后,进行修改操作
Dao层:添加部分PromotionAdMapper:
public void updatePromotionAd ( PromotionAd promotionAd) ;
添加部分PromotionAdMapper.xml:
< update id = " updatePromotionAd" parameterType = " PromotionAd" >
update promotion_ad
< trim prefix = " SET" suffixOverrides = " ," >
< if test = " name != null and name != ''" >
name = #{name},
</ if>
< if test = " spaceId != null and spaceId != ''" >
spaceId = #{spaceId},
</ if>
< if test = " link != null" >
link=#{link},
</ if>
< if test = " status != null and status != '' or status == 0" >
status=#{status},
</ if>
< if test = " img != null" >
img=#{img},
</ if>
< if test = " text != null" >
text=#{text},
</ if>
< if test = " startTime != null" >
startTime=#{startTime},
</ if>
< if test = " endTime != null" >
endTime=#{endTime},
</ if>
< if test = " updateTime != null" >
updateTime=#{updateTime},
</ if>
</ trim>
< where>
< if test = " id != null and id != '' " > id = #{id}</ if>
</ where>
</ update>
Service层:添加部分PromotionAdService及其实现类 :
public void updatePromotionAd ( PromotionAd promotionAd) ;
@Override
public void updatePromotionAd ( PromotionAd promotionAd) {
promotionAd. setUpdateTime ( new Date ( ) ) ;
promotionAdMapper. updatePromotionAd ( promotionAd) ;
}
Web层:修改部分PromotionAdController (扩展saveOrUpdatePromotionAd方法):
@RequestMapping ( "/saveOrUpdatePromotionAd" )
public ResponseResult saveOrUpdatePromotionAd ( @RequestBody PromotionAd promotionAd) {
ResponseResult responseResult;
if ( promotionAd. getId ( ) == null ) {
promotionAdService. savePromotionAd ( promotionAd) ;
responseResult= new ResponseResult ( true , 200 , "添加广告成功" , null ) ;
} else {
promotionAdService. updatePromotionAd ( promotionAd) ;
responseResult= new ResponseResult ( true , 200 , "修改广告成功" , null ) ;
}
return responseResult;
}
广告状态上下线
需求分析
需求:点击按钮,实现状态的动态上下线
Dao层:添加部分PromotionAdMapper:
public void updatePromotionAdStatus ( PromotionAd promotionAd) ;
添加部分PromotionAdMapper.xml:
< update id = " updatePromotionAdStatus" parameterType = " PromotionAd" >
update promotion_ad set status = #{status},updateTime = #{updateTime} where id = #{id}
</ update>
Service层:添加部分PromotionAdService及其实现类 :
public void updatePromotionAdStatus ( Integer id, Integer status) ;
@Override
public void updatePromotionAdStatus ( Integer id, Integer status) {
PromotionAd promotionAd = new PromotionAd ( ) ;
promotionAd. setId ( id) ;
promotionAd. setStatus ( status) ;
promotionAd. setUpdateTime ( new Date ( ) ) ;
promotionAdMapper. updatePromotionAdStatus ( promotionAd) ;
}
Web层:添加部分PromotionAdController :
@RequestMapping ( "/updatePromotionAdStatus" )
public ResponseResult updatePromotionAdStatus ( Integer id, Integer status) {
promotionAdService. updatePromotionAdStatus ( id, status) ;
ResponseResult responseResult = new ResponseResult ( true , 200 , "广告动态上下线成功" , null ) ;
return responseResult;
}
使用Postman测试接口
用户管理模块:
用户管理模块功能分析 :
实现以下功能:
用户分页和条件查询一起操作
用户状态设置
下面也与用户操作关联,但会与权限相关,所以我们说明完权限管理模块后,再在用户对应代码里进行操作:
登陆(权限管理模块)
权限控制(显示管理模块的操作,权限管理模块)
分配角色(权限管理模块)
登录页面部分显示:
下面是用户可以操作的模块权限,若没有对应权限,那么对应的模块也就不会显示出现
对应数据库表,在70章博客里有了(实际上包含了所有数据库)
表关系介绍:
用户管理模块的实现 :
用户分页和条件查询的一起的操作
返回总体数据,实际上就是先进行条件查询,然后在进行分页,与前面广告的分页不同的是,这里在查询时
即加上了条件语句,又加上了分页语句,而广告那里只加上了分页语句
需求分析
需求:实现多条件分页组合查询
具体分析:
根据分析我们知道,我们需要编写一个类,而之所以我们不去扩展PromotionAdVo类,是因为,扩展的话
虽然现在对应内存空间是占优势,但随着广告分页的请求变多,初始化也就越来越多(两个变量,变成了五个变量了)
所以创建的对象也就越大,所以需要的内存空间也就会越来越多,在长远看来,还是新创建一个类比较好
创建UserVo类:
package com. lagou. domain ;
import org. springframework. format. annotation. DateTimeFormat ;
import java. util. Date ;
public class UserVo {
private Integer currentPage;
private Integer pageSize;
private String username;
@DateTimeFormat ( pattern = "yyyy-MM-dd" )
private Date startCreateTime;
@DateTimeFormat ( pattern = "yyyy-MM-dd" )
private Date endCreateTime;
public Integer getCurrentPage ( ) {
return currentPage;
}
public void setCurrentPage ( Integer currentPage) {
this . currentPage = currentPage;
}
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;
}
public Date getStartCreateTime ( ) {
return startCreateTime;
}
public void setStartCreateTime ( Date startCreateTime) {
this . startCreateTime = startCreateTime;
}
public Date getEndCreateTime ( ) {
return endCreateTime;
}
public void setEndCreateTime ( Date endCreateTime) {
this . endCreateTime = endCreateTime;
}
}
Dao层:UserMapper:
package com. lagou. dao ;
import com. lagou. domain. User ;
import com. lagou. domain. UserVo ;
import java. util. List ;
public interface UserMapper {
public List < User > findAllUserByPage ( UserVo userVo) ;
}
对应映射配置文件(UserMapper.xml):
<?xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace = " com.lagou.dao.UserMapper" >
< select id = " findAllUserByPage" parameterType = " UserVo" resultType = " User" >
select * from user
< where>
< if test = " true" >
and is_del !=1
</ if>
< if test = " username !=null and username != ''" >
and name = #{username}
</ if>
< if test = " startCreateTime !=null and endCreateTime != null" >
and create_time between #{startCreateTime} and #{endCreateTime}
</ if>
</ where>
</ select>
</ mapper>
Service层:UserService及其实现类 :
package com. lagou. service ;
import com. github. pagehelper. PageInfo ;
import com. lagou. domain. UserVo ;
public interface UserService {
public PageInfo findAllUserByPage ( UserVo userVo) ;
}
package com. lagou. service. impl ;
import com. github. pagehelper. PageHelper ;
import com. github. pagehelper. PageInfo ;
import com. lagou. dao. UserMapper ;
import com. lagou. domain. User ;
import com. lagou. domain. UserVo ;
import com. lagou. service. UserService ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. stereotype. Service ;
import java. util. List ;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public PageInfo findAllUserByPage ( UserVo userVo) {
PageHelper . startPage ( userVo. getCurrentPage ( ) , userVo. getPageSize ( ) ) ;
List < User > allUserByPage = userMapper. findAllUserByPage ( userVo) ;
PageInfo < User > pageInfo = new PageInfo < > ( allUserByPage) ;
return pageInfo;
}
}
Web层:UserController:
package com. lagou. controller ;
import com. github. pagehelper. PageInfo ;
import com. lagou. domain. ResponseResult ;
import com. lagou. domain. UserVo ;
import com. lagou. service. UserService ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. web. bind. annotation. RequestBody ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. web. bind. annotation. RestController ;
@RestController
@RequestMapping ( "/user" )
public class UserController {
@Autowired
private UserService userService;
@RequestMapping ( "/findAllUserByPage" )
public ResponseResult findAllUserByPage ( @RequestBody UserVo userVo) {
PageInfo allUserByPage = userService. findAllUserByPage ( userVo) ;
ResponseResult responseResult = new ResponseResult ( true , 200 , "分页多条件查询成功" ,
allUserByPage) ;
return responseResult;
}
}
使用Postman测试接口
用户状态设置:
通过上面的编写,若可以的话,可以不看后面代码,自己进行一次用户状态设置的修改代码的编写
需求分析
点击禁用,实现用户的状态变更
Dao层:添加部分UserMapper:
public void updateUserStatus ( User user) ;
添加部分UserMapper.xml:
< update id = " updateUserStatus" parameterType = " User" >
update user set status = #{status},update_time = #{update_time} where id = #{id}
</ update>
Service层:添加部分UserService及其实现类 :
public void updateUserStatus ( Integer id, String status) ;
@Override
public void updateUserStatus ( Integer id, String status) {
User user = new User ( ) ;
user. setId ( id) ;
user. setStatus ( status) ;
user. setUpdate_time ( new Date ( ) ) ;
userMapper. updateUserStatus ( user) ;
}
Web层:添加部分UserController:
@RequestMapping ( "/updateUserStatus" )
public ResponseResult updateUserStatus ( Integer id, String status) {
userService. updateUserStatus ( id, status) ;
ResponseResult responseResult = new ResponseResult ( true , 200 , "用户状态修改成功" , status) ;
return responseResult;
}
使用Postman测试接口