Springmvc+mybatis案列 2(实现员工增删改查的操作,工具myeclipse)

来源:互联网 发布:童瑶的知乎回答 编辑:程序博客网 时间:2024/04/29 08:49

加jar包





web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" 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_2_5.xsd">  <display-name></display-name>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>    <!-- springmvc前端控制器 -->  <servlet>  <servlet-name>SpringMVC</servlet-name>  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  <init-param>  <param-name>contextConfigLocation</param-name>  <param-value>classpath:applicationContext.xml</param-value>  </init-param>  </servlet>  <servlet-mapping>  <servlet-name>SpringMVC</servlet-name>  <url-pattern>*.do</url-pattern>  </servlet-mapping>    <!-- 解决中文乱码 -->  <filter>  <filter-name>encodingFilter</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>  </filter>  <filter-mapping>  <filter-name>encodingFilter</filter-name>  <url-pattern>*.do</url-pattern>  </filter-mapping></web-app>

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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="    http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd                      http://www.springframework.org/schema/context                      http://www.springframework.org/schema/context/spring-context-3.2.xsd                      http://www.springframework.org/schema/tx                     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd                    http://www.springframework.org/schema/mvc                     http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd                    http://www.springframework.org/schema/aop                     http://www.springframework.org/schema/aop/spring-aop-3.2.xsd" default-autowire="byName"><!-- 读取属性文件 --><!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="classpath:db.properties"></property></bean> --><!-- 配置DataSource数据源 --><bean id="dataSource" class=" org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/><property name="url" value="jdbc:oracle:thin:@192.168.6.25:1521:orcl"/><property name="username" value="scott"/><property name="password" value="tiger"/></bean><!--配置工厂, 创建SqlSessionFactory,同时指定数据源 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"/><property name="mapperLocations" value="classpath:com/tarena/entity/*.xml"></property></bean><!-- 配置mybatis --><bean class=" org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.tarena.dao"></property><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> --></bean><!-- 开启注解扫描 --><context:component-scan base-package="com.tarena"/><!-- 开启RequestMapping 注解 --><mvc:annotation-driven /><!-- 处理请求转发 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/emp/"/><property name="suffix" value=".jsp"/></bean></beans>  
3、Emp.java
package com.tarena.entity;import java.sql.Date;public class Emp {private Integer empno;private String ename;private String job;private Integer mgr;private Date hiredate;private Double sal;private Double comm;private Integer deptno;public Dept getDept() {return dept;}public void setDept(Dept dept) {this.dept = dept;}public Integer getEmpno() {return empno;}public void setEmpno(Integer empno) {this.empno = empno;}public String getEname() {return ename;}public void setEname(String ename) {this.ename = ename;}public String getJob() {return job;}public void setJob(String job) {this.job = job;}public Integer getMgr() {return mgr;}public void setMgr(Integer mgr) {this.mgr = mgr;}public Date getHiredate() {return hiredate;}public void setHiredate(Date hiredate) {this.hiredate = hiredate;}public Double getSal() {return sal;}public void setSal(Double sal) {this.sal = sal;}public Double getComm() {return comm;}public void setComm(Double comm) {this.comm = comm;}public Integer getDeptno() {return deptno;}public void setDeptno(Integer deptno) {this.deptno = deptno;}}
EmpDao.java

package com.tarena.dao;import java.util.List;import com.tarena.entity.Condition;import com.tarena.entity.Emp;public interface EmpDao {public List<Emp> findAll(); // 查询全部public void save(Emp emp);// 添加public Emp findById(int id);// 按id查询public void update(Emp emp);// 更新public void delete(int id);// 删除}

EmpMapper.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="com.tarena.dao.EmpDao" >    <!-- 查询全部的员工 -->  <select id="findAll" resultType="com.tarena.entity.Emp">  select * from t_emp order by empno  </select>    <!-- 保存一条员工数据 -->  <insert id="save" parameterType="com.tarena.entity.Emp">  insert into t_emp values(  emp_seq.nextval,  #{ename,jdbcType=VARCHAR},  #{job,jdbcType=VARCHAR},  #{mgr,jdbcType=NUMERIC},  #{hiredate,jdbcType=DATE},  #{sal,jdbcType=NUMERIC},  #{comm,jdbcType=NUMERIC},  #{deptno,jdbcType=NUMERIC}  )  </insert>    <!-- 根据ID查询员工 --><select id="findById" parameterType="java.lang.Integer" resultType="com.tarena.entity.Emp">select * from t_emp where empno=#{id}</select><!-- 修改 --><update id="update" parameterType="com.tarena.entity.Emp">update t_emp setename = #{ename,jdbcType=VARCHAR},job = #{job,jdbcType=VARCHAR},sal = #{sal,jdbcType=NUMERIC}where empno=#{empno,jdbcType=NUMERIC}</update><!-- 删除 --><delete id="delete" parameterType="java.lang.Integer">delete from t_emp where empno=#{id}</delete></mapper>


EmpController.java

package com.tarena.controller;import java.util.List;import javax.annotation.Resource;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import com.tarena.dao.EmpDao;import com.tarena.entity.Emp;@Controllerpublic class EmpController {@Resourceprivate EmpDao empDao;public void setEmpDao(EmpDao empDao) {this.empDao = empDao;}@RequestMapping("/findEmp.do")public String find(Model model) {List<Emp> list = empDao.findAll();model.addAttribute("emps", list);return "emp_list";}/* * 打开新页面 */@RequestMapping("/toAddEmp.do")public String toAdd(){return "add_emp";}/* * 新增保存 */@RequestMapping("/addEmp.do")public String add(Emp emp){empDao.save(emp);return "redirect:findEmp.do";}/* * 打开修改页面 */@RequestMapping("/toUpdateEmp.do")public String toUpdate(@RequestParam("id") int id,Model model){Emp emp =empDao.findById(id);model.addAttribute("emp",emp);return "update_emp";}/* * 修改保存 */@RequestMapping("/updateEmp.do")public String update(Emp emp){empDao.update(emp);return "redirect:findEmp.do";}/* * 删除 */@RequestMapping("/deleteEmp.do")public String delete(@RequestParam("id") int id){empDao.delete(id);return "redirect:findEmp.do";}}

db.properties

driver=oracle.jdbc.driver.OracleDriverurl=jdbc:oracle:thin:@192.168.6.25:1521:orclusername=scottpassword=tiger

emp_list.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><%String path = request.getContextPath();String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>My JSP 'emp_list.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"><script type="text/javascript" src="../../assets/js/jquery-1.4.4.min.js"></script><script type="text/javascript">function delete_emp(id){var str = window.confirm("are you sure ?");if(str){location.href="deleteEmp.do?id="+id;}}</script></head><body><div align="center"><input type="button" value="新增" onclick="location.href='toAddEmp.do'" ></div><table width="80%" border="1" cellpadding="2" cellspacing="0" align="center"><tr><th>EMPNO</th><th>ENAME</th><th>JOB</th><th>MGR</th><th>HIREDATE</th><th>SAL</th><th>COMM</th><th>DEPTNO</th></tr><c:forEach items="${emps}" var="emp"><tr><td>${emp.empno}</td><td>${emp.ename}</td><td>${emp.job}</td><td>${emp.mgr}</td><td>${emp.hiredate}</td><td>${emp.sal}</td><td>${emp.comm}</td><td>${emp.deptno}</td><td><input type="button" value="修改" onclick="location.href='toUpdateEmp.do?id=${emp.empno}'" /><input type="button" value="删除" onclick="delete_emp(${emp.empno});" /><!-- <script type="text/javascript">function delete_emp(id){alert(id);}</script> --></td></tr></c:forEach></table></body></html>


add_emp.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'add_emp.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>    <form action="addEmp.do" method="post">    <table width="40%" border="1" cellpadding="2" cellspacing="0" align="center" >    <tr>    <td>姓名</td>    <td><input type="text" name="ename" /></td>    </tr>    <tr>    <td>岗位</td>    <td><input type="text" name="job" /></td>    </tr>    <tr>    <td>工资</td>    <td><input type="text" name="sal" /></td>    </tr>    <tr>    <td colspan="2"><input type="submit" value="保存" /></td>    </tr>    </table>    </form>  </body></html>

update_emp.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'update_emp.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>    <form action="updateEmp.do" method="post">    <table width="40%" align="center" border="1" cellpadding="2" cellspacing="0" >    <tr>    <td>EMPNO:</td>    <td><input type="text" name="empno" value="${emp.empno }" > </td>    </tr>    <tr>    <td>姓名:</td>    <td><input type="text" name="ename" value="${emp.ename }" > </td>    </tr>    <tr>    <td>岗位:</td>    <td><input type="text" name="job" value="${emp.job }" > </td>    </tr>    <tr>    <td>工资:</td>    <td><input type="text" name="sal" value="${emp.sal }" > </td>    </tr>    <tr>    <td> <input type="submit" value="保存" > </td>    </tr>    </table>    </form>  </body></html>



0 0
原创粉丝点击