Hibernate4+Spring JPA+SpringMVC+Volecity搭建web应用(三)
hibernate注解实体类示例
package cn.bdqn.smvc.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="sys_user")
public class User implements Serializable {
@Id
@GeneratedValue
private Integer id;
@Column(name="user_name",length=20)
private String name;
@Column(name="user_gender",length=2)
private String gender;
@Column(name="phone_number",length=20)
private String phoneNumber;
@Column(name="user_mail",length=50)
private String email;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
基于JPA的 UserDao封装
package cn.bdqn.smvc.dao;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;
import cn.bdqn.smvc.entity.User;
@Repository
public interface UserDao extends PagingAndSortingRepository<User, Integer> {
}
业务层UserService接口定义
package cn.bdqn.smvc.service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import cn.bdqn.smvc.entity.User;
public interface UserService {
User findUserById(int uid);
void saveOrUpdate(User user);
void deleteUser(int uid);
Page<User> findUsersByPage(Pageable pageable);
}
UserService实现类
package cn.bdqn.smvc.service.impl;
import javax.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import cn.bdqn.smvc.dao.UserDao;
import cn.bdqn.smvc.entity.User;
import cn.bdqn.smvc.service.UserService;
@Service("userService")
public class UserServiceImpl implements UserService {
@Resource
private UserDao userDao;
@Override
public User findUserById(int uid) {
return userDao.findOne(uid);
}
@Override
public void saveOrUpdate(User user) {
userDao.save(user);
}
@Override
public void deleteUser(int uid) {
userDao.delete(uid);
}
@Override
public Page<User> findUsersByPage(Pageable pageable) {
return userDao.findAll(pageable);
}
}
UserController类
package cn.bdqn.smvc.controller;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import cn.bdqn.smvc.entity.User;
import cn.bdqn.smvc.service.UserService;
@Controller
public class UserManagerController {
@Resource
private UserService userService;
@RequestMapping(value="index.shtml",method=RequestMethod.GET)
public ModelAndView index(@RequestParam(value="pIdx",defaultValue="1")Integer pageNumber){
Pageable pageable = new PageRequest(pageNumber-1, 10);
Page<User> page = userService.findUsersByPage(pageable);
Map<String, Object> data = new HashMap<String,Object>();
data.put("data", page);
ModelAndView mav = new ModelAndView("usermanager/index", data);
return mav;
//return "usermanager/index";
}
}
简单实现的一个视图
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1" cellpadding="0" cellspacing="0">
<thead>
<th>用户编号</th>
<th>用户名</th>
<th>性别</th>
<th>联系电话</th>
<th>电子邮箱</th>
</thead>
<tbody>
<c:forEach items="${data.content }" var="user">
<tr>
<td>${user.id }</td>
<td>${user.name }</td>
<td>${user.gender }</td>
<td>${user.phoneNumber }</td>
<td>${user.email }</td>
</tr>
</c:forEach>
</tbody>
<tfoot>
<td colspan="5">
当前第${data.number+1 }页
共${data.numberOfElements }条记录
总共${data.totalElements }条记录
共${data.totalPages }页
||
<a href="index.shtml?pIdx=1">首页</a>
<c:if test="${data.number==0 }">上一页</c:if>
<c:if test="${data.number>0 }"><a href="index.shtml?pIdx=${data.number+1-1 }">上一页</a></c:if>
<c:if test="${data.number+1==data.totalPages }">下一页</c:if>
<c:if test="${data.number+1<data.totalPages }"><a href="index.shtml?pIdx=${data.number+1+1 }">下一页</a></c:if>
<a href="index.shtml?pIdx=${data.totalPages }">末页</a>
</td>
</tfoot>
</table>
</body>
</html>