ssm框架的多表查询和增删查改
必须声明本文章==》http://www.cnblogs.com/zhu520/p/7883273.html
一:
1):我的运行环境
我使用myeclipse(你也可以使用eclipse),tomcat7
jar包 放在百度云,托到文章最后有链接下载即可(其实也可以根据我之前http://www.cnblogs.com/zhu520/p/7772823.html 去弄,不需要去网上下载(但是只是对myeclipse而言,eclipse还是要到网上下载的jar包的))
可以去看看孤傲苍狼写的Mybatis https://www.cnblogs.com/xdp-gacl/p/4261895.html
2):包的情况
3):配置
applicationContext.xml ==》少了很多代码
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!-- 开启注解 --> <context:annotation-config /> <!-- 自动扫描 --> <context:component-scan base-package="zhu.dao,zhu.service"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" /> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan> </beans>
jdbc.properties
driverClassName=com.mysql.jdbc.Driver url=jdbc\:mysql\://localhost\:3306/jdbc01?useUnicode\=true&characterEncoding\=utf-8 username=root password=root validationQuery=SELECT 1 # \u6570\u636e\u5e93\u914d\u7f6e jdbc.driverClass=com.mysql.jdbc.Driver jdbc.url=jdbc\:mysql\://localhost\:3306/user?useUnicode\=true&characterEncoding\=utf-8&zeroDateTimeBehavior\=convertToNull jdbc.user=root jdbc.password=root #\u5305\u6587\u4ef6\u4f4d\u7f6e targetProject=THIS4 package.model=zhu.po package.sql.mapper=zhu.mapper package.dao.mapper=zhu.dao
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 --> <context:component-scan base-package="zhu.web"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- 日期转换 必须放在<mvc:annotation-driven />前面 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean> <!-- 注解方式 --> <mvc:annotation-driven> </mvc:annotation-driven> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <!--mvc配置视图解析 --> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name="prefix" value="/WEB-INF/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--跳转的时候只用写jsp名字,不用带后缀,因为默认的后缀“.jsp”,路径为“/WEB-INF/jsp” --> </beans>
spring-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!-- ========================================数据库方言========================================= --> <!-- properties配置文件 --> <bean id="dbProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!-- 是否忽略不可解析的 --> <property name="ignoreUnresolvablePlaceholders" value="true" /> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> <!-- ========================================数据源配置========================================= --> <!-- 配置数据源,使用的是alibaba的Druid(德鲁伊)数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <!-- 初始化连接大小 --> <property name="initialSize" value="0" /> <!-- 连接池最大使用连接数量 --> <property name="maxActive" value="20" /> <!-- 连接池最大空闲 --> <property name="maxIdle" value="20" /> <!-- 连接池最小空闲 --> <property name="minIdle" value="0" /> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="60000" /> <property name="validationQuery" value="${validationQuery}" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="testWhileIdle" value="true" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="25200000" /> <!-- 打开removeAbandoned功能 --> <property name="removeAbandoned" value="true" /> <!-- 1800秒,也就是30分钟 --> <property name="removeAbandonedTimeout" value="1800" /> <!-- 关闭abanded连接时输出错误日志 --> <property name="logAbandoned" value="true" /> <!-- 监控数据库 --> <!-- <property name="filters" value="stat" /> --> <property name="filters" value="mergeStat" /> </bean> <!-- ========================================MyBatis的配置项============================== --> <!-- 配置sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 实例化sqlSessionFactory时需要使用上述配置好的数据源以及SQL映射文件 --> <property name="dataSource" ref="dataSource" /> <!-- <property name="configLocation" value="classpath:mybatis.cofig.xml" /> --> <!-- 自动扫描Mybatis的Mapper.xml文件 --> <property name="mapperLocations" value="classpath:zhu/mapper/*.xml" /> </bean> <!-- 配置扫描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 扫描zhu.dao这个包以及它的子包下的所有映射接口类 --> <property name="basePackage" value="zhu.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 拦截器方式配置事物 --> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="append*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="modify*" propagation="REQUIRED" /> <tx:method name="edit*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="repair" propagation="REQUIRED" /> <tx:method name="delAndRepair" propagation="REQUIRED" /> <tx:method name="get*" propagation="SUPPORTS" /> <tx:method name="find*" propagation="SUPPORTS" /> <tx:method name="load*" propagation="SUPPORTS" /> <tx:method name="search*" propagation="SUPPORTS" /> <tx:method name="datagrid*" propagation="SUPPORTS" /> <tx:method name="*" propagation="SUPPORTS" /> </tx:attributes> </tx:advice> <aop:config> <!-- 配置所有的Advice,使用正则表达式,配置所有类,所有返回值的方法 --> <aop:pointcut id="transactionPointcut" expression="execution(* zhu.service..*Impl.*(..))" /> <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" /> </aop:config> <!-- 配置druid监控spring jdbc --> <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor"></bean> <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype"> <property name="patterns"> <list> <value>zhu.service.*</value> </list> </property> </bean> <aop:config> <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" /> </aop:config> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml, classpath:spring-mybatis.xml</param-value> </context-param> <listener> <description>spring监听器</description> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <description>spring mvc servlet</description> <servlet-name>springMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <description>spring mvc 配置文件</description> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <init-param> <param-name>activeReverseAjaxEnabled</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app>
4):po包
EmpDeptVo.java
package zhu.po; import java.util.Date; public class EmpDeptVo { private Integer eid; //员工id private String ename; //员工编号 private int did; //部门id private boolean gende;//性别 private int age; //年龄 private String workDate;//入职时间 private String password;//密码 private String dname;//部门名称 public EmpDeptVo(){} //用来查询数据 public EmpDeptVo(Integer eid,String ename,boolean gende ,int age,String workDate,String password,int did,String dname ){ this.eid=eid; this.ename=ename; this.gende=gende; this.age=age; this.workDate=workDate; this.password=password; this.did=did; this.dname=dname; } public Integer getEid() { return eid; } public void setEid(Integer eid) { this.eid = eid; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public int getDid() { return did; } public void setDid(int did) { this.did = did; } public boolean isGende() { return gende; } public void setGende(boolean gende) { this.gende = gende; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getWorkDate() { return workDate; } public void setWorkDate(String workDate) { this.workDate = workDate; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getDname() { return dname; } public void setDname(String dname) { this.dname = dname; } }
Tbdept.java
package zhu.po; public class Tbdept { private Integer did; private String dname; public Integer getDid() { return did; } public void setDid(Integer did) { this.did = did; } public String getDname() { return dname; } public void setDname(String dname) { this.dname = dname; } }
Tbemp.java
package zhu.po; import java.util.Date; public class Tbemp { private Integer eid; private Integer did; private Integer age; private Boolean gende; private Date workdate; private String password; private String ename; public Integer getEid() { return eid; } public void setEid(Integer eid) { this.eid = eid; } public Integer getDid() { return did; } public void setDid(Integer did) { this.did = did; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Boolean getGende() { return gende; } public void setGende(Boolean gende) { this.gende = gende; } public Date getWorkdate() { return workdate; } public void setWorkdate(Date workdate) { this.workdate = workdate; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } }
5):映射文件
TbdeptMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <!--namespace命令空间,作用就是对sql进行分类化管理,理解sql隔离 注意:使用mapper代理方法开发,namepace有特殊重要的作用 --> <mapper namespace="zhu.dao.TbdeptMapper" > <resultMap id="BaseResultMap" type="zhu.po.Tbdept" > <id column="did" property="did" jdbcType="INTEGER" /> <result column="dname" property="dname" jdbcType="VARCHAR" /> </resultMap> <sql id="Base_Column_List" > did, dname </sql> </mapper>
TbempMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="zhu.dao.TbempMapper" > <resultMap id="BaseResultMap" type="zhu.po.Tbemp" > <id column="eid" property="eid" jdbcType="INTEGER" /> <result column="did" property="did" jdbcType="INTEGER" /> <result column="age" property="age" jdbcType="INTEGER" /> <result column="gende" property="gende" jdbcType="BIT" /> <result column="workDate" property="workdate" jdbcType="DATE" /> <result column="password" property="password" jdbcType="VARCHAR" /> <result column="ename" property="ename" jdbcType="VARCHAR" /> </resultMap> <sql id="Base_Column_List" > eid, did, age, gende, workDate, password, ename </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from tbemp where eid = #{eid,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from tbemp where eid = #{eid,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="zhu.po.Tbemp" > insert into tbemp (eid, did, age, gende, workDate, password, ename) values (#{eid,jdbcType=INTEGER}, #{did,jdbcType=INTEGER}, #{age,jdbcType=INTEGER}, #{gende,jdbcType=BIT}, #{workdate,jdbcType=DATE}, #{password,jdbcType=VARCHAR}, #{ename,jdbcType=VARCHAR}) </insert> <insert id="insertSelective" parameterType="zhu.po.Tbemp" > insert into tbemp <trim prefix="(" suffix=")" suffixOverrides="," > <if test="eid != null" > eid, </if> <if test="did != null" > did, </if> <if test="age != null" > age, </if> <if test="gende != null" > gende, </if> <if test="workdate != null" > workDate, </if> <if test="password != null" > password, </if> <if test="ename != null" > ename, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="eid != null" > #{eid,jdbcType=INTEGER}, </if> <if test="did != null" > #{did,jdbcType=INTEGER}, </if> <if test="age != null" > #{age,jdbcType=INTEGER}, </if> <if test="gende != null" > #{gende,jdbcType=BIT}, </if> <if test="workdate != null" > #{workdate,jdbcType=DATE}, </if> <if test="password != null" > #{password,jdbcType=VARCHAR}, </if> <if test="ename != null" > #{ename,jdbcType=VARCHAR}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="zhu.po.Tbemp" > update tbemp <set > <if test="did != null" > did = #{did,jdbcType=INTEGER}, </if> <if test="age != null" > age = #{age,jdbcType=INTEGER}, </if> <if test="gende != null" > gende = #{gende,jdbcType=BIT}, </if> <if test="workdate != null" > workDate = #{workdate,jdbcType=DATE}, </if> <if test="password != null" > password = #{password,jdbcType=VARCHAR}, </if> <if test="ename != null" > ename = #{ename,jdbcType=VARCHAR}, </if> </set> where eid = #{eid,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="zhu.po.Tbemp" > update tbemp set did = #{did,jdbcType=INTEGER}, age = #{age,jdbcType=INTEGER}, gende = #{gende,jdbcType=BIT}, workDate = #{workdate,jdbcType=DATE}, password = #{password,jdbcType=VARCHAR}, ename = #{ename,jdbcType=VARCHAR} where eid = #{eid,jdbcType=INTEGER} </update> <!-- 增加查询所有的数据 的方法--> <select id="listAll" resultType="zhu.po.EmpDeptVo"> SELECT tbdept.dname,tbemp.eid,tbemp.age, tbemp.gende,tbemp.workDate,tbemp.`password`, tbemp.ename FROM tbemp INNER JOIN tbdept ON tbdept.did = tbemp.did </select> <!--在映射文件中配置很多sql语句 --> <!-- 需求:通过id查询员工表的记录 --> <!--通过select执行数据库的查询 id:标签映射文件的sql,将sql语句封装 到mappedStatement对象中, 所以将id成为statement的id #{}表示一个占位符 parameterType:指定输入参数的类型 resultType:指定sql输出结果的所映射的java的对象类型,select指定resultType表示 将单挑记录映射成的Java对象 --> <!-- 增加通过账号查询一条数据--> <select id="getByName" resultType="zhu.po.Tbemp"> SELECT * FROM tbemp where tbemp.ename= #{ename,jdbcType=VARCHAR} </select> </mapper>
6):dao包
TbempMapper.java
package zhu.dao; import java.util.List; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; import zhu.po.EmpDeptVo; import zhu.po.Tbemp; @Repository(value = "empMapper") public interface TbempMapper { int deleteByPrimaryKey(Integer eid); int insert(Tbemp record); int insertSelective(Tbemp record); Tbemp selectByPrimaryKey(Integer eid); int updateByPrimaryKeySelective(Tbemp record); int updateByPrimaryKey(Tbemp record); List<EmpDeptVo> listAll(); Tbemp getByName(@Param("ename") String ename); }
7):service包
IEmpService.java
package zhu.service; import java.util.List; import zhu.po.EmpDeptVo; import zhu.po.Tbemp; public interface IEmpService { void save(Tbemp t); void update(Tbemp t); void delete(int id); Tbemp getByID(int id); List<EmpDeptVo> listAll(); Tbemp getByName(String name); }
EmpServiceImpl.java
package zhu.service.impl; import java.util.List; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import zhu.dao.TbempMapper; import zhu.po.EmpDeptVo; import zhu.po.Tbemp; import zhu.service.IEmpService; @Service(value="myIEmpService") public class EmpServiceImpl implements IEmpService{ @Autowired private TbempMapper empMapper; @Override public void save(Tbemp t) { // TODO Auto-generated method stub empMapper.insert(t); } @Override public void update(Tbemp t) { // TODO Auto-generated method stub empMapper.updateByPrimaryKey(t); } @Override public void delete(int id) { // TODO Auto-generated method stub empMapper.deleteByPrimaryKey(id); } @Override public Tbemp getByID(int id) { // TODO Auto-generated method stub return empMapper.selectByPrimaryKey(id); } @Override public List<EmpDeptVo> listAll() { // TODO Auto-generated method stub return empMapper.listAll(); } @Override public Tbemp getByName(String name) { // TODO Auto-generated method stub return empMapper.getByName(name); } }
8):web包
EmpAction.java
package zhu.web; import java.sql.Date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import zhu.po.EmpDeptVo; import zhu.po.Tbemp; import zhu.service.IEmpService; @Controller @RequestMapping("/EmpAction") public class EmpAction { @Autowired private IEmpService myIEmpService; // 登录账号 @RequestMapping("/getByName") public ModelAndView getByName(String ename, String password) { try { Tbemp tbemp = myIEmpService.getByName(ename); if (tbemp.getEid() != null) { return listAll(); }else{ ModelAndView mv = new ModelAndView("/register"); mv.addObject("ename", ename); return mv ; } } catch (Exception e) { ModelAndView mv = new ModelAndView("/register"); mv.addObject("ename", ename); return mv ; } } // 查询所有的数据 private ModelAndView listAll() { List<EmpDeptVo> emps = myIEmpService.listAll(); ModelAndView mv = new ModelAndView("/jsp_CRUD/login_ok1"); mv.addObject("empVo", emps); return mv; } // 新增数据 @RequestMapping("/save") public ModelAndView save(EmpDeptVo emp) { try { Tbemp tbEmp1 = new Tbemp(); tbEmp1.setAge(emp.getAge()); tbEmp1.setDid(emp.getDid()); tbEmp1.setEname(emp.getEname()); tbEmp1.setGende(emp.isGende()); // request.getParameter("workDate"); tbEmp1.setPassword(emp.getPassword()); //将字符串转换为==》Data String workdate = emp.getWorkDate(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date sqlData = new Date(sdf.parse(workdate).getTime()); tbEmp1.setWorkdate(sqlData); myIEmpService.save(tbEmp1); } catch (Exception e) { e.printStackTrace(); } return listAll(); } // 查询一条数据 @RequestMapping("/findById") public ModelAndView findById(int eid) { Tbemp tbEmp1 = myIEmpService.getByID(eid); ModelAndView mView = new ModelAndView("/jsp_CRUD/update"); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String dateString = formatter.format(tbEmp1.getWorkdate()); mView.addObject("e", tbEmp1); ////将Data转换为==》字符串 mView.addObject("edata",dateString); return mView; } // 修改数据 @RequestMapping("/update") public ModelAndView update(EmpDeptVo emp) { try { Tbemp tbEmp1 = new Tbemp(); tbEmp1.setAge(emp.getAge()); tbEmp1.setDid(emp.getDid()); tbEmp1.setEname(emp.getEname()); tbEmp1.setGende(emp.isGende()); tbEmp1.setEid(emp.getEid()); tbEmp1.setPassword(emp.getPassword()); //将字符串转换为==》Data String workdate = emp.getWorkDate(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date sqlData = new Date(sdf.parse(workdate).getTime()); tbEmp1.setWorkdate(sqlData); myIEmpService.update(tbEmp1); } catch (ParseException e) { e.printStackTrace(); } return listAll(); } // 删除 @RequestMapping("/delete") public ModelAndView delete(int eid) { myIEmpService.delete(eid); return listAll(); } }
9):jsp
login_ok1.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>所有数据</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <div align="center" > <table cellspacing="0" border="1"> <thead> <tr> <td>id</td> <td>编号</td> <td>年龄</td> <td>性别</td> <td>部门</td> <td>时间</td> <td>密码</td> <td>修改</td> <td>删除</td> </tr> </thead> <tbody> <c:forEach items="${empVo}" var="list2"> <tr> <td>${ list2.eid}</td> <td>${ list2.ename}</td> <td>${ list2.age}</td> <c:if test="${ list2.gende==false}"><td>男</td></c:if> <c:if test="${ list2.gende==true}"><td>女</td></c:if> <td>${ list2.dname}</td> <td>${ list2.workDate}</td> <td>${ list2.password}</td> <td><a href="EmpAction/findById.do?eid=${list2.eid }" >修改</a></td> <td><a href="EmpAction/delete.do?eid=${list2.eid }" >删除</a></td> </tr> </c:forEach> </tbody> </table> </div> <hr/> <div align="center" > <form action="EmpAction/save.do" method="post"> 编号:<input type="text" name="ename"/> <br/> 密码:<input type="text" name="password"/> <br/> 时间:<input type="text" name="workDate"/> <br/> 年龄:<input type="text" name="age"/> <br/> 性别:<select name="gende"> <option value="0">男</option> <option value="1">女</option> </select> <br/> 部门:<select name="did"> <option value="1">A部门</option> <option value="2">B部门</option> </select> <br/> <input type="submit" value="新增"/> </form> </div> </body> </html>
update.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'update.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <hr/> <div align="center" > <form action="EmpAction/update.do" method="post"> <input type="hidden" name="eid" value="${ e.eid}"/> 编号:<input type="text" name="ename" value="${ e.ename}"/> <br/> 密码:<input type="text" name="password" value="${ e.password}"/> <br/> 时间:<input type="text" name="workDate" value="${ edata}"/> <br/> 年龄:<input type="text" name="age" value="${ e.age}"/> <br/> 性别:<c:if test="${e.gende==false }"> <select name="gende"> <option value="0">男</option> <option value="1">女</option> </select> </c:if> <c:if test="${e.gende==true }"> <select name="gende"> <option value="1">女</option> <option value="0">男</option> </select> </c:if> <br/> 部门:<c:if test="${e.did==1 }"> <select name="did" > <option value="1">A部门</option> <option value="2">B部门</option> </select> </c:if> <c:if test="${e.did==2 }"> <select name="did" > <option value="2">B部门</option> <option value="1">A部门</option> </select> </c:if> <br/> <input type="submit" value="修改"/> </form> </div> <!--<input type="text" name="gende" value="${e.gende==true?'女':'男' }"><br> --> </body> </html>
运行效果:
下载源码链接:http://pan.baidu.com/s/1i5vOqxf 密码:lrgo