spring+springmvc+jdbc (2)
阿新 • • 發佈:2018-11-03
增刪改
- dao 層方法
//新增方法
int insertUser(User user);
//修改方法
int updateUser(User user);
//刪除方法
int deleteUser(int id);
//根據id 獲取一條資料
User findUser(int id);
- dao 層實現
@Override
public int insertUser(User user) {
String sql = "insert into user(uname,upwd,usex) value(?,?,?)" ;
int num = BaseDao.executeUpdate(sql, new Object[]{user.getUname(), user.getUpwd(),user.getUsex()});
return num;
}
@Override
public int updateUser(User user) {
String sql = "update user set uname=?,upwd=?,usex=? where uid=?";
int num = BaseDao.executeUpdate (sql, new Object[]{user.getUname(), user.getUpwd(), user.getUsex(), user.getUid()});
return num;
}
@Override
public int deleteUser(int id) {
String sql = "delete from user where uid=?";
int num = BaseDao.executeUpdate(sql, new Object[]{id});
return num;
}
@Override
public User findUser(int id) {
String sql = "select * from user where uid=?";
return (User) BaseDao.executeQuery(sql,new Object[]{id},new BeanResultHandler(User.class));
}
- service / service 實現
//新增方法
int insertUser(User user);
//修改方法
int updateUser(User user);
//刪除方法
int deleteUser(int id);
//根據id 獲取一條資料
User findUser(int id);
@Override
public int insertUser(User user) {
return userDao.insertUser(user);
}
@Override
public int updateUser(User user) {
return userDao.updateUser(user);
}
@Override
public int deleteUser(int id) {
return userDao.deleteUser(id);
}
@Override
public User findUser(int id) {
return userDao.findUser(id);
}
- UserController
/*去新增*/
@RequestMapping("toAdd")
public String toAdd(){
return "addUser";
}
/*新增*/
@RequestMapping("add")
public String add(@RequestParam(value = "uname",required = false) String uname,
@RequestParam(value = "upwd",required = false) String upwd,
@RequestParam(value = "usex",required = false) String usex){
//接下來
User user = new User();
user.setUname(uname);
user.setUpwd(upwd);
user.setUsex(Integer.parseInt(usex));
/* if (usex.equals("男")){
user.setUsex(1);
}else if (usex.equals("女")){
user.setUsex(0);
}*/
int num = userService.insertUser(user);
if (num > 0){
//重定向打破首頁
return "redirect:toPage";
}
return "toAdd";
}
/*去修改*/
@RequestMapping("toUpdate")
public String toUpdate(@RequestParam(value = "uid",required = false) String uid,Model model){
User user = userService.findUser(Integer.parseInt(uid));
model.addAttribute("user",user);
return "updateUser";
}
/*修改*/
@RequestMapping("update")
public String update(@RequestParam(value = "uname",required = false) String uname,
@RequestParam(value = "upwd",required = false) String upwd,
@RequestParam(value = "usex",required = false) String usex,
@RequestParam(value = "uid",required = false) String uid){
User user = new User();
user.setUname(uname);
user.setUpwd(upwd);
user.setUsex(Integer.parseInt(usex));
user.setUid(Integer.parseInt(uid));
int num = userService.updateUser(user);
if (num > 0){
//
return "redirect:toPage";
}
return "toUpdate";
}
//刪除
/*ajax 請求是不需要 返回值的(因為 ajax 非同步 不需要跳轉),一定要加上這個註解@ResponseBody*/
@RequestMapping("delete")
@ResponseBody
public void delete(@RequestParam(value = "uid",required = false) String uid,
HttpServletResponse response){
int num = userService.deleteUser(Integer.parseInt(uid));
}
- 分頁 於 模糊查詢
//獲取資料,第一個引數代表的是當前頁,第二個引數代表頁量
List<User> selectAllUser(PageUtils pageUtils);
//查詢總記錄數
Integer selectUserCount(PageUtils pageUtils);
@Override
public List<User> selectAllUser(PageUtils pageUtils) {
StringBuffer sb = new StringBuffer("select * from user where 1=1");
//定義一個佔位符的集合
List<Object> list = new ArrayList<Object>();
//首先判斷是否需要查詢
if (EmptyUtils.isNotEmpty(pageUtils.getQueryWord())){
//拼接sql語句
sb.append(" and uname like ?");
list.add( "%"+ pageUtils.getQueryWord() +"%");
}
//排序
//分頁查詢
if(EmptyUtils.isNotEmpty(pageUtils.getCurrentPageNo())){
sb.append(" limit ?,?");
//給佔位符賦值
list.add((pageUtils.getCurrentPageNo()-1)*pageUtils.getPageSize());
list.add(pageUtils.getPageSize());
}
return (List<User>) BaseDao.executeQuery(sb.toString(), list.toArray(), new BeanListResultHandler(User.class));
}
@Override
public Integer selectUserCount(PageUtils pageUtils) {
StringBuffer sb = new StringBuffer("select count(1) from user where 1=1");
//定義一個佔位符的集合
List<Object> list = new ArrayList<Object>();
//首先判斷是否需要查詢
if (EmptyUtils.isNotEmpty(pageUtils.getQueryWord())){
//拼接sql語句
sb.append(" and uname like ? ");
list.add( "%"+ pageUtils.getQueryWord() +"%");
}
List<User> list1 = (List<User>) BaseDao.executeQuery(sb.toString(), list.toArray(), new ArrayResultHandler());
String count = list1.get(0)+"";
return Integer.parseInt(count);
}
- 主頁面程式碼顯示 以及 分頁 ,刪除運用了ajax
<body>
<div class="box">
<a href="toAdd">增加</a>
<form action="toPage" method="post" class="box2">
<input type="text" name="queryWord">
<input type="submit" value="確認">
</form>
</div>
<table border="1" cellspacing="0" cellpadding="0" width="400" align="center">
<tr id="box">
<th>編號</th><th>姓名</th><th>密碼</th><th>性別</th><th colspan="2">功能</th>
</tr>
<c:forEach var="user" items="${pageUtils.userList}">
<tr>
<td>${user.uid}</td>
<td>${user.uname}</td>
<td>${user.upwd}</td>
<c:choose>
<c:when test="${user.usex == 1}">
<td>男</td>
</c:when>
<c:otherwise>
<td>女</td>
</c:otherwise>
</c:choose>
<td>
<a href="toUpdate?uid=${user.uid}">修改</a>
</td>
<td>
<a href="javascript:;" onclick="showInfo(this,${user.uid});" >刪除</a>
</td>
</tr>
</c:forEach>
</table>
<div style="width: 300px;margin: 10px 0 0 650px;">
<c:choose>
<c:when test="${pageUtils.currentPageNo>1}">
<a href="toPage?currentPageNo=1&queryWord=${pageUtils.queryWord}">首頁</a>
<a href="toPage?currentPageNo=${pageUtils.currentPageNo-1}&queryWord=${pageUtils.queryWord}">上一頁</a>
</c:when>
<c:otherwise>
<span style="color:#999">首頁</span>
<span style="color:#999">上一頁</span>
</c:otherwise>
</c:choose>
[${pageUtils.currentPageNo}/${pageUtils.totalPageSize}]
<c:choose>
<c:when test="${pageUtils.currentPageNo < pageUtils.totalPageSize}">
<a href="toPage?currentPageNo=${pageUtils.currentPageNo+1}&queryWord=${pageUtils.queryWord}">下一頁</a>
<a href="toPage?currentPageNo=${pageUtils.totalPageSize}&queryWord=${pageUtils.queryWord}">末頁</a>
</c:when>
<c:otherwise>
<span style="color:#999">下一頁</span>
<span style="color:#999">末頁</span>
</c:otherwise>
</c:choose>
</div>
<script type="text/javascript">
function showInfo(a,id) {
if (confirm("確認刪除此條資訊")) {
$.post("${pageContext.request.contextPath}/userController/delete",{"uid":id},function () {
$(a).parents("tr").remove();
})
}
}
</script>
</body>