一:
1):我的运行环境
我使用myeclipse(你也可以使用eclipse),tomcat7
jar包 放在百度云,托到文章最后有链接下载即可(其实也可以根据我之前http://www.cnblogs.com/zhu520/p/7772823.html 去弄,不需要去网上下载(但是只是对myeclipse而言,eclipse还是要到网上下载的jar包的))
2):包的情况
3):配置的文件需要applicationContext.xml和springmvc.xml,不需要struts.xml配置
applicationContext.xml
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">
org.hibernate.dialect.MySQLDialect
update
classpath:zhu/cfg/
applicationContext.xml
web.xml
contextConfigLocation
classpath:applicationContext.xml
spring监听器
org.springframework.web.context.ContextLoaderListener
springMvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
activeReverseAjaxEnabled
true
1
springMvc
*.do
login.jsp
web。xml
springmvc.xml
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">
4):po
EmpDeptVo.java
packagezhu.po;importjava.util.Date;public classEmpDeptVo {private Integer eid; //员工id
private String ename; //员工编号
private int did; //部门id
private boolean gende;//性别
private int age; //年龄
private Date workDate;//入职时间
private String password;//密码
private String dname;//部门名称
publicEmpDeptVo(){}//用来查询数据
public EmpDeptVo(Integer eid,String ename,boolean gende ,int age,Date workDate,String password,intdid,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;
}publicInteger getEid() {returneid;
}public voidsetEid(Integer eid) {this.eid =eid;
}publicString getEname() {returnename;
}public voidsetEname(String ename) {this.ename =ename;
}public intgetDid() {returndid;
}public void setDid(intdid) {this.did =did;
}public booleanisGende() {returngende;
}public void setGende(booleangende) {this.gende =gende;
}public intgetAge() {returnage;
}public void setAge(intage) {this.age =age;
}publicDate getWorkDate() {returnworkDate;
}public voidsetWorkDate(Date workDate) {this.workDate =workDate;
}publicString getPassword() {returnpassword;
}public voidsetPassword(String password) {this.password =password;
}publicString getDname() {returndname;
}public voidsetDname(String dname) {this.dname =dname;
}
}
EmpDeptVo.java
TbDept.java
packagezhu.po;importjava.util.HashSet;importjava.util.Set;//部门表
public classTbDept {privateInteger did;privateString dname;publicInteger getDid() {returndid;
}public voidsetDid(Integer did) {this.did =did;
}publicString getDname() {returndname;
}public voidsetDname(String dname) {this.dname =dname;
}
}
TbDept.java
TbEmp.java
packagezhu.po;importjava.util.Date;//员工表
public classTbEmp {privateInteger eid;privateString ename;private intdid;private booleangende;private intage;privateDate workDate;privateString password;publicInteger getEid() {returneid;
}public voidsetEid(Integer eid) {this.eid =eid;
}publicString getEname() {returnename;
}public voidsetEname(String ename) {this.ename =ename;
}public intgetDid() {returndid;
}public void setDid(intdid) {this.did =did;
}public booleanisGende() {returngende;
}public void setGende(booleangende) {this.gende =gende;
}public intgetAge() {returnage;
}public void setAge(intage) {this.age =age;
}publicDate getWorkDate() {returnworkDate;
}public voidsetWorkDate(Date workDate) {this.workDate =workDate;
}publicString getPassword() {returnpassword;
}public voidsetPassword(String password) {this.password =password;
}
}
TbEmp.java
5):cfg包
TbDept.hbm.xml
/p>
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
TbDept.hbm.xml
TbEmp.hbm.xml
/p>
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
TbEmp.hbm.xml
6):dao包
IEmpDao.java
packagezhu.dao;importjava.util.List;importzhu.po.EmpDeptVo;importzhu.po.TbEmp;public interfaceIEmpDao {public ListfindAll();public TbEmp findDataById(intid);public booleansave(TbEmp t);public booleanupdate(TbEmp t);public boolean delete(intid);
}
IEmpDao.java
EmpDaoImpl.java
packagezhu.dao.impl;importjava.util.Date;importjava.util.List;importorg.hibernate.Query;importorg.hibernate.Session;importorg.hibernate.SessionFactory;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Repository;importorg.springframework.transaction.annotation.Transactional;importzhu.dao.IEmpDao;importzhu.po.EmpDeptVo;importzhu.po.TbEmp;
@Transactional
@Repository(value="empDao")public class EmpDaoImpl implementsIEmpDao{//自动注入
@Autowired
SessionFactory sessionFactory;boolean b=false;//getCurrentSession()这个方法用到了事物,如果不在applicationContext.xml配置设置事物就会出错
publicSession getSession(){returnsessionFactory.openSession();
}
@SuppressWarnings("unchecked")
@Overridepublic ListfindAll() {
String hql="select new zhu.po.EmpDeptVo(e.eid,e.ename,e.gende,e.age,e.workDate,e.password,d.did,d.dname) from TbEmp e ,TbDept d where e.did=d.did";
List list=getSession().createQuery(hql).list();returnlist;
}//select jdbc01.tbemp.*,jdbc01.tbdept.dname from jdbc01.tbemp left join jdbc01.tbdept on jdbc01.tbdept.did=jdbc01.tbemp.did where jdbc01.tbemp//.eid=15
@Overridepublic TbEmp findDataById(intid) {
String hql="from TbEmp where eid="+id;
Query query=getSession().createQuery(hql);
TbEmp e=(TbEmp) query.list().get(0);returne;
}
@Overridepublic booleansave(TbEmp t) {try{
getSession().save(t);b=true;
}catch(Exception e) {
}returnb;
}
@Overridepublic booleanupdate(TbEmp t) {
String hql="update TbEmp set ename=:ename,gende=:gende,age=:age,workDate=:workDate,password=:password,did=:did where eid=:eid";
Query query=getSession().createQuery(hql);
query.setParameter("ename", t.getEname());
query.setParameter("gende", t.isGende());
query.setParameter("age", t.getAge());
query.setParameter("workDate", t.getWorkDate());
query.setParameter("password", t.getPassword());
query.setParameter("did", t.getDid());
query.setParameter("eid", t.getEid());try{
query.executeUpdate();
b=true;
}catch(Exception e) {
}returnb;
}
@Overridepublic boolean delete(intid) {
String hql="delete from TbEmp where eid=:eid";
Query query=getSession().createQuery(hql);
query.setParameter("eid", id);try{
query.executeUpdate();
b=true;
}catch(Exception e) {
}returnb;
}
}
7):serviceSpring包
IEmpService.java
packagezhu.serviceSpring;importjava.util.List;importzhu.po.EmpDeptVo;importzhu.po.TbEmp;public interfaceIEmpService {
ListfindAll();
TbEmp findDataById(intid);booleansave(TbEmp t);booleanupdate(TbEmp t);boolean delete(intid);
}
IEmpService.java
IEmpService.java
packagezhu.serviceSpring.impl;importjava.util.List;importjavax.annotation.Resource;importorg.springframework.stereotype.Service;importzhu.dao.IEmpDao;importzhu.po.EmpDeptVo;importzhu.po.TbEmp;importzhu.serviceSpring.IEmpService;
@Servicepublic class EmpServiceImpl implementsIEmpService {
@Resource(name="empDao")
IEmpDao empDao;
@Overridepublic ListfindAll() {//TODO Auto-generated method stub
returnempDao.findAll();
}
@Overridepublic TbEmp findDataById(intid) {//TODO Auto-generated method stub
returnempDao.findDataById(id);
}
@Overridepublic booleansave(TbEmp t) {//TODO Auto-generated method stub
returnempDao.save(t);
}
@Overridepublic booleanupdate(TbEmp t) {//TODO Auto-generated method stub
returnempDao.update(t);
}
@Overridepublic boolean delete(intid) {//TODO Auto-generated method stub
returnempDao.delete(id);
}
}
EmpServiceImpl.java
8):webAction包
LoginAction.java
packagezhu.webAction;importjava.util.List;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.servlet.ModelAndView;importsun.print.resources.serviceui;importzhu.po.EmpDeptVo;importzhu.po.TbEmp;importzhu.serviceSpring.impl.EmpServiceImpl;//这里的
/*** 时间的新增 必须要经过处理,因为springMVC没有没办法把字符串转换成日期类型。所以需要自定义参数绑定
* 前端控制器接收到请求后,找到注解形式的处理器适配器,
*
*@paramemp
*@return
*/@Controller//这个注解必须要加
@RequestMapping("/loginAction")public classLoginAction {
@AutowiredpublicEmpServiceImpl myeEmpServiceImpl;/** @RequestMapping("/loginAction") 我们在类上面注解和方法上注解这样会更加的清晰,
* 我们在类上标示更能清晰的知道这个路径是请求这个类,并在方法上注解比较清楚的是请求哪个方法*/@RequestMapping("/login1")publicModelAndView login(String name,String password){if (name!=null) {returnlistAll();
}return new ModelAndView("/fail");
}publicModelAndView listAll(){
List list=myeEmpServiceImpl.findAll();
ModelAndView mv=new ModelAndView("/login_ok1");
mv.addObject("empVo",list);returnmv;
}//新增数据
@RequestMapping("/save")publicModelAndView save(EmpDeptVo emp){
TbEmp tbEmp=newTbEmp();
tbEmp.setAge(emp.getAge());
tbEmp.setDid(emp.getDid());
tbEmp.setEname(emp.getEname());
tbEmp.setGende( emp.isGende() );//request.getParameter("workDate");
tbEmp.setPassword(emp.getPassword());
tbEmp.setWorkDate(emp.getWorkDate());if(myeEmpServiceImpl.save(tbEmp)) {returnlistAll();
}return new ModelAndView("/fail");
}//删除
@RequestMapping("/delete")public ModelAndView delete(inteid){if(myeEmpServiceImpl.delete(eid)) {returnlistAll();
}return new ModelAndView("/fail");
}//查询一条数据
@RequestMapping("/findById")public ModelAndView findById(inteid){
TbEmp emVo=myeEmpServiceImpl.findDataById(eid);
ModelAndView mView=new ModelAndView("/update");
mView.addObject("e", emVo);returnmView;
}//修改数据
@RequestMapping("/update")publicModelAndView update(EmpDeptVo emp){
TbEmp tbEmp=newTbEmp();
tbEmp.setAge(emp.getAge());
tbEmp.setDid(emp.getDid());
tbEmp.setEname(emp.getEname());
tbEmp.setGende( emp.isGende() );
tbEmp.setEid(emp.getEid());
tbEmp.setPassword(emp.getPassword());
tbEmp.setWorkDate(emp.getWorkDate());if(myeEmpServiceImpl.update(tbEmp)) {returnlistAll();
}return new ModelAndView("/fail");
}
}
8):util包
DateConverter.java
packagezhu.util;importjava.text.SimpleDateFormat;importjava.util.Date;importorg.springframework.core.convert.converter.Converter;/*** 时间处理
*@authorXiaoZhu
**/
public class DateConverter implements Converter{
@OverridepublicDate convert(String source) {
SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd");try{returnsimpleDateFormat.parse(source);
}catch(Exception e) {
e.printStackTrace();
}return null;
}
}
9):jsp
login.jsp
Stringpath= request.getContextPath();String basePath= request.getScheme()+ "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
">
登录编号:
密码: