前言
在day1中,我们已经完成了基本的框架搭建,接下来继续完成controller层和前端页面
源代码项目结构图
开发步骤
DAO->Service->Controller
Result
java
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class Result {
private int code;
private String message;
private List<Object> data;
public Result() {
}
public Result(int code, String message) {
this.code = code;
this.message = message;
}
public Result(int code, String message, List<Object> data) {
this.code = code;
this.message = message;
this.data = data;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<Object> getData() {
return data;
}
public void setData(List<Object> data) {
this.data = data;
}
}
定义Result类,统一返回结果,@JsonInclude(value = JsonInclude.Include.NON_NULL)
注解的作用是,当Result类中某个属性没有值时,会忽略该属性,不进行数据传递
UserDao
开发一个项目的步骤应该是从底向上的一个过程,先写dao,在写service,最后写controller和对应的前端页面
java
public interface UserDao {
//模糊查询
List<User> getLikeUsers(Map<String,Object> user);
//模糊查询个数
Integer getLikeUsersCount(Map<String,Object> user);
//新增用户
Integer insertUser(User user);
// 修改用户
int updateUser(User user);
//删除用户
int delUser(User user);
//根据id查询指定用户
User selectUserById(User user);
}
项目所需的sql支持如上:包括模糊查询和CRUD
注意:这里的根据id查询指定用户,我传递的参数类型为对象,实际上只需要user的id即可,我这么写的目的,是为了简化开发
UserDao.xml
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.csx.dao.UserDao">
<insert id="insertUser" parameterType="User">
insert into t_user(name,age)values (#{name},#{age})
</insert>
<update id="updateUser">
update t_user set name = #{name},age = #{age}
where id = #{id}
</update>
<delete id="delUser">
delete from t_user where id = #{id}
</delete>
<!-- select * from t_user where name like concat('%',#{user.name},'%') order by id desc-->
<select id="getLikeUsers" parameterType="map" resultType="User">
select* from t_user
<where>
name like concat('%',#{user.name},'%')
<if test="user.age!=null">
and age=#{user.age}
</if>
</where>
order by id desc
</select>
<select id="getLikeUsersCount" parameterType="map" resultType="int">
select count(1) from t_user where name like concat('%',#{user.name},'%')
</select>
<select id="selectUserById" resultType="com.csx.entity.User" parameterType="User">
select * from t_user where id=#{id}
</select>
</mapper>
UserService
java
package com.csx.service;
import com.csx.entity.User;
import com.github.pagehelper.Page;
import java.util.Map;
public interface UserService {
//分页查询
Page queryLikeUsers(Map<String,Object> cond);
//新增用户
int addUser(User user);
//修改用户
int changeUser(User user);
//删除用户
int removeUser(User user);
User getUser(User user);
}
书写业务支持
UserServiceImpl
java
package com.csx.service.impl;
import com.csx.dao.UserDao;
import com.csx.entity.User;
import com.csx.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
@Service(value = "userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public Page queryLikeUsers(Map<String, Object> cond) {
Page page =new Page();
// //查询记录总数
// page.setTotal(userDao.getLikeUsersCount(cond));
// //每页大小(每页记录数)
int pageSize =Integer.parseInt(String.valueOf(cond.get("pageSize")));
// page.setPageSize(pageSize);
// //当前页
int currentPageNum =Integer.parseInt(String.valueOf(cond.get("pageNum")));
// page.setPageNum(currentPageNum);
//开启分页
page= PageHelper.startPage(currentPageNum,pageSize);
// List<User> list = userDao.getLikeUsers(cond);
userDao.getLikeUsers(cond);
return page;
}
@Override
public int addUser(User user) {
return userDao.insertUser(user);
}
@Override
public int changeUser(User user) {
return userDao.updateUser(user);
}
@Override
public int removeUser(User user) {
return userDao.delUser(user);
}
@Override
public User getUser(User user) {
return userDao.selectUserById(user);
}
}
关键点在于如何使用PageHelper分页插件完成分页功能,分页功能主要关注两个数据,一个是当前页码,另一个查询总条数(总记录数)
UserController
java
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/toList")
public ModelAndView toList(){
return new ModelAndView("list");
}
//分页查询
@RequestMapping("/getPage")
public Map queryUserByPage(int pageNum, int pageSize, User user) {
Map map = new HashMap();
map.put("pageSize", pageSize);
map.put("pageNum", pageNum);
map.put("user", user);
//调用Service
Page page = userService.queryLikeUsers(map);
map.put("page", page);
map.put("totalPage", page.getPages());
return map;
}
@RequestMapping("/add")
public Result addUser(User u){
userService.addUser(u);
return new Result(200,"success");
}
@RequestMapping("/editPage")
public ModelAndView getUser(User user) {
User u = userService.getUser(user);
System.out.println(u);
Map map = new HashMap();
map.put("data", u);
return new ModelAndView("edit", map);
}
@RequestMapping("/updateUser")
public Result updateUser(User u) {
userService.changeUser(u);
return new Result();
}
@RequestMapping("/delUser")
public Result delUser(User user) {
userService.removeUser(user);
return new Result();
}
}
因为我们使用freemarker技术,又因为我们不能直接访问ftl后缀的文件,所以需要先访问controller层,在根据ModelAndView进行页面跳转(list),然后页面通过ajax技术,异步获取后台数据。
总结
后台的工作准备完毕,现在需要完成前端页面,转到day3