在ssm框架下对数据库表单的增查删改操作(分页查询)
一.编写流程
配置ssm整合——创建java实体类——创建服务方法service接口——创建方法与参数的映射关系Mapper接口——创建实体类方法实现类Impl实现service接口中的方法——设置方法与数据库逻辑的Mapper.xml配置——编写逻辑控制层control类
二样例
建立公司员工库,并对员工库进行增查删改以及分页操作
1创建员工实体类、分页page类
员工类
public class Employee {
/**
* 员工测试类
*/
private int id; //编号
private String name; //姓名
private String email; //邮箱
private String phone; //电话
private Date add_date; //创建日期
private String position; //职位
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Date getAdd_date() {
return add_date;
}
public void setAdd_date(Date add_date) {
this.add_date = add_date;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", email=" + email + ", phone=" + phone + ", add_date="
+ add_date + ", position=" + position + "]";
}
}
页面类
public class Page
{
private int pageNo = 1;// 页码,默认是第一页
private int pageSize = 10;// 每页显示的记录数,默认是10
private int totalRecord;// 总记录数
private int totalPage;// 总页数
private boolean getAllRecord;// 是否获取所有记录
private List
results;// 对应的当前页记录
private Map
params = new HashMap
();// 其他的参数我们把它分装成一个Map对象
public boolean isGetAllRecord() {
return getAllRecord;
}
public void setGetAllRecord(boolean getAllRecord) {
this.getAllRecord = getAllRecord;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalRecord() {
return totalRecord;
}
public void setTotalRecord(int totalRecord) {
this.totalRecord = totalRecord;
// 在设置总页数的时候计算出对应的总页数,在下面的三目运算中加法拥有更高的优先级,所以最后可以不加括号.
int totalPage = totalRecord % pageSize == 0 ? totalRecord / pageSize
: totalRecord / pageSize + 1;
if(totalPage == 0)
totalPage = 1;
this.setTotalPage(totalPage);
if(this.pageNo <=0)
this.setPageNo(1);
if(this.pageNo > this.totalPage)
this.pageNo = this.totalPage;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List
getResults() { return results; } public void setResults(List
results) { this.results = results; } public Map
getParams() { return params; } public void setParams(Map
params) { this.params = params; } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("Page [pageNo=").append(pageNo).append(", pageSize=") .append(pageSize).append(", results=").append(results) .append(", totalPage=").append(totalPage) .append(", totalRecord=").append(totalRecord).append("]"); return builder.toString(); } }
2.创建服务方法service接口
public interface EmployeeService{
public int getListCount(String name); //获取列表所有信息数目
public List
getList (Page
page,String name);//通过姓名搜索员工
public void addEmployee(Employee employee);//添加员工
public void delEmployee(String[] idArray);//删除员工
public void updEmployee(Employee employee);//更新员工
}
3.创建方法与参数的映射关系Mapper接口
public interface AdsvEmployeeMapper {
public int getListCount(@param("name") String name);
List
getList(@param("begin") int pageNo, @param("size") int pageSize, @param("name") String name)
public void addEmployee(Employee employee);
public void delEmployee(@param("idArray") String[] idArray);
public void updEmployee(Employee employee);
}
4.创建实体类方法实现类Impl实现service接口中的方法
@Service
public class EmployImpl implements EmpolyeeServise{
@Autowired
private EmployeeMapper mapper;
public int getListCount(String name) {
return mapper.getListCount(name);
}
public List
getList(Page
page, String name) {
return mapper.getList(page.getPageNo(), page.getPageSize(), name);
}
public void addEmployee(Employee employee) {
mapper.addEmployee(employee);
}
public void delEmployee(String[] idArray) {
mapper.delEmployee(idArray);
}
public void updEmployee(Employee employee) {
mapper.updEmployee(employee);
}
}
5.设置方法与数据库逻辑的Mapper.xml配置
SELECT count(*) form employee // SQL语言操作,这里的employ代表数据库里的employee表
and name like (concat('%',#{name},'%')
SELECT
id,name,email,phone,add_date,position from employee
and name like concat(concat('%',#{name}),'%')
LIMIT #{begin},#{size}
INSERT into Employee (name,email,phone,add_date,position)value(#{name},#{email},#{phone},#{add_date},#{position})
#{id}
update employee
name=#{name},
email=#{email},
phone=#{phone},
add_date=#{add_date},
position=#{position}
id=#{id}
/**
* 员工管理控制类
*/
@Controller
public class EmployeeControl{
@Autowired
private EmpolyeeServise service;
public static AdsvReturn adsvRetrun = new AdsvReturn(); //返回消息工具类
/**
* 界面跳转至用户管理界面
*
* @param model
* @param request
* @return String
*/
@RequestMapping(value = { "admin/Employee" }, method = RequestMethod.GET) //设置路径映射
public String adsvEmployee(Model model, HttpServletRequest request) {
return "admin/module/adsvEmployee";
}
/**
* 查询用户信息
* @param name
* @param request
* @return
*/
@RequestMapping(value = { "admin/employeeList" }, method = RequestMethod.POST)
public @ResponseBody String showListPage(String name, HttpServletRequest request) {
try {
if (null != request.getParameter("page")) {
int index = Integer.parseInt(request.getParameter("page"));
int size = Integer.parseInt(request.getParameter("rows"));
Page
page = new Page
();
index = (index - 1) * size;
page.setPageNo(index);
page.setPageSize(size);
int count = service.getListCount(name);
List
list = service.getList(page, name);
Map
map = new HashMap
(); map.put("total", count); map.put("rows", list); String json = JSON.toJSONString(map); return json; } else { return null; } } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } /** * 添加用户 * * @param name * @param request * @return */ @ResponseBody @RequestMapping(value = { "admin/addEmployee" }, method = RequestMethod.POST, produces = "text/html;charset=UTF-8") public String addEmployee(AdsvEmployee employee, HttpServletRequest request) { try { //添加员工的创建时间 employee.setAdd_date(new Date()); service.addEmployee(employee); adsvRetrun.addSuccess(); return adsvRetrun.toString(); } catch (Exception e) { e.printStackTrace(); adsvRetrun.addFail(""); return adsvRetrun.toString(); } } /** * 删除用户 * * @param name * @param request * @return */ @RequestMapping(value = { "admin/delEmployee" }, method = RequestMethod.POST, produces = "text/html;charset=UTF-8") public @ResponseBody String delEmployee(String ids, HttpServletRequest request) { String[] idArray = ids.split(",", 0); try { service.delEmployee(idArray); adsvRetrun.delSuccess(); return adsvRetrun.toString(); } catch (Exception e) { e.printStackTrace(); adsvRetrun.delFail(""); return adsvRetrun.toString(); } } /** * 修改用户 * * @param name * @param request * @return */ @RequestMapping(value = { "admin/updEmployee" }, method = RequestMethod.POST, produces = "text/html;charset=UTF-8") public @ResponseBody String updEmployee(AdsvEmployee employee, HttpServletRequest request) { try { service.updEmployee(employee); adsvRetrun.updSuccess(); return adsvRetrun.toString(); } catch (Exception e) { e.printStackTrace(); adsvRetrun.updFail(""); return adsvRetrun.toString(); } } }