基于SpringMVC+SpringJDBC的用户管理系统(增删查改)

鉴于MyBatis暂时不会用,所以用刚学的SpringJDBC代替,也很简洁。以下贴出几个重要的代码。

1.UserDaoImpl数据库操作实现类

 1 package com.wxy.dao.impl;
 2 
 3 import com.wxy.pojo.User;
 4 import org.springframework.jdbc.core.BeanPropertyRowMapper;
 5 import org.springframework.jdbc.core.JdbcTemplate;
 6 import org.springframework.jdbc.core.RowMapper;
 7 import org.springframework.stereotype.Repository;
 8 import javax.annotation.Resource;
 9 import java.util.ArrayList;
10 import java.util.List;
11 
12 @Repository("userDao")
13 public class UserDaoImpl{
14     //获取JdbcTemplate实例
15     @Resource(name="jdbcTemplate")
16     private JdbcTemplate jdbcTemplate;
17     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
18         this.jdbcTemplate = jdbcTemplate;
19     }
20     //执行SQL语句的方法
21     public void execute() {
22         jdbcTemplate.execute("create table account8( id int primary key auto_increment,"
23                 + "username varchar(50)," + "balance double)");
24     }
25     //添加用户
26 
27     public void addUser(User user) {
28         String sql = "insert into user(id,name,age) value("+user.getId()+",'"+user.getName()+"',"+user.getAge()+")";
29         this.jdbcTemplate.update(sql);
30     }
31     //更新用户信息
32     public void updateUser(User user){
33         String sql =  "update user set name='"+user.getName()+"',age="+user.getAge()+" where id="+user.getId()+"";
34         this.jdbcTemplate.update(sql);
35     }
36     //删除用户
37     public void deleteUser(int id) {
38         String sql = "delete from user where id =?";
39         jdbcTemplate.update(sql,id);
40     }
41     //查询所有用户
42     public List query() {
43         String listSql = "select * from user";
44         RowMapper rowMapper = new BeanPropertyRowMapper(User.class);
45         List users = this.jdbcTemplate.query(listSql,rowMapper);
46         return users;
47     }
48 }
UserDaoImpl

2.UserController数据绑定和页面跳转

 1 package com.wxy.controller;
 2 
 3 import com.wxy.dao.impl.ServiceImpl;
 4 import com.wxy.pojo.User;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.context.ApplicationContext;
 7 import org.springframework.context.support.ClassPathXmlApplicationContext;
 8 import org.springframework.stereotype.Controller;
 9 import org.springframework.ui.Model;
10 import org.springframework.web.bind.annotation.RequestMapping;
11 import org.springframework.web.bind.annotation.RequestMethod;
12 
13 import javax.annotation.Resource;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16 import java.util.ArrayList;
17 import java.util.List;
18 
19 @Controller("userController")
20 public class UserController {
21     @Resource(name="userService")
22     ServiceImpl userService;
23     public void setUserService(ServiceImpl userService) {
24         this.userService = userService;
25     }
26 
27     //跳转添加页面
28     @RequestMapping("touseradd")
29     public String useradd(){ return "useradd"; }
30 
31     //执行添加操作,并跳转至成功页面
32     @RequestMapping("/useradd.action")
33     public String add(Model model, User user){
34         userService.addUser(user);
35         return "success";
36     }
37 
38     //跳转至修改页面
39     @RequestMapping("/touserupdate")
40     public String userupdate(){ return "userupdate"; }
41 
42     //执行修改操作
43     @RequestMapping("/userupdate.action")
44     public String update(User user){
45         userService.updateUser(user);
46         return "success";
47     }
48 
49     //跳转至查询页面
50     @RequestMapping("/touserlist")
51     public String userselect(){ return "userlist"; }
52 
53     //执行查询操作
54     @RequestMapping("/userselect.action")
55     public String query(HttpServletRequest request, HttpServletResponse response) {
56         List looklist = userService.query();
57         request.setAttribute("looklist", looklist);
58         return "forward:touserlist";
59     }
60 
61     //执行删除操作
62     @RequestMapping("/delete.action")
63     public String delete(int id){
64         userService.deleteUser(id);
65         return "forward:userselect.action";
66     }
67 }
UserController

3.userlist.jsp显示用户信息

 1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 2 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 3 
 4 
 5     用户列表
 6 
 7 
 8              9101112131415161718
19                     20212223242526272829
序号 Id Name Age 选择 操作
${j } ${lists.id } ${lists.name } ${lists.age} 修改 删除
30
31
32
33 34
35
36 37
38
39 40
userlist.jsp

4.springmvc-servlet.xml配置

 1 
 2  3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:mvc="http://www.springframework.org/schema/mvc"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans
 7        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
 8        http://www.springframework.org/schema/context
 9        http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
10     
11     
12     
13     14           class="org.springframework.jdbc.datasource.DriverManagerDataSource">
15         
16         
17         
18         
19     
20     
21     class="org.springframework.jdbc.core.JdbcTemplate">
22         
23     
24     
25     class="com.wxy.dao.impl.UserDaoImpl">
26         
27     
28     class="com.wxy.dao.impl.ServiceImpl">
29         
30     
31     class="com.wxy.controller.UserController">
32         
33     
34     
35     package="com.wxy.controller">
36     package="com.wxy.dao.impl">
37     
38     39           class="org.springframework.web.servlet.view.InternalResourceViewResolver">
40         
41         
42         
43         
44     
45 
springmvc-servlet.xml

配置方式XML和注解选用一种即可。

 

转载于:https://www.cnblogs.com/wxywxy/p/8723585.html

你可能感兴趣的:(基于SpringMVC+SpringJDBC的用户管理系统(增删查改))