Spring MVC系列:(6)添加用户的小案例
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,1、添加数据库表使用sqlplus打开数据库sqlplus scott/tiger创建emps数据表create table emps( id varchar(32) not null,
千家信息网最后更新 2025年11月07日Spring MVC系列:(6)添加用户的小案例
1、添加数据库表
使用sqlplus打开数据库
sqlplus scott/tiger
创建emps数据表
create table emps( id varchar(32) not null, username varchar(20) not null, salary number(6,2), hiredate date);
2、添加jar包
项目需要的jar包有spring-core、spring-web、spring-webmvc、oracle数据库驱动、c3p0数据库连接池、dbutils。
| jar包分类 | 具体jar包 |
| spring-core | commons-logging-1.2.jar spring-beans-3.2.5.RELEASE.jar spring-context-3.2.5.RELEASE.jar spring-core-3.2.5.RELEASE.jar spring-expression-3.2.5.RELEASE.jar |
| spring-web | spring-web-3.2.5.RELEASE.jar |
| spring-webmvc | spring-webmvc-3.2.5.RELEASE.jar |
| oracle数据库驱动 | ojdbc5.jar 位于:OracleDB\product\11.2.0\dbhome_1\jdbc\lib\ojdbc5.jar |
| c3p0数据库连接池 | c3p0-0.9.1.2.jar |
| dbutils | commons-dbutils-1.6.jar |
3、配置
添加jar包之后,要进行配置:
(1)将springmvc加入到web项目中,需要配置web.xml、springmvc.xml文件
(2)使用c3p0,需要配置c3p0-config.xml文件
web.xml
emp index.jsp springmvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springmvc.xml springmvc *.action CharacterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 CharacterEncodingFilter /*
springmvc.xml
c3p0-config.xml
jdbc:oracle:thin:@127.0.0.1:1521:orcl oracle.jdbc.driver.OracleDriver scott tiger 2 5 1 5 1000
4、工具类编写
SecurityUtils用来提供UUID,而JDBCUtils用来获取DataSource。
SecurityUtils.java
package com.rk.utils;import java.util.UUID;public class SecurityUtils { public static String getUUID() { return UUID.randomUUID().toString().replaceAll("-", ""); }}JDBCUtils.java
package com.rk.utils;import com.mchange.v2.c3p0.ComboPooledDataSource;public class JDBCUtils { /** * 去src目录下加载c3p0-config.xml配置文件 */ private static ComboPooledDataSource dataSource = new ComboPooledDataSource(); /** * 获取数据源 */ public static ComboPooledDataSource getDataSource() { return dataSource; }}5、从entity到action
Employee.java
package com.rk.entity;import java.util.Date;public class Employee { private String id; private String username; private Double salary; private Date hiredate; public Employee(){} public Employee(String id, String username, Double salary, Date hiredate) { this.id = id; this.username = username; this.salary = salary; this.hiredate = hiredate; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } }EmpDao.java
package com.rk.dao;import java.sql.Timestamp;import java.util.Date;import org.apache.commons.dbutils.QueryRunner;import org.junit.Test;import com.rk.entity.Employee;import com.rk.utils.JDBCUtils;import com.rk.utils.SecurityUtils;public class EmpDao { public void add(Employee emp) throws Exception{ QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource()); String sql = "insert into emps(id,username,salary,hiredate) values(?,?,?,?)"; Object[] params = {SecurityUtils.getUUID(),emp.getUsername(),emp.getSalary(),new Timestamp(emp.getHiredate().getTime())}; queryRunner.update(sql,params); } @Test public void run() throws Exception{ Employee emp = new Employee(); emp.setUsername("小明"); emp.setSalary(88.88); emp.setHiredate(new Date()); add(emp); }}EmpService.java
package com.rk.service;import com.rk.dao.EmpDao;import com.rk.entity.Employee;public class EmpService { private EmpDao empDao; public void setEmpDao(EmpDao empDao) { this.empDao = empDao; } public void register(Employee emp) throws Exception{ empDao.add(emp); }}EmpAction.java
package com.rk.action;import java.text.SimpleDateFormat;import java.util.Date;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.beans.propertyeditors.CustomDateEditor;import org.springframework.validation.BindException;import org.springframework.web.bind.ServletRequestDataBinder;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.AbstractCommandController;import com.rk.entity.Employee;import com.rk.service.EmpService;@SuppressWarnings("deprecation")public class EmpAction extends AbstractCommandController { //业务层 private EmpService empService; public void setEmpService(EmpService empService) { this.empService = empService; } //将表单参数封装到Employee实体中 public EmpAction(){ this.setCommandClass(Employee.class); } //自定义String->Date的转换器 @Override protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true)); } @Override protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object obj, BindException bindException) throws Exception { ModelAndView modelAndView = new ModelAndView(); Employee emp = (Employee) obj; empService.register(emp); modelAndView.addObject("message", "操作成功"); modelAndView.setViewName("success"); return modelAndView; }}6、对dao/service/action的配置
spring-emp.xml
7、JSP页面
WebRoot/jsp/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>添加员工
WebRoot/jsp/success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>添加成功 ${message }
演示
数据
数据库
配置
员工
文件
成功
项目
驱动
业务
参数
姓名
实体
工具
数据源
数据表
时间
目录
薪水
表单
转换器
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
北京专业软件开发服务品质保障
sql修改数据库最大值
软件开发技术服务费比例
sql数据库查询用户名命令
电信网络安全管理工作会议
数据服务器和客户机是如何分工的
数据库中的知识发现什么意思
国家网络安全宣传公益广告
删除服务器老数据库
两个区域比较重复数据库
谁开发了4g网络技术
服务器会翻墙吗
数据库签名包拓展名
软件开发是软件工程么
泉州市网络技术有限公司
hp服务器插上网线不能自动连接
广东哇美网络技术有限公司
绍兴软件开发驻场代办
商城属于软件开发吗
网络安全信息教育进校园
南开19春学期数据库
黑龙江龙采科技集团软件开发
河南潮流软件开发过程参考价格
综述文献检索数据库
软件开发为啥学不懂
奉贤区专业性网络技术推荐咨询
哈尔滨运鸿网络技术开发公司
径河国家网络安全基地作用
测绘软件开发包
网络安全技术什么是木马