java框架完成表的查询操作_SpringMVC框架的多表查询和增删查改

一:

1):我的运行环境

我使用myeclipse(你也可以使用eclipse),tomcat7

jar包 放在百度云,托到文章最后有链接下载即可(其实也可以根据我之前http://www.cnblogs.com/zhu520/p/7772823.html 去弄,不需要去网上下载(但是只是对myeclipse而言,eclipse还是要到网上下载的jar包的))

2):包的情况

f976f78c60cc76ab8d386173243f9443.png

3):配置的文件需要applicationContext.xml和springmvc.xml,不需要struts.xml配置

applicationContext.xml

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

d02de9a66fc0fca95768cc53135bc24e.png

EmpDeptVo.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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包

6cbfa8c63fd708fd2109f8bd6b374aa5.png

TbDept.hbm.xml

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

/p>

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

TbDept.hbm.xml

TbEmp.hbm.xml

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

/p>

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

TbEmp.hbm.xml

6):dao包

22084ed3376427a96837e0ff13ac3a8b.png

IEmpDao.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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包

e4fc1c131d26518d5690137eb96627d6.png

IEmpService.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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包

25735dd9aa6315468c0164a6b3356268.png

LoginAction.java

9eff74366af78732060de91bccd439e6.png

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包

d89086a855435c8e8daf4d036c1c0d60.png

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

d4202c9cd16e9a265a4cece1f3f7ce09.png

login.jsp

Stringpath= request.getContextPath();String basePath= request.getScheme()+ "://"

+ request.getServerName() + ":" + request.getServerPort()

+ path + "/";

%>

">

登录

编号:
密码:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值