SSH 两个表全套增删改(运动员住宿管理)
0、创建如下oracle的命令
create table HOTALINFO
(
HOTALID NUMBER(10) not null,
HOTALNAME VARCHAR2(50) not null,
HOTALADDRESS VARCHAR2(100) not null
)
;
comment on table HOTALINFO
is '酒店信息表';
comment on column HOTALINFO.HOTALID
is '酒店编号';
comment on column HOTALINFO.HOTALNAME
is '酒店名称';
comment on column HOTALINFO.HOTALADDRESS
is '酒店地址';
alter table HOTALINFO
add constraint PK_HOTALID primary key (HOTALID); create table SPORTSMAN
(
SPORTID NUMBER(10) not null,
SPORTNAME VARCHAR2(50) not null,
NATIONALITY VARCHAR2(50) not null,
HOTALID NUMBER(10) not null,
INTAKETIME DATE not null,
LEAVETIME DATE not null
)
; comment on table SPORTSMAN
is '运动员信息表';
comment on column SPORTSMAN.SPORTID
is '运动员编号';
comment on column SPORTSMAN.SPORTNAME
is '运动员姓名';
comment on column SPORTSMAN.NATIONALITY
is '国籍';
comment on column SPORTSMAN.HOTALID
is '酒店编号';
comment on column SPORTSMAN.INTAKETIME
is '入住时间';
comment on column SPORTSMAN.LEAVETIME
is '离开时间'; alter table SPORTSMAN
add constraint PK_SPORTID primary key (SPORTID);
alter table SPORTSMAN
add constraint FK_HOTELID foreign key (HOTALID)
references HOTALINFO (HOTALID); -- Create sequence
create sequence SEQ_HOTALID
minvalue 1
maxvalue 999999
start with 1
increment by 1
cache 30; create sequence SEQ_SPORTID
minvalue 1
maxvalue 99999
start with 1
increment by 1
cache 30; insert into HOTALINFO (HOTALID, HOTALNAME, HOTALADDRESS)
values (SEQ_HOTALID.nextval, '帝豪酒店', '南京中央门-30');
insert into HOTALINFO (HOTALID, HOTALNAME, HOTALADDRESS)
values (SEQ_HOTALID.nextval, '格林豪泰', '南京宣武门-30');
insert into HOTALINFO (HOTALID, HOTALNAME, HOTALADDRESS)
values (SEQ_HOTALID.nextval, '7天假日', '北京中央门-30');
insert into HOTALINFO (HOTALID, HOTALNAME, HOTALADDRESS)
values (SEQ_HOTALID.nextval, '99酒店', '南京大厂门-30');
commit; insert into SPORTSMAN (SPORTID, SPORTNAME, NATIONALITY, HOTALID, INTAKETIME, LEAVETIME)
values (SEQ_SPORTID.nextval, '张三', '英国', 1, to_date('05-04-2014', 'dd-mm-yyyy'), to_date('18-04-2014', 'dd-mm-yyyy'));
insert into SPORTSMAN (SPORTID, SPORTNAME, NATIONALITY, HOTALID, INTAKETIME, LEAVETIME)
values (SEQ_SPORTID.nextval, '李四', '韩国', 1, to_date('06-04-2014', 'dd-mm-yyyy'), to_date('17-04-2014', 'dd-mm-yyyy'));
insert into SPORTSMAN (SPORTID, SPORTNAME, NATIONALITY, HOTALID, INTAKETIME, LEAVETIME)
values (SEQ_SPORTID.nextval, '王五', '日本', 1, to_date('07-04-2014', 'dd-mm-yyyy'), to_date('16-04-2014', 'dd-mm-yyyy'));
insert into SPORTSMAN (SPORTID, SPORTNAME, NATIONALITY, HOTALID, INTAKETIME, LEAVETIME)
values (SEQ_SPORTID.nextval, '马六', '中国', 2, to_date('08-04-2014', 'dd-mm-yyyy'), to_date('15-04-2014', 'dd-mm-yyyy'));
insert into SPORTSMAN (SPORTID, SPORTNAME, NATIONALITY, HOTALID, INTAKETIME, LEAVETIME)
values (SEQ_SPORTID.nextval, '小猪', '法国', 3, to_date('09-04-2014', 'dd-mm-yyyy'), to_date('14-04-2014', 'dd-mm-yyyy'));
insert into SPORTSMAN (SPORTID, SPORTNAME, NATIONALITY, HOTALID, INTAKETIME, LEAVETIME)
values (SEQ_SPORTID.nextval, '菜菜', '外国', 4, to_date('10-04-2014', 'dd-mm-yyyy'), to_date('13-04-2014', 'dd-mm-yyyy'));
insert into SPORTSMAN (SPORTID, SPORTNAME, NATIONALITY, HOTALID, INTAKETIME, LEAVETIME)
values (SEQ_SPORTID.nextval, '放大', '是的', 4, to_date('03-03-2013', 'dd-mm-yyyy'), to_date('06-05-2014', 'dd-mm-yyyy'));
insert into SPORTSMAN (SPORTID, SPORTNAME, NATIONALITY, HOTALID, INTAKETIME, LEAVETIME)
values (SEQ_SPORTID.nextval, '答复', '的', 1, to_date('03-03-2013', 'dd-mm-yyyy'), to_date('06-05-2014', 'dd-mm-yyyy'));
insert into SPORTSMAN (SPORTID, SPORTNAME, NATIONALITY, HOTALID, INTAKETIME, LEAVETIME)
values (SEQ_SPORTID.nextval, '', '', 3, to_date('05-04-2014', 'dd-mm-yyyy'), to_date('05-04-2014', 'dd-mm-yyyy'));
insert into SPORTSMAN (SPORTID, SPORTNAME, NATIONALITY, HOTALID, INTAKETIME, LEAVETIME)
values (SEQ_SPORTID.nextval, '按到', '中国', 4, to_date('03-03-2013', 'dd-mm-yyyy'), to_date('04-04-2014', 'dd-mm-yyyy'));
commit;
oracle的脚本
1、项目实现如下功能
(1)分页查询所有
(2)分页查询下一页
(3)、模糊查询
(4)、模糊分页查询下一页
(5)、修改第一条
(6)、查看修改结果
(7)、添加
(8)、查看添加后的结果
(9) 删除第二条查看删除结果
2、创建如下项目结构
3、在entity包下创建HotalInfo.java
package com.entity; import java.io.Serializable;
import java.util.HashSet;
import java.util.Set; /**
* 1.一方:酒店
* @author Holly老师
*
*/
public class HotalInfo implements Serializable{
private static final long serialVersionUID = 1L;
/**
* 酒店id
*/
private Integer hotalid;
/**
* 酒店名称
*/
private String hotalname;
/**
* 酒店地址
*/
private String hotaladdress; /**
* 一方引入set集合:一对多
*/
private Set<Sportsman> sportsmans=new HashSet<Sportsman>(); public HotalInfo() {
} public HotalInfo(String hotalname, String hotaladdress) {
this.hotalname = hotalname;
this.hotaladdress = hotaladdress;
} public HotalInfo(Integer hotalid, String hotalname, String hotaladdress) {
this.hotalid = hotalid;
this.hotalname = hotalname;
this.hotaladdress = hotaladdress;
} public HotalInfo(Integer hotalid, String hotalname, String hotaladdress,
Set<Sportsman> sportsmans) {
this.hotalid = hotalid;
this.hotalname = hotalname;
this.hotaladdress = hotaladdress;
this.sportsmans = sportsmans;
} public Integer getHotalid() {
return hotalid;
}
public void setHotalid(Integer hotalid) {
this.hotalid = hotalid;
}
public String getHotalname() {
return hotalname;
}
public void setHotalname(String hotalname) {
this.hotalname = hotalname;
}
public String getHotaladdress() {
return hotaladdress;
}
public void setHotaladdress(String hotaladdress) {
this.hotaladdress = hotaladdress;
} public Set<Sportsman> getSportsmans() {
return sportsmans;
} public void setSportsmans(Set<Sportsman> sportsmans) {
this.sportsmans = sportsmans;
} @Override
public String toString() {
return "HotalInfo [hotaladdress=" + hotaladdress + ", hotalid="
+ hotalid + ", hotalname=" + hotalname + "]";
} }
HotalInfo.java
4、在entity包下创建Sportsman.java
package com.entity; import java.util.Date; /**
* 2.多方:运动员
* @author Holly老师
*
*/
public class Sportsman {
/**
* 运动员编号
*/
private Integer sportid;
/**
* 运动员姓名
*/
private String sportname;
/**
* 国籍
*/
private String nationality;
/**
* 入住时间
*/
private Date intaketime;
/**
* 离开时间
*/
private Date leavetime; /**
* 多对一:配置对象
*/
private HotalInfo hotalinfo; public Sportsman() {
} public Sportsman(String sportname, String nationality, Date intaketime,
Date leavetime, HotalInfo hotalinfo) {
this.sportname = sportname;
this.nationality = nationality;
this.intaketime = intaketime;
this.leavetime = leavetime;
this.hotalinfo = hotalinfo;
} public Sportsman(Integer sportid, String sportname, String nationality,
Date intaketime, Date leavetime, HotalInfo hotalinfo) {
this.sportid = sportid;
this.sportname = sportname;
this.nationality = nationality;
this.intaketime = intaketime;
this.leavetime = leavetime;
this.hotalinfo = hotalinfo;
} public Integer getSportid() {
return sportid;
} public void setSportid(Integer sportid) {
this.sportid = sportid;
} public String getSportname() {
return sportname;
} public void setSportname(String sportname) {
this.sportname = sportname;
} public String getNationality() {
return nationality;
} public void setNationality(String nationality) {
this.nationality = nationality;
} public Date getIntaketime() {
return intaketime;
} public void setIntaketime(Date intaketime) {
this.intaketime = intaketime;
} public Date getLeavetime() {
return leavetime;
} public void setLeavetime(Date leavetime) {
this.leavetime = leavetime;
} public HotalInfo getHotalinfo() {
return hotalinfo;
} public void setHotalinfo(HotalInfo hotalinfo) {
this.hotalinfo = hotalinfo;
} @Override
public String toString() {
return "Sportsman [hotalinfo=" + hotalinfo + ", intaketime="
+ intaketime + ", leavetime=" + leavetime + ", nationality="
+ nationality + ", sportid=" + sportid + ", sportname="
+ sportname + "]";
} }
Sportsman.java
5、在entity包下创建HotalInfo.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<!--一方配置: name是实体类的全路径,table是数据库表名-->
<class name="com.entity.HotalInfo" lazy="false" table="HOTALINFO"> <!-- 1.主键配置 ,name是类字段名。type是数据类型,column是列名-->
<id name="hotalid" type="java.lang.Integer" column="HOTALID">
<generator class="sequence">
<!-- 主键是自增长序列 -->
<param name="sequence">SEQ_HOTALID</param>
</generator>
</id>
<!-- 2.普通属性配置 -->
<property name="hotalname" type="java.lang.String" column="HOTALNAME"/>
<property name="hotaladdress" type="java.lang.String" column="HOTALADDRESS"/> <!-- 3. 一对多配置:-->
<set name="sportsmans" table="SPORTSMAN">
<!--外键列 -->
<key column="HOTALID"></key> <!-- 集合对应的实体类路径 -->
<one-to-many class="com.entity.Sportsman"/>
</set> </class>
</hibernate-mapping>
HotalInfo.hbm.xml
6、在entity包下创建Sportsman.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<!-- 多方配置: -->
<class name="com.entity.Sportsman" table="SPORTSMAN">
<!-- 1.主键配置 -->
<id name="sportid" type="java.lang.Integer" column="SPORTID">
<!-- 主键自增长 -->
<generator class="sequence">
<param name="sequence">SEQ_SPORTID</param>
</generator>
</id>
<!-- 2.普通属性配置 -->
<property name="sportname" type="java.lang.String" column="SPORTNAME"/>
<property name="nationality" type="java.lang.String" column="NATIONALITY"/>
<property name="intaketime" type="java.util.Date" column="INTAKETIME"/>
<property name="leavetime" type="java.util.Date" column="LEAVETIME"/> <!-- 3.多对一配置 -->
<!-- name是该类对应的外键类的对象名,class为外键类的全路径,column为外键列 -->
<many-to-one name="hotalinfo" class="com.entity.HotalInfo" column="HOTALID"/>
</class>
</hibernate-mapping>
Sportsman.hbm.xml
7、在dao包下创建HotalInfoDao.java
package com.dao; import java.util.List; import com.entity.HotalInfo; /**
* 一方:酒店数据访问层接口
* @author Holly老师
*
*/
public interface HotalInfoDao {
/**
* 1.查询所有
* @return
*/
public List<HotalInfo> query();
/**
* 2.根据id查询
* @param id
* @return
*/
HotalInfo queryById(Integer id); }
HotalInfoDao.java
8、在dao包下创建SportsmanDao.java
package com.dao; import java.util.List; import com.entity.Sportsman; /**
* 多方:运动员数据访问层接口
* @author Holly老师
*
*/
public interface SportsmanDao {
/**
* 1.分页查询
* @param pageSize 页面大小
* @param pageNo 当前页
* @return 分页集合
*/
List<Sportsman> queryPage(Integer pageSize,Integer pageNo); /**
* 2.插入一个对象
* @param obj 携带数据的对象
* @return 受影响的行数
*/
int insert(Sportsman obj); /**
* 3.根据id删除
* @param id
* @return
*/
int delete(Sportsman obj); /**
* 4.修改对象
* @param obj 携带数据的对象
* @return 受影响的行数
*/
int update(Sportsman obj); /**
* 5.根据id查询
* @param id
* @return
*/
Sportsman queryById(Integer id); /**
* 6.查询总条数
*/
Integer totalCount(); /**
* 7.模糊条件分页查询
* @param pageSize 页面大小
* @param pageNo 当前页
* @param likeparam 模糊条件
* @return
*/
List<Sportsman> queryPageLike(Integer pageSize,Integer pageNo,String likeparam);
/**
* 8.模糊条件查询总条数
* @return
*/
Integer totalCountLike(String likeparam);
}
SportsmanDao.java
9、在dao.impl包下创建HotalInfoDaoImpl.java
package com.dao.impl; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.dao.HotalInfoDao;
import com.entity.HotalInfo;
/**
* 一方:酒店
* @author Holly老师
*
*/
public class HotalInfoDaoImpl extends HibernateDaoSupport implements HotalInfoDao {
/**
* 1.查询所有区域信息
*/
public List<HotalInfo> query() { List<HotalInfo> list=getHibernateTemplate().find("from HotalInfo"); return list;
}
/**
* 2.根据id查询
*/
public HotalInfo queryById(Integer id) {
//根据主键查询
HotalInfo hotalInfo=getHibernateTemplate().get(HotalInfo.class, id);
return hotalInfo;
} }
HotalInfoDaoImpl.java
10、在dao.impl包下创建SportsmanDaoImpl.java
package com.dao.impl; import java.util.List; import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.dao.SportsmanDao;
import com.entity.Sportsman;
/**
* 多方:运动员数据访问层实现类
* @author Holly老师
*
*/
public class SportsmanDaoImpl extends HibernateDaoSupport
implements SportsmanDao {
/**
* 1.删除
*/
public int delete(Sportsman obj) {
try {
getHibernateTemplate().delete(obj); return 1;
} catch (DataAccessException e) {
e.printStackTrace();
return 0;
}
}
/**
* 2.添加
*/
public int insert(Sportsman obj) {
try {
getHibernateTemplate().save(obj);
return 1;
} catch (DataAccessException e) {
e.printStackTrace();
return 0;
}
}
/**
* 3.根据id查询
*/
public Sportsman queryById(Integer id) {
try {
//根据主键id查询
Sportsman man=getHibernateTemplate().get(Sportsman.class, id);
return man;
} catch (DataAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
/**
* 4.分页查询
*/
public List<Sportsman> queryPage(Integer pageSize, Integer pageNo) {
//4.1 获取session对象
Session session=getHibernateTemplate().getSessionFactory().openSession(); //4.2 创建Query对象
Query query=session.createQuery("from Sportsman"); //4.3 查询起始行数据3-5
query.setFirstResult((pageNo-1)*pageSize); //4.4 查询结束行数据
query.setMaxResults(pageSize); //4.5 分页查询
List<Sportsman> list=query.list(); //4.6 关闭session(?) //4.7 返回集合
return list;
}
/**
* 5.模糊分页查询
* 查询 运行员的信息
*/
public List<Sportsman> queryPageLike(Integer pageSize, Integer pageNo,
String likeparam) {
//5.1 获取session对象
Session session=getHibernateTemplate().getSessionFactory().openSession(); //5.2 将string类型酒店id的类型转换为Integer 类型
Integer id=Integer.parseInt(likeparam);
// Integer id=likeparam; //5.3定义hql语句
String hql="from Sportsman a where a.hotalinfo.hotalid=:id"; //5.4 创建Query对象
Query query=session.createQuery(hql); //5.5为命名参数赋值(第一个id是sql中:id)
query.setParameter("id", id); //5.6 查询起始行
query.setFirstResult((pageNo-1)*pageSize); //5.7查询结束行
query.setMaxResults(pageSize); //5.8 模糊分页查询
List<Sportsman> list=query.list(); //5.9 关闭session(?)
//5.10 返回集合
return list;
}
/**
* 6.求出总记录数
*/
public Integer totalCount() {
//6.1 获取session对象
Session session=getHibernateTemplate().getSessionFactory().openSession(); //6.2 定义hql语句
String hql="select count(*) from Sportsman"; //6.3定义Query对象
Query query=session.createQuery(hql); //6.4使用唯一结果查询方法
String count=query.uniqueResult().toString(); //6.5 Stirng 装换为integer
Integer totalCount=Integer.parseInt(count); //6.6返回记录数
return totalCount;
}
/**
* 7.求模糊总记录条数
*/
public Integer totalCountLike(String likeparam) {
//7.1 获取session对象
Session session=getHibernateTemplate().getSessionFactory().openSession(); //7.2 定义hql语句
String hql="select count(*) from Sportsman a " +
"where a.hotalinfo.hotalid=:id"; //7.3 创建Query对象
Query query=session.createQuery(hql); //7.4 id类型转换
Integer id=Integer.parseInt(likeparam); //7.5 为命名参数赋值
query.setParameter("id", id); //7.6 查询唯一结果
String count=query.uniqueResult().toString(); //7.7 类型转换
Integer totalCount=Integer.parseInt(count); //7.8 返回记录数
return totalCount;
} /**
* 8.修改
*/
public int update(Sportsman obj) {
try {
getHibernateTemplate().update(obj);
return 1;
} catch (DataAccessException e) {
e.printStackTrace();
return 0;
}
} }
SportsmanDaoImpl.java
11、在service包下创建HotalInfoService.java
package com.service; import java.util.List; import com.entity.HotalInfo; /**
* 一方:酒店业务接口
* @author Holly老师
*
*/
public interface HotalInfoService {
/**
* 1.查询所有
* @return
*/
public List<HotalInfo> query();
/**
* 2.根据id查询
* @param id
* @return
*/
HotalInfo queryById(Integer id); }
HotalInfoService.java
12、在service包下创建SportsmanService.java
package com.service; import java.util.List; import com.entity.Sportsman; /**
* 运动员数据访问层接口
* @author Holly老师
*
*/
public interface SportsmanService {
/**
* 1.分页查询
* @param pageSize 页面大小
* @param pageNo 当前页
* @return 分页集合
*/
List<Sportsman> queryPage(Integer pageSize,Integer pageNo); /**
* 2.插入一个对象
* @param obj 携带数据的对象
* @return 受影响的行数
*/
int insert(Sportsman obj); /**
* 3.根据id删除
* @param id
* @return
*/
int delete(Sportsman obj); /**
* 4.修改对象
* @param obj 携带数据的对象
* @return 受影响的行数
*/
int update(Sportsman obj); /**
* 5.根据id查询
* @param id
* @return
*/
Sportsman queryById(Integer id); /**
* 6.查询总条数
*/
Integer totalCount(); /**
* 7.模糊条件分页查询
* @param pageSize 页面大小
* @param pageNo 当前页
* @param likeparam 模糊条件
* @return
*/
List<Sportsman> queryPageLike(Integer pageSize,Integer pageNo,String likeparam);
/**
* 8.模糊条件查询总条数
* @return
*/
Integer totalCountLike(String likeparam);
}
SportsmanService.java
13、在service.impl包下创建HotalInfoServiceImpl.java
package com.service.impl; import java.util.List; import com.dao.HotalInfoDao;
import com.entity.HotalInfo;
import com.service.HotalInfoService;
/**
* 一方:业务逻辑接口
* @author Holly老师
*
*/
public class HotalInfoServiceImpl implements HotalInfoService {
/**
* 在spring中注入数据访问
*/
private HotalInfoDao oneDao; public HotalInfoDao getOneDao() {
return oneDao;
} public void setOneDao(HotalInfoDao oneDao) {
this.oneDao = oneDao;
} /**
* 查询所有
*/
public List<HotalInfo> query() { return oneDao.query();
}
/**
* 根据id查询
*/
public HotalInfo queryById(Integer id) { return oneDao.queryById(id);
} }
HotalInfoServiceImpl.java
14、在service.impl包下创建SportsmanServiceImpl.java
package com.service.impl; import java.util.List; import com.dao.SportsmanDao;
import com.entity.Sportsman;
import com.service.SportsmanService;
/**
*多方:业务逻辑接口实现类
* @author Holly老师
*
*/
public class SportsmanServiceImpl implements SportsmanService {
/**
* Spring中注入数据访问层dao:多方
*/
private SportsmanDao manyDao; public SportsmanDao getManyDao() {
return manyDao;
} public void setManyDao(SportsmanDao manyDao) {
this.manyDao = manyDao;
}
/**
* 1.删除
*/
public int delete(Sportsman obj) {
// TODO Auto-generated method stub
return manyDao.delete(obj);
}
/**
* 2.添加
*/
public int insert(Sportsman obj) {
return manyDao.insert(obj);
}
/**
* 3.根据id查询
*/
public Sportsman queryById(Integer id) {
return manyDao.queryById(id);
}
/**
* 4.普通分页查询
*/
public List<Sportsman> queryPage(Integer pageSize, Integer pageNo) {
return manyDao.queryPage(pageSize, pageNo);
}
/**
* 5.模糊分页查询
*/
public List<Sportsman> queryPageLike(Integer pageSize, Integer pageNo,
String likeparam) {
// TODO Auto-generated method stub
return manyDao.queryPageLike(pageSize, pageNo, likeparam);
}
/**
* 6.获取总条数
*/
public Integer totalCount() {
return manyDao.totalCount();
}
/**
* 7.条件查询总条数
*/
public Integer totalCountLike(String likeparam) {
// TODO Auto-generated method stub
return manyDao.totalCountLike(likeparam);
}
/**
* 8.修改
*/
public int update(Sportsman obj) {
// TODO Auto-generated method stub
return manyDao.update(obj);
} }
SportsmanServiceImpl.java
15、在action包下创建SportsmanAction.java
package com.action; import java.util.ArrayList;
import java.util.List; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext;
import org.hibernate.mapping.Array; import com.entity.HotalInfo;
import com.entity.Sportsman;
import com.opensymphony.xwork2.ActionSupport;
import com.service.HotalInfoService;
import com.service.SportsmanService;
/**
* 多方:控制层
* @author Holly老师
*
*/
public class SportsmanAction extends ActionSupport {
/**
* 1.在Spring中注入service层对象
*/
private HotalInfoService oneService; //一方service
private SportsmanService manyService; //多方service /**
* 3.Struts 给一方对象注入数据
*/
private HotalInfo oneObj; /**
* 3.Struts 给多方对象注入数据
* @return
*/
private Sportsman manyObj; /**
* 4.查询单条
* @return
*/
public String findById(){
//6.1 获取request对象
HttpServletRequest request=ServletActionContext.getRequest(); //6.2 判断参数id是否接到
if(manyObj!=null){
Integer id=manyObj.getSportid(); //6.2.1 根据id查询
Sportsman many=manyService.queryById(id);
//6.2.2 对象放入
request.setAttribute("many", many);
return SUCCESS; }else{
System.out.println("id没有接到");
return ERROR;
}
} /**
* 5.分页查询和模糊分页查询
* @return
*/
public String likePage(){
//5.1 获取request对象
HttpServletRequest request=ServletActionContext.getRequest(); //5.2 获取pageNo参数
String no=request.getParameter("pageNo"); //5.3 转换
Integer pageNo=1;
if(no!=null){
pageNo=Integer.parseInt(no);
} //5.4 获取输入框的的条件
Integer param=0;
if(manyObj!=null){
//通过struts2注入的对象中获取值
param= manyObj.getHotalinfo().getHotalid();
System.out.println("输入框值:"+param);
} //由于后台传递的是String ,所以这里
String likeparam=param.toString(); //5.5 定义页面大小
Integer pageSize=3; //5.6 判断模糊分页和普通分页
List<Sportsman> list=new ArrayList<Sportsman>();
Integer totalCount=null;
Integer totalPage=null;
if(likeparam!=null && !likeparam.equals("0")){
//5.6.1 获取条件区域id模糊分页查询集合
list=manyService.queryPageLike(pageSize, pageNo, likeparam); for (Sportsman sportsman : list) {
System.out.println(sportsman);
}
//5.6.2 查询总条数
totalCount= manyService.totalCountLike(likeparam); //5.6.3 计算总页数
totalPage=totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1; //5.6.4 将前台需要的数据给request
request.setAttribute("list", list);
request.setAttribute("totalCount", totalCount);
request.setAttribute("totalPage", totalPage);
request.setAttribute("pageNo", pageNo);
request.setAttribute("likeparam", likeparam);
//** }else{
//5.7 查询所有分页
list=manyService.queryPage(pageSize, pageNo); //5.6.2 查询总条数
totalCount= manyService.totalCount(); //5.6.3 计算总页数
totalPage=totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1; // 获取session对象
HttpSession session=request.getSession(); //查询one放所有数据
List<HotalInfo> onelist=oneService.query();
//
session.setAttribute("onelist", onelist); //将前台需要的数据给request
request.setAttribute("list", list);
request.setAttribute("totalCount", totalCount);
request.setAttribute("totalPage", totalPage);
request.setAttribute("pageNo", pageNo);
request.setAttribute("likeparam", likeparam); }
System.out.println("likeparam:"+likeparam);
return SUCCESS;
} /**
* 6.修改
* @return
*/
public String updateMany(){
if(manyObj!=null){
HotalInfo one =oneService.queryById(manyObj.getHotalinfo().getHotalid());
System.out.println("修改one:"+one); manyObj.setHotalinfo(one);
HttpServletRequest request=ServletActionContext.getRequest();
System.out.println("修改many:"+manyObj); Integer num=manyService.update(manyObj);
return SUCCESS;
}else{
return ERROR;
} }
/**
* 7.删除
* @return
*/
public String deleteMany(){
if(manyObj!=null){
//根据多方的id重新查询整条数据=对象
Sportsman many=manyService.queryById(manyObj.getSportid());
//删除对象
Integer num=manyService.delete(many);
return SUCCESS;
}else{
return ERROR;
} }
/**
* 8.添加
*/
public String insertMany(){
//判断多方对象是否为空
if( manyObj!=null){
System.out.println("save的manyObj:"+manyObj);
//根据多方中一方对象属性的id查询,一方数据
HotalInfo one=oneService.queryById(manyObj.getHotalinfo().getHotalid()); //将查询到的一方数据赋值给多方的一方对象属性
manyObj.setHotalinfo(one); //多方的添加操作
Integer num=manyService.insert(manyObj) ;
if(num>0){
System.out.println("添加查询");
return SUCCESS;
}else{
System.out.println("添加失败");
return ERROR;
}
}else{
System.out.println("save没接到manyObj:"+manyObj);
return ERROR; }
} public HotalInfoService getOneService() {
return oneService;
}
public void setOneService(HotalInfoService oneService) {
this.oneService = oneService;
}
public SportsmanService getManyService() {
return manyService;
}
public void setManyService(SportsmanService manyService) {
this.manyService = manyService;
}
public HotalInfo getOneObj() {
return oneObj;
}
public void setOneObj(HotalInfo oneObj) {
this.oneObj = oneObj;
}
public Sportsman getManyObj() {
return manyObj;
}
public void setManyObj(Sportsman manyObj) {
this.manyObj = manyObj;
} }
SportsmanAction.java
16、在WebRoot下WEB-INF下创建applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
"> <!-- 1.定义数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>
<property name="jdbcUrl" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
<property name="user" value="ssh"/>
<property name="password" value="sys"/>
<!-- 最小连接数 -->
<property name="minPoolSize" value="1"/>
<!-- 最大连接数 -->
<property name="maxPoolSize" value="100"/>
<!-- 初始化连接数 -->
<property name="initialPoolSize" value="1"/>
<!-- 最大等待时间 -->
<property name="maxIdleTime" value="30"/>
</bean> <!-- 2.创建Session工厂 :地址session工厂bean-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 2.1 注入数据源 -->
<property name="dataSource" ref="dataSource"/> <!-- 2.2 注册引入映射的hibernate的xml文件 -->
<property name="mappingResources">
<list>
<value>com/entity/HotalInfo.hbm.xml</value>
<value>com/entity/Sportsman.hbm.xml</value>
</list>
</property> <!-- 2.3 hibernate显示sql,格式化,方言配置 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
</props>
</property>
</bean> <!-- 3.事务管理 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean> <!-- 4.dao注入session -->
<!-- id任意的 -->
<bean id="oneDao" class="com.dao.impl.HotalInfoDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="manyDao" class="com.dao.impl.SportsmanDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean> <!-- 5.service注入dao -->
<bean id="oneService" class="com.service.impl.HotalInfoServiceImpl">
<!-- name是固定的,ref的值是上一个bean的id -->
<property name="oneDao" ref="oneDao"/>
</bean>
<bean id="manyService" class="com.service.impl.SportsmanServiceImpl">
<!-- name是固定的,ref的值是上一个bean的id -->
<property name="manyDao" ref="manyDao"/>
</bean> <!-- 6.action注入service -->
<bean id="manyAction" class="com.action.SportsmanAction" scope="prototype">
<property name="oneService" ref="oneService"/>
<property name="manyService" ref="manyService"/>
</bean>
</beans>
applicationContext.xml
17、在src下创建struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "struts-2.1.7.dtd" >
<struts>
<!-- 乱码处理 -->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<package name="default" namespace="/" extends="struts-default">
<!-- 全局配置 -->
<global-results>
<result name="error">error.jsp</result>
</global-results> <!-- 注意全局配置和默认action首启项不能同时存在,并且,默认首期项,在web.xml,拦截的时候最好不要设置为以.action结尾才会起作用, -->
<!--
<default-action-ref name="page"/>
-->
<!-- name是请求的action的地址,
class是spring中注入的action的bean的id,
method是action类中对应的方法 -->
<!-- 分页查询 和模糊分页查询-->
<action name="page" class="manyAction" method="likePage">
<result name="success">index.jsp</result>
</action> <!-- 删除 -->
<action name="delete" class="manyAction" method="deleteMany">
<result name="success" type="redirectAction">page</result>
</action> <!-- 查询单条 -->
<action name="findById" class="manyAction" method="findById">
<result name="success">update.jsp</result>
</action> <!-- 修改 -->
<action name="update" class="manyAction" method="updateMany">
<result name="success" type="redirectAction">page</result>
</action> <!-- 添加 -->
<action name="save" class="manyAction" method="insertMany">
<result name="success" type="redirectAction">page</result>
</action>
</package>
</struts>
struts.xml
18、在WebRoot下WEB-INF下创建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">
<!-- -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-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>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
web.xml
19、在WebRoot下创建index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
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 'index.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>
<center>
<h3>运动员住宿管理系统</h3>
<table border="1">
<tr>
<td colspan="7">
<form action="page.action" method="post">
酒店:<select name="manyObj.hotalinfo.hotalid">
<option value="0" selected="selected">不限</option> <c:forEach var="i" items="${onelist}" >
<c:choose>
<c:when test="${likeparam eq i.hotalid}">
<option value="${i.hotalid}" selected="selected">${i.hotalname}</option>
</c:when>
<c:otherwise>
<option value="${i.hotalid}">${i.hotalname}</option>
</c:otherwise>
</c:choose>
</c:forEach> </select>
<input type="submit" value="查询"/>
<a href="add.jsp">添加运动员信息</a>
</form>
</td>
</tr>
<tr><td>序号</td><td>酒店名称</td><td>酒店地址</td>
<td>运动员姓名</td><td>入住时间</td><td>离开时间</td><td>操作</td></tr>
<c:forEach var="i" items="${list}"> <tr><td>${i.sportid}</td><td>${i.hotalinfo.hotalname}</td>
<td>${i.hotalinfo.hotaladdress}</td>
<td>${i.sportname}</td>
<td>${i.intaketime}</td>
<td>${i.leavetime}</td>
<td>
<a href="findById.action?manyObj.sportid=${i.sportid}">修改</a>
<a href="delete.action?manyObj.sportid=${i.sportid}">删除</a></td>
</tr>
</c:forEach>
<tr><td colspan="7" align="center"> <c:choose>
<c:when test="${likeparam eq 0}"> 第${pageNo}/${totalPage}页
<a href="page.action?pageNo=${pageNo}">首页</a>
<c:choose>
<c:when test="${pageNo gt 1}">
<a href="page.action?pageNo=${pageNo-1}">上一页</a>
</c:when>
<c:otherwise>
<a href="javascript:alert('已经是第一页!');">上一页</a>
</c:otherwise>
</c:choose> <c:choose>
<c:when test="${pageNo lt totalPage}">
<a href="page.action?pageNo=${pageNo+1}">下一页</a>
</c:when>
<c:otherwise>
<a href="javascript:alert('已经是最后一页!');">上一页</a>
</c:otherwise>
</c:choose> <a href="page.action?pageNo=${totalPage}">末页</a>
共${totalCount}条 </c:when> <c:otherwise> 第${pageNo}/${totalPage}页
<a href="page.action?pageNo=${pageNo}&manyObj.hotalinfo.hotalid=${likeparam}">首页</a>
<c:choose>
<c:when test="${pageNo gt 1}">
<a href="page.action?pageNo=${pageNo-1}&manyObj.hotalinfo.hotalid=${likeparam}">上一页</a>
</c:when>
<c:otherwise>
<a href="javascript:alert('已经是第一页!');">上一页</a>
</c:otherwise>
</c:choose> <c:choose>
<c:when test="${pageNo lt totalPage}">
<a href="page.action?pageNo=${pageNo+1}&manyObj.hotalinfo.hotalid=${likeparam}">下一页</a>
</c:when>
<c:otherwise>
<a href="javascript:alert('已经是最后一页!');">上一页</a>
</c:otherwise>
</c:choose> <a href="page.action?pageNo=${totalPage}&manyObj.hotalinfo.hotalid=${likeparam}">末页</a>
共${totalCount}条 </c:otherwise>
</c:choose> </td>
</tr>
</table>
</center>
</body>
</html>
index.jsp
20、在WebRoot下创建add.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
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.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>
<center>
<h3>添加运动员信息</h3>
<form action="save.action" method="post">
<table border="1">
<tr>
<td>酒店:</td>
<td>
<select name="manyObj.hotalinfo.hotalid">
<option value="0" selected="selected">不限</option>
<c:forEach var="i" items="${onelist}" >
<c:choose>
<c:when test="${likeparam eq i.hotalid}">
<option value="${i.hotalid}" selected="selected">${i.hotalname}</option>
</c:when>
<c:otherwise>
<option value="${i.hotalid}">${i.hotalname}</option>
</c:otherwise>
</c:choose>
</c:forEach>
</select>
</td>
</tr>
<tr><td>运动员姓名:</td>
<td><input type="text" name="manyObj.sportname"/></td>
</tr>
<tr><td>国籍:</td>
<td><input type="text" name="manyObj.nationality"/></td>
</tr>
<tr><td>入住时间:</td><td><input type="text" name="manyObj.intaketime"/></td></tr>
<tr><td>离店时间:</td><td><input type="text" name="manyObj.leavetime"/></td></tr>
<tr><td colspan="2" align="center">
<input type="submit" value="添加"/>
<input type="reset" value="取消"/>
</td></tr>
</table>
</form>
</center>
</body>
</html>
add.jsp
21、在WebRoot下创建update.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
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.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>
<center>
<h3>运动员信息维护页</h3>
<form action="update.action?manyObj.sportid=${many.sportid}" method="post">
<table border="1">
<tr>
<td>酒店:</td>
<td>
<select name="manyObj.hotalinfo.hotalid">
<option value="0" selected="selected">不限</option>
<c:forEach var="i" items="${onelist}" >
<c:choose>
<c:when test="${likeparam eq i.hotalid}">
<option value="${i.hotalid}" selected="selected">${i.hotalname}</option>
</c:when>
<c:otherwise>
<option value="${i.hotalid}">${i.hotalname}</option>
</c:otherwise>
</c:choose>
</c:forEach>
</select>
</td>
</tr>
<tr><td>运动员姓名:</td>
<td><input type="text" name="manyObj.sportname" value="${many.sportname}"/></td>
</tr>
<tr><td>国籍:</td>
<td><input type="text" name="manyObj.nationality" value="${many.nationality}"/></td>
</tr>
<tr><td>入住时间:</td><td><input type="text" name="manyObj.intaketime" value="${many.intaketime}"/></td></tr>
<tr><td>离店时间:</td><td><input type="text" name="manyObj.leavetime" value="${many.leavetime}"/></td></tr>
<tr><td colspan="2" align="center">
<input type="submit" value="更新"/>
<input type="reset" value="取消"/>
</td></tr>
</table>
</form>
</center>
</body>
</html>
update.jsp
SSH 两个表全套增删改(运动员住宿管理)的更多相关文章
- 2.SSH 两个表全套增删改(运动员住宿管理)
0.创建如下oracle的命令 create table HOTALINFO ( HOTALID ) not null, HOTALNAME ) not null, HOTALADDRESS ) no ...
- 2.Mybatis入门程序(单表的增删改成)
这里讲的单表的增删改查,是由mapper代理的增删改查,先来看看步骤: 1.jar包的导入 2.配置全局的配置文件 3.建立接口 4.编写mapper.xml 5.测试 工程结构:这个你们自己可以调整 ...
- 1.MyBaits无代理全套增删改
一.mybatis使用的准备工作 1.找到mybatis所需要的jar文件: mybatis-3.2.3.jar mybatis-spring-1.2.1.jar 2.解压mybatis-3.2.3. ...
- python全栈开发day61-django简单的出版社网站展示,添加,删除,编辑(单表的增删改查)
day61 django内容回顾: 1. 下载: pip install django==1.11.14 pip install -i 源 django==1.11.14 pycharm 2. 创建项 ...
- MYSQL数据类型 表基本操作 表记录增删改 单表查询
一.数据类型 常用的数据类型如下: 整数:int,bit 小数:decimal 字符串:varchar,char 日期时间: date, time, datetime 枚举类型(enum) 特别说明的 ...
- Hibernate5笔记2--单表的增删改查操作
单表的增删改查操作: (1)定义获取Session和SessionFactory的工具类: package com.tongji.utils; import org.hibernate.Session ...
- django模型层 关于单表的增删改查
关于ORM MTV或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库, 通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人员 ...
- MySQL数据库之表的增删改查
目录 MySQL数据库之表的增删改查 1 引言 2 创建表 3 删除表 4 修改表 5 查看表 6 复制表 MySQL数据库之表的增删改查 1 引言 1.MySQL数据库中,数据库database就是 ...
- Django学习笔记(10)——Book单表的增删改查页面
一,项目题目:Book单表的增删改查页面 该项目主要练习使用Django开发一个Book单表的增删改查页面,通过这个项目巩固自己这段时间学习Django知识. 二,项目需求: 开发一个简单的Book增 ...
随机推荐
- 国内首家MR头显公司于CES惊艳亮相
在刚刚过去的CES2017大会上,我们看到了许多较为优秀的VR产品,而在这里面,有一家名不见经传的中国公司易瞳发布了一款兼具VR和AR功能的头显VMG-MARK.它的外观与联想VR和骁龙VR820等产 ...
- 将Cygwin Emacs设为Windows explorer默认打开程序
由于我在平日的学习与工作中会经常用到Cygwin中的Emacs,很自然地想到应该将emacsclient作为指定文件类型在Windows explorer中的默认打开程序.这样,便可以直接双击文件后在 ...
- python1 tkinter
turtle 图形: turtle.color(c) 设置笔的颜色turtle.fillcolor(c) 设置笔填充颜色turtle.begin_fill() 在填充颜色前访问这个方法turtle ...
- 【IE6的疯狂之十二】一个display:none引起的3像素的BUG
今天同事给我看了一个display:none引起的3像素的BUG,非常奇怪!从来没碰到过display:none还能引起这种bug. 看代码: <div style="width: ...
- OpenGL编译问题随手记
1.error C2381: "exit" : 重定义:__declspec(noreturn) 不同 编译OpenGL Red Book 的例子时出现错误, stdl ...
- 在MacOS下Python安装lxml报错xmlversion.h not found 报错的解决方案
最近在看一个自动化测试框架的问题,需要用到Lxml库,下载lxml总是报错. 1,使用pip安装lxml pip install lxml 2,然后报错了,报错内容是: In file include ...
- kali客户端攻击
浏览器攻击 browser_autpwn2 (BAP2) mkdir /test 为接受响应的服务器创建目录 use auxiliary/server/browser_autopwn2 set ...
- 关于数据汇总方面返回Json数据的小小心得
在一开始的开发中,计算好相关数据,然后通过 1.拼串 2.实例化Dictory对象再通过JavaScriptSerializer转换为json. 其中,2只适合于二维数据.1适合多维数据,但拼串比较费 ...
- C# ExcelHelper
using System; using System.IO; using System.Data; using System.Collections; using System.Data.OleDb; ...
- Edward's Cola Plan
Edward's Cola Plan Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu S ...