综合练习:
1. 简单功能
1. 列表查询
2. 登录
3. 添加
4. 删除
5. 修改
2. 复杂功能
1. 删除选中
2. 分页查询
3. 复杂条件查询
添加功能:
删除功能: 
修改功能:

删除选中功能:

分页查询功能:

复杂条件查询功能

一些类的实现
FindUserByPageServlet:
- UserService userService = new UserServiceImpl();
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- request.setCharacterEncoding("utf-8");
- // System.out.println("已进入1");
- //1.获取参数
- String currentPage = request.getParameter("currentPage");//当前页码
- String rows = request.getParameter("rows");//每页显示的条数
-
- if (currentPage ==null || "".equals(currentPage) ){
- currentPage = "1";
- }
- if (rows ==null || "".equals(rows)){
- rows = "5";
- }
-
- //条件查询参数
- Map<String, String[]> condition = request.getParameterMap();
-
- PageBean<User> pb =userService.findUserBypage(currentPage,rows,condition);
-
- //存储转发
- request.setAttribute("pb",pb);
- request.setAttribute("condition",condition);
-
- request.getRequestDispatcher("/list.jsp").forward(request,response);
-
- }
-
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- this.doPost(request, response);
- }
UserServiceImpl:
- public class UserServiceImpl implements UserService {
-
- UserDao userDao = new UserDaoImpl();
- @Override
- public List<User> findAll() {
-
- return userDao.findAll();
- }
-
- @Override
- public int addUser(User user) {
- return userDao.addUder(user);
- }
-
- @Override
- public int updateUser(User user) {
- return userDao.updateUser(user);
-
- }
-
- @Override
- public User queryByid(int id) {
- return userDao.queryByid(id);
- }
-
- @Override
- public void delstlected(String[] uid) {
-
- if (uid != null && uid.length != 0){
- for (String u: uid){
- // System.out.println("u:"+u);
- userDao.deleteUser(Integer.parseInt(u));
- }
- }
-
- }
-
- @Override
- public PageBean<User> findUserBypage(String currentPage1, String rows1, Map<String, String[]> condition) {
-
- int currentPage = Integer.parseInt(currentPage1);
- int rows = Integer.parseInt(rows1);
-
- if (currentPage < 1){
- currentPage =1;
- }
-
- PageBean<User> pb = new PageBean<User>();
-
-
- pb.setRows(rows);//每页显示的个数
-
- //查询数据库中总条数
- int totalCount = userDao.findTetalCount(condition);
- pb.setTotalCount(totalCount);//总条数
-
- //开始索引
- int start = (currentPage-1) * rows;
- //查询list集合
- List<User> list = userDao.findByPage(start,rows,condition);
- pb.setList(list);//list集合
-
-
-
- int totalPage = totalCount%rows == 0 ? totalCount/rows : totalCount/rows +1;
- if (currentPage >= totalPage+1){
- currentPage = totalPage;
- }
-
- pb.setTotalPage(totalPage);//总页码数
-
- pb.setCurrentPage(currentPage);//当前页数
-
-
-
- return pb;
- }
- }
UserDaoImpl:
- public class UserDaoImpl implements UserDao {
-
- private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
-
- @Override
- public List<User> findAll() {
- List<User> users = null;
- try {
- String sql = "select * from user";
- users = template.query(sql, new BeanPropertyRowMapper<User>(User.class));
- return users;
- }catch (Exception e){
- return users;
- }
- }
-
- @Override
- public User login(User user) {
- try {
- String sql = "select * from user where username=?&&password=?";
- User user1 = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), user.getUsername(), user.getPassword());
- return user1;
- }catch (Exception e){
- return null;
- }
-
- }
-
- @Override
- public int addUder(User user) {
- try{
- String sql = "insert into user values(null,?,?,?,?,?,?,null,null)";
- int update = template.update(sql, user.getName(), user.getGender(), user.getAge(), user.getAddress(), user.getQq(), user.getEmail());
- return update;
- }catch (Exception e){
- return 0;
- }
- }
-
- @Override
- public int deleteUser(int id) {
- try{
- String sql ="delete from user where id=? ";
- int update = template.update(sql, id);
- return update;
- }catch (Exception e){
- return 0;
- }
- }
-
- @Override
- public User queryByid(int id) {
- try{
- String sql ="select * from user where id=?";
- User user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), id);
- return user;
- }catch (Exception e){
- return null;
- }
- }
-
- @Override
- public int updateUser(User user) {
- try{
- String sql ="update user set gender=?,age=?,address=?,qq=?,email=? where id=?";
- int update = template.update(sql, user.getGender(), user.getAge(), user.getAddress(), user.getQq(), user.getEmail(),user.getId());
- return update;
- }catch (Exception e){
- return 0;
- }
- }
-
- @Override
- public int findTetalCount(Map<String, String[]> condition) {
- try{
- String sql = "select count(*) from user where 1 = 1 ";
- StringBuilder sb = new StringBuilder(sql);
- Set<String> strings = condition.keySet();
- List<Object> parms = new ArrayList<>();
- for (String key: strings){
-
- if ("currentPage".equals(key) || "rows".equals(key)){
- continue;
- }
-
- String value = condition.get(key)[0];
- System.out.println("value"+value);
-
- if (value != null && !"".equals(value)){
- sb.append(" and "+key+" like ? ");
- parms.add("%"+value+"%");//?条件里面的值
- }
- }
- System.out.println(sb.toString());
- System.out.println(parms);
- //parms.toArray() 转为数组
- return template.queryForObject(sb.toString(), Integer.class,parms.toArray());
- }catch (Exception e){
- return 0;
- }
- }
-
- @Override
- public List<User> findByPage(int start, int rows, Map<String, String[]> condition) {
- try{
- String sql ="select * from user where 1 = 1 ";
-
- // String sql = "select count(*) from user where 1 = 1 ";
- StringBuilder sb = new StringBuilder(sql);
- Set<String> strings = condition.keySet();
- List<Object> parms = new ArrayList<>();
- for (String key: strings){
-
- if ("currentPage".equals(key) || "rows".equals(key)){
- continue;
- }
-
- String value = condition.get(key)[0];
- System.out.println("value"+value);
-
- if (value != null && !"".equals(value)){
- sb.append(" and "+key+" like ? ");
- parms.add("%"+value+"%");//?条件里面的值
- }
- }
-
- sb.append(" limit ?,? ");
- parms.add(start);
- parms.add(rows);
-
-
- return template.query(sb.toString(), new BeanPropertyRowMapper<User>(User.class), parms.toArray());
- }catch (Exception e){
- return null;
- }
- }
- }