SpringMVC +Spring+ SpringJDBC整合实例:
首先设置dao层:
package com.offcn.dao;
import com.offcn.entity.User;
import com.offcn.utils.PageUtils;
import java.util.List;
public interface UserDao {
User selectByName(String userName);
//获取数据-第一个参数当前页,第二个参数是每页显示多少条
List selectAllList(Integer currentPageNo,Integer pageSize);
/**
*
* @return
*/
//查出总记录数
Integer selectUserCount();
/**
*
* @param user
* @return
*/
//增加的方法
Integer addUser(User user);
//删除
/**
*
* @param id
* @return
*/
Integer deleteUser(int id);
//修改
/**
*
* @param user
* @return
*/
Integer updateUser(User user);
//查询单个用户
User selectOneUser(int id);
/*----模糊查询-----*/
List selectLikeUser(PageUtils pageUtils);
//模糊查询总记录数
Integer selectLikeCount(PageUtils pageUtils);
}
UserDaoImpl实现层:
package com.offcn.dao.impl;
import com.offcn.dao.UserDao;
import com.offcn.entity.User;
import com.offcn.handle.ArrayResultHandler;
import com.offcn.handle.BeanListResultHandler;
import com.offcn.handle.BeanResultHandler;
import com.offcn.utils.BaseDao;
import com.offcn.utils.EmptyUtils;
import com.offcn.utils.PageUtils;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.List;
@Repository(“userDao”)
public class UserDaoImpl implements UserDao {
//登陆验证
@Override
public User selectByName(String userName) {
String sql = "select * from user where uname=?";
return (User) BaseDao.executeQuery(sql,new Object[]{userName}, new BeanResultHandler(User.class));
}
//查询分页
@Override
public List selectAllList(Integer currentPageNo, Integer pageSize) {
String sql="select * from user limit ?,?";
Object []objects={(currentPageNo-1)*pageSize,pageSize};
return (List) BaseDao.executeQuery(sql,objects, new BeanListResultHandler(User.class));
}
@Override
//查询总记录数
public Integer selectUserCount() {
String sql="select count(1) from user";
List list =(List) BaseDao.executeQuery(sql, null, new ArrayResultHandler());
String count= list.get(0)+"";
return Integer.parseInt(count);
}
@Override
public Integer addUser(User user) {
String sql="insert into user(uname,upwd,usex) values(?,?,?)";
return BaseDao.executeUpdate(sql,new Object[]{user.getUname(),user.getUpwd(),user.getUsex()});
}
@Override
public Integer deleteUser(int id) {
String sql="delete from user where uid=?";
return BaseDao.executeUpdate(sql,new Object[]{id});
}
@Override
public Integer updateUser(User user) {
String sql="update user set uname=?,upwd=?,usex=? where uid=?";
return BaseDao.executeUpdate(sql,new Object[]{user.getUname(),user.getUpwd(),user.getUsex(),user.getUid()});
}
@Override
public User selectOneUser(int id) {
String sql="select * from user where uid=?";
User user = (User) BaseDao.executeQuery(sql, new Object[]{id}, new BeanResultHandler(User.class));
return user;
}
/*--模糊分页查询--*/
@Override
public List selectLikeUser( PageUtils pageUtils) {
//String sql="select * from user where 1=1";
StringBuffer sb=new StringBuffer("select * from user where 1=1");
//定义一个集合来存占位符的集合
List
}
UserServiceImpl实现业务层:
package com.offcn.service.impl;
import com.offcn.dao.UserDao;
import com.offcn.entity.User;
import com.offcn.service.UserService;
import com.offcn.utils.PageUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/—注解扫描Service层并取别名—/
@Service(“userService”)
public class UserServiceImpl implements UserService {
@Resource/*注入userDao*/
private UserDao userDao;
@Override
public User selectByName(String uName, String upwd) {
User user=userDao.selectByName(uName);
if (user.getUpwd().equals(upwd)) {
return user;
}
return null;
}
@Override
public List selectAllList(Integer currentPageNo, Integer pageSize) {
return userDao.selectAllList(currentPageNo,pageSize);
}
@Override
public Integer selectUserCount() {
return userDao.selectUserCount();
}
@Override
public Integer addUser(User user) {
return userDao.addUser(user);
}
@Override
public Integer updateUser(User user) {
return userDao.updateUser(user);
}
@Override
public User selectOneUser(int id) {
return userDao.selectOneUser(id);
}
@Override
public Integer deleteUser(int id) {
return userDao.deleteUser(id);
}
@Override
public List selectLikeUser(PageUtils pageUtils) {
return userDao.selectLikeUser(pageUtils);
}
@Override
public Integer selectLikeCount(PageUtils pageUtils) {
return userDao.selectLikeCount(pageUtils);
}
}
springmvc-servlet.xml配置:
mvc:annotation-driven/
**applicationContext-jdbc.xml:配置**
修改界面.jsp
<%–
Created by IntelliJ IDEA.
User: ibm
Date: 2018/10/18
Time: 9:49
To change this template use File | Settings | File Templates.
–%>
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>
用户名:
密码:
性别:
首页.jsp文件:
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>
编号
姓名
密码
性别
${user.uid}
${user.uname}
${user.upwd}
女
男
修改
删除
首页
上一页
首页
上一页
[${pageUtils.currentPageNo}/${pageUtils.totalPageSize}]
下一页
末页
下一页
末页
模糊查询: **UserController控制层的类** package com.offcn.controller;
import com.offcn.entity.User;
import com.offcn.service.UserService;
import com.offcn.utils.EmptyUtils;
import com.offcn.utils.PageUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@Controller
@RequestMapping("/userController")
public class UserController {
@Resource
private UserService userService;
/*----提交方式--*/
@RequestMapping(value = "/login",method = RequestMethod.POST)
public String loginIn(@RequestParam(value = "uname",required = false)String uname,
@RequestParam(value = "upwd",required = false)String upwd, Model model){
System.out.println();
User user = userService.selectByName(uname, upwd);
if(user!=null){
model.addAttribute("user",user);
/*---这个代表重定向---*/
//默认转发:forward
return "redirect:toPage";
}
return "error";
}
/*跳到首页*/
@RequestMapping("toPage")
public String toPage(@RequestParam(value = "currentPageNo",required = false) String currentPageNo
,@RequestParam(value ="pageSize" ,required = false) String pageSize,
@RequestParam(value = "muname",required = false)String queryWord,
Model model){
if (currentPageNo == "" || currentPageNo == null) {
currentPageNo = "1";
}
if (pageSize == null || pageSize == "") {
pageSize = "5";
}
PageUtils pageUtils=new PageUtils();
pageUtils.setCurrentPageNo(Integer.parseInt(currentPageNo));
pageUtils.setPageSize(Integer.parseInt(pageSize));
if(EmptyUtils.isNotEmpty(queryWord)){
pageUtils.setQueryWorld(queryWord);}
//给pageUtils所有属性赋值
//userService.selectAllList(Integer.parseInt(currentPageNo),Integer.parseInt(pageSize));
//模糊查询用户并分页
List list = userService.selectLikeUser(pageUtils);
//查询总记录数
Integer countPage = userService.selectLikeCount(pageUtils);
//总页数
Integer totalPageSize = countPage % Integer.parseInt(pageSize) == 0 ?
countPage / Integer.parseInt(pageSize) : countPage / Integer.parseInt(pageSize) + 1;
//赋值
pageUtils.setTotalCount(countPage);
pageUtils.setTotalPageSize(totalPageSize);
//pageUtils.setCurrentPageNo(Integer.parseInt(currentPageNo));
pageUtils.setUserList(list);
//pageUtils.setPageSize(Integer.parseInt(pageSize));
model.addAttribute("pageUtils", pageUtils);
return "showInfo";
}
/*到增加页面*/
@RequestMapping("/toAdd")
public String toAdd(){
return "addUser";
}
/*从增加页面返回*/
@RequestMapping(value = "/add",method = RequestMethod.POST)
public String add(@RequestParam(value = "uname",required = false) String uname,
@RequestParam(value = "upwd",required = false)String upwd,
@RequestParam(value = "usex",required = true)String usex){
User user=new User();
user.setUname(uname);
user.setUpwd(upwd);
user.setUsex(Integer.parseInt(usex));
Integer integer = userService.addUser(user);
if(integer>0){
return "redirect:toPage";
}
return "toAdd";
}
/*到修改页面*/
@RequestMapping(value = "/toUpdate")
public String toUpdate(@RequestParam(value = "uid",required = true) String uid,Model model){
User user = userService.selectOneUser(Integer.parseInt(uid));
model.addAttribute("user",user);
return "updateUser";
}
/*从修改页面返回*/
@RequestMapping(value = "/update",method = RequestMethod.POST)
public String update(@RequestParam(value = "uid",required = true) String uid,
@RequestParam(value = "uname",required = true) String uname,
@RequestParam(value = "upwd",required = true) String upwd,
@RequestParam(value = "usex",required = true) String usex){
User user=new User();
user.setUsex(Integer.parseInt(usex));
user.setUid(Integer.parseInt(uid));
user.setUpwd(upwd);
user.setUname(uname);
Integer integer = userService.updateUser(user);
if(integer>0){
return "redirect:toPage";
}
return "toUpdate";
}
/*删除功能*/
@RequestMapping("deleteUser")
@ResponseBody
public void deleteUser(@RequestParam(value = "uid",required = true) String uid,
HttpServletResponse response){
Integer integer = userService.deleteUser(Integer.parseInt(uid));
}
}
PageUtils工具类:
package com.offcn.utils;
import com.offcn.entity.User;
import java.util.List;
//分页的工具类:
public class PageUtils {
//当前页码
private Integer currentPageNo;
//每页显示多少条
private Integer pageSize;
//总记录数
private Integer totalCount;
//总页数
private Integer totalPageSize;
//查询出的集合
private List userList;
//模糊查询的匹配字段
private String queryWorld;
public String getQueryWorld() {
return queryWorld;
}
public void setQueryWorld(String queryWorld) {
this.queryWorld = queryWorld;
}
public Integer getCurrentPageNo() {
return currentPageNo;
}
public void setCurrentPageNo(Integer currentPageNo) {
this.currentPageNo = currentPageNo;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getTotalCount() {
return totalCount;
}
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
}
public Integer getTotalPageSize() {
return totalPageSize;
}
public void setTotalPageSize(Integer totalPageSize) {
this.totalPageSize = totalPageSize;
}
public List getUserList() {
return userList;
}
public void setUserList(List userList) {
this.userList = userList;
}
}
实现对用户的增删改查(包括模糊查询与分页)