二、逆向生成增删改查
2.1.导入相关util类
①StringUtils.java
package com.csdn.xw.util; public class StringUtils { // 私有的构造方法,保护此类不能在外部实例化 private StringUtils() { } /** * 如果字符串等于null或去空格后等于"",则返回true,否则返回false * * @param s * @return */ public static boolean isBlank(String s) { boolean b = false; if (null == s || s.trim().equals("")) { b = true; } return b; } /** * 如果字符串不等于null或去空格后不等于"",则返回true,否则返回false * * @param s * @return */ public static boolean isNotBlank(String s) { return !isBlank(s); } }
②PageBean.java
package com.csdn.xw.util; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.Map; /** * @author Java方文山 * @compay csdn_Java方文山 * @create 2023-09-07-16:28 */ public class PageBean { private int page = 1;// 页码 private int rows = 10;// 页大小 private int total = 0;// 总记录数 private boolean pagination = true;// 是否分页 private String url; //保存上一次请求的URL private Map<String,String[]> paramMap = new HashMap<>();// 保存上一次请求的参数 /** * 初始化pagebean的,保存上一次请求的重要参数 * @param req */ public void setRequest(HttpServletRequest req) { // 1.1 需要保存上一次请求的URL this.setUrl(req.getRequestURL().toString()); // 1.2 需要保存上一次请求的参数 bname、price this.setParamMap(req.getParameterMap()); // 1.3 需要保存上一次请求的分页设置 pagination this.setPagination(req.getParameter("pagination")); // 1.4 需要保存上一次请求的展示条目数 this.setRows(req.getParameter("rows")); // 1.5 初始化请求的页码 page this.setPage(req.getParameter("page")); } public void setPage(String page) { if(StringUtils.isNotBlank(page)) this.setPage(Integer.valueOf(page)); } public void setRows(String rows) { if(StringUtils.isNotBlank(rows)) this.setRows(Integer.valueOf(rows)); } public void setPagination(String pagination) { // 只有在前台jsp填写了pagination=false,才代表不分页 if(StringUtils.isNotBlank(pagination)) this.setPagination(!"false".equals(pagination)); } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public Map<String, String[]> getParamMap() { return paramMap; } public void setParamMap(Map<String, String[]> paramMap) { this.paramMap = paramMap; } public PageBean() { super(); } public int getPage() { return page; } public void setPage(int page) { this.page = page; } public int getRows() { return rows; } public void setRows(int rows) { this.rows = rows; } public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } public void setTotal(String total) { this.total = Integer.parseInt(total); } public boolean isPagination() { return pagination; } public void setPagination(boolean pagination) { this.pagination = pagination; } /** * 获得起始记录的下标 * * @return */ public int getStartIndex() { return (this.page - 1) * this.rows; } /** * 最大页 * @return */ public int maxPage() { // total % rows == 0 ? total / rows : total / rows +1 return this.total % this.rows == 0 ? this.total / this.rows : this.total / this.rows + 1; } /** * 下一页 * @return */ public int nextPage() { // 如果当前页小于最大页,那就下一页为当前页+1;如果不小于,说明当前页就是最大页,那就无需+1 return this.page < this.maxPage() ? this.page + 1 : this.page; } /** * 上一页 * @return */ public int previousPage() { return this.page > 1 ? this.page - 1 : this.page; } @Override public String toString() { return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]"; } }
2.2.aspect切面层
创建一个名为aspect的包,编写一个PageAspect切面类专门来做分页。
PageAspect
package com.csdn.xw.aspect; import com.csdn.xw.util.PageBean; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; import java.util.List; @Aspect //代表当前类是切面类 @Component //代表这个是交给Spring管理 public class PageAspect { /** * *:返回值 * *..:无限包 * *Biz:以Biz结尾的接口名 * .pager:以pager方法 * 只要同时匹配上诉四个条件,就会被列为目标对象 * 上诉配置要生效,代理注释一定要打开:<aop:aspectj-autoproxy/> * @param args * @return * @throws Throwable */ @Around("execution(* *..*Biz.*Pager(..))") public Object invoke(ProceedingJoinPoint args) throws Throwable { Object[] params = args.getArgs(); PageBean pageBean = null; for (Object param : params) { if(param instanceof PageBean){ pageBean = (PageBean)param; break; } } if(pageBean != null && pageBean.isPagination()) PageHelper.startPage(pageBean.getPage(),pageBean.getRows()); Object list = args.proceed(params); if(null != pageBean && pageBean.isPagination()){ PageInfo pageInfo = new PageInfo((List) list); pageBean.setTotal(pageInfo.getTotal()+""); } return list; } }
@Around("execution(* *..*Biz.*Pager(..))")`:这是一个注解,表示这个切面将会拦截所有使用了Biz.Pager()`方法的方法,所以我们需要分页的代码在其方法后面,加上Pager并在Biz多传递一个PageBean参数即可。
2.3.Mybatis generator逆向生成
在pom.xml或者generatorConfig.xml处
右击➡Run Maven➡Plugins➡Mybatis-generator-maven-plugin➡单击mybatis-generator:generate
会在根据你配置文件中提供的路径进行逆向生成代码。
2.4.根据生成代码编写Biz层与实现类
由于我们还需要一个模糊查询的方法,所以在StudentMapper.xml中添加一个方法
<select id="selectBySnamePager" resultType="com.csdn.xw.model.Student" parameterType="com.csdn.xw.model.Student" > select <include refid="Base_Column_List" /> from t_mysql_student <if test="sname != null"> where sname like concat('%',#{sname},'%') </if> </select>
并在StudentMapper.java添加一个相对应的方法和注解@Repository
StudentBiz
public interface StudentBiz { int deleteByPrimaryKey(String sid); int insert(Student record); int insertSelective(Student record); Student selectByPrimaryKey(String sid); int updateByPrimaryKeySelective(Student record); int updateByPrimaryKey(Student record); List<Student> selectBySnamePager(Student student,PageBean PageBean); }
StudentBizImpl
package com.csdn.xw.Biz.Impl; import com.csdn.xw.Biz.StudentBiz; import com.csdn.xw.mapper.StudentMapper; import com.csdn.xw.model.Student; import com.csdn.xw.util.PageBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * @author Java方文山 * @compay csdn_Java方文山 * @create 2023-09-07-16:22 */ @Service public class StudentBizImpl implements StudentBiz { @Autowired private StudentMapper studentMapper; @Override public int deleteByPrimaryKey(String sid) { return studentMapper.deleteByPrimaryKey(sid); } @Override public int insert(Student record) { return studentMapper.insert(record); } @Override public int insertSelective(Student record) { return studentMapper.insertSelective(record); } @Override public Student selectByPrimaryKey(String sid) { return studentMapper.selectByPrimaryKey(sid); } @Override public int updateByPrimaryKeySelective(Student record) { return studentMapper.updateByPrimaryKeySelective(record); } @Override public int updateByPrimaryKey(Student record) { return studentMapper.updateByPrimaryKey(record); } @Override public List<Student> selectBySnamePager(Student student, PageBean PageBean) { return studentMapper.selectBySnamePager(student); } }