博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2.SSH 两个表全套增删改(运动员住宿管理)
阅读量:4992 次
发布时间:2019-06-12

本文共 37910 字,大约阅读时间需要 126 分钟。

0、创建如下oracle的命令

1 create table HOTALINFO 2 ( 3   HOTALID      NUMBER(10) not null, 4   HOTALNAME    VARCHAR2(50) not null, 5   HOTALADDRESS VARCHAR2(100) not null 6 ) 7 ; 8 comment on table HOTALINFO 9   is '酒店信息表';10 comment on column HOTALINFO.HOTALID11   is '酒店编号';12 comment on column HOTALINFO.HOTALNAME13   is '酒店名称';14 comment on column HOTALINFO.HOTALADDRESS15   is '酒店地址';16 alter table HOTALINFO17   add constraint PK_HOTALID primary key (HOTALID);18 19 20 create table SPORTSMAN21 (22   SPORTID     NUMBER(10) not null,23   SPORTNAME   VARCHAR2(50) not null,24   NATIONALITY VARCHAR2(50) not null,25   HOTALID     NUMBER(10) not null,26   INTAKETIME  DATE not null,27   LEAVETIME   DATE not null28 )29 ;30 31 comment on table SPORTSMAN32   is '运动员信息表';33 comment on column SPORTSMAN.SPORTID34   is '运动员编号';35 comment on column SPORTSMAN.SPORTNAME36   is '运动员姓名';37 comment on column SPORTSMAN.NATIONALITY38   is '国籍';39 comment on column SPORTSMAN.HOTALID40   is '酒店编号';41 comment on column SPORTSMAN.INTAKETIME42   is '入住时间';43 comment on column SPORTSMAN.LEAVETIME44   is '离开时间';45   46 alter table SPORTSMAN47   add constraint PK_SPORTID primary key (SPORTID);48 alter table SPORTSMAN49   add constraint FK_HOTELID foreign key (HOTALID)50   references HOTALINFO (HOTALID);51   52   -- Create sequence 53 create sequence SEQ_HOTALID54 minvalue 155 maxvalue 99999956 start with 157 increment by 158 cache 30;59 60 create sequence SEQ_SPORTID61 minvalue 162 maxvalue 9999963 start with 164 increment by 165 cache 30;66 67 68 insert into HOTALINFO (HOTALID, HOTALNAME, HOTALADDRESS)69 values (SEQ_HOTALID.nextval, '帝豪酒店', '南京中央门-30');70 insert into HOTALINFO (HOTALID, HOTALNAME, HOTALADDRESS)71 values (SEQ_HOTALID.nextval, '格林豪泰', '南京宣武门-30');72 insert into HOTALINFO (HOTALID, HOTALNAME, HOTALADDRESS)73 values (SEQ_HOTALID.nextval, '7天假日', '北京中央门-30');74 insert into HOTALINFO (HOTALID, HOTALNAME, HOTALADDRESS)75 values (SEQ_HOTALID.nextval, '99酒店', '南京大厂门-30');76 commit;77 78 79 insert into SPORTSMAN (SPORTID, SPORTNAME, NATIONALITY, HOTALID, INTAKETIME, LEAVETIME)80 values (SEQ_SPORTID.nextval, '张三', '英国', 1, to_date('05-04-2014', 'dd-mm-yyyy'), to_date('18-04-2014', 'dd-mm-yyyy'));81 insert into SPORTSMAN (SPORTID, SPORTNAME, NATIONALITY, HOTALID, INTAKETIME, LEAVETIME)82 values (SEQ_SPORTID.nextval, '李四', '韩国', 1, to_date('06-04-2014', 'dd-mm-yyyy'), to_date('17-04-2014', 'dd-mm-yyyy'));83 insert into SPORTSMAN (SPORTID, SPORTNAME, NATIONALITY, HOTALID, INTAKETIME, LEAVETIME)84 values (SEQ_SPORTID.nextval, '王五', '日本', 1, to_date('07-04-2014', 'dd-mm-yyyy'), to_date('16-04-2014', 'dd-mm-yyyy'));85 insert into SPORTSMAN (SPORTID, SPORTNAME, NATIONALITY, HOTALID, INTAKETIME, LEAVETIME)86 values (SEQ_SPORTID.nextval, '马六', '中国', 2, to_date('08-04-2014', 'dd-mm-yyyy'), to_date('15-04-2014', 'dd-mm-yyyy'));87 insert into SPORTSMAN (SPORTID, SPORTNAME, NATIONALITY, HOTALID, INTAKETIME, LEAVETIME)88 values (SEQ_SPORTID.nextval, '小猪', '法国', 3, to_date('09-04-2014', 'dd-mm-yyyy'), to_date('14-04-2014', 'dd-mm-yyyy'));89 insert into SPORTSMAN (SPORTID, SPORTNAME, NATIONALITY, HOTALID, INTAKETIME, LEAVETIME)90 values (SEQ_SPORTID.nextval, '菜菜', '外国', 4, to_date('10-04-2014', 'dd-mm-yyyy'), to_date('13-04-2014', 'dd-mm-yyyy'));91 insert into SPORTSMAN (SPORTID, SPORTNAME, NATIONALITY, HOTALID, INTAKETIME, LEAVETIME)92 values (SEQ_SPORTID.nextval, '放大', '是的', 4, to_date('03-03-2013', 'dd-mm-yyyy'), to_date('06-05-2014', 'dd-mm-yyyy'));93 insert into SPORTSMAN (SPORTID, SPORTNAME, NATIONALITY, HOTALID, INTAKETIME, LEAVETIME)94 values (SEQ_SPORTID.nextval, '答复', '的', 1, to_date('03-03-2013', 'dd-mm-yyyy'), to_date('06-05-2014', 'dd-mm-yyyy'));95 insert into SPORTSMAN (SPORTID, SPORTNAME, NATIONALITY, HOTALID, INTAKETIME, LEAVETIME)96 values (SEQ_SPORTID.nextval, '11', '233', 3, to_date('05-04-2014', 'dd-mm-yyyy'), to_date('05-04-2014', 'dd-mm-yyyy'));97 insert into SPORTSMAN (SPORTID, SPORTNAME, NATIONALITY, HOTALID, INTAKETIME, LEAVETIME)98 values (SEQ_SPORTID.nextval, '按到', '中国', 4, to_date('03-03-2013', 'dd-mm-yyyy'), to_date('04-04-2014', 'dd-mm-yyyy'));99 commit;
oracle的脚本

1、项目实现如下功能

(1)分页查询所有

 

(2)分页查询下一页

(3)、模糊查询

(4)、模糊分页查询下一页

(5)、修改第一条

(6)、查看修改结果

(7)、添加

(8)、查看添加后的结果

(9) 删除第二条查看删除结果

2、创建如下项目结构

3、在entity包下创建HotalInfo.java

1 package com.entity; 2  3 import java.io.Serializable; 4 import java.util.HashSet; 5 import java.util.Set; 6  7 /** 8  * 1.一方:酒店 9  * @author Holly老师10  *11  */12 public class HotalInfo implements Serializable{13     private static final long serialVersionUID = 1L;14     /**15      * 酒店id16      */17     private Integer hotalid;18     /**19      * 酒店名称20      */21     private String hotalname;22     /**23      * 酒店地址24      */25     private String hotaladdress;26     27     /**28      * 一方引入set集合:一对多29      */30     private Set
sportsmans=new HashSet
();31 32 public HotalInfo() {33 }34 35 public HotalInfo(String hotalname, String hotaladdress) {36 this.hotalname = hotalname;37 this.hotaladdress = hotaladdress;38 }39 40 41 public HotalInfo(Integer hotalid, String hotalname, String hotaladdress) {42 this.hotalid = hotalid;43 this.hotalname = hotalname;44 this.hotaladdress = hotaladdress;45 }46 47 48 public HotalInfo(Integer hotalid, String hotalname, String hotaladdress,49 Set
sportsmans) {50 this.hotalid = hotalid;51 this.hotalname = hotalname;52 this.hotaladdress = hotaladdress;53 this.sportsmans = sportsmans;54 }55 56 public Integer getHotalid() {57 return hotalid;58 }59 public void setHotalid(Integer hotalid) {60 this.hotalid = hotalid;61 }62 public String getHotalname() {63 return hotalname;64 }65 public void setHotalname(String hotalname) {66 this.hotalname = hotalname;67 }68 public String getHotaladdress() {69 return hotaladdress;70 }71 public void setHotaladdress(String hotaladdress) {72 this.hotaladdress = hotaladdress;73 }74 75 public Set
getSportsmans() {76 return sportsmans;77 }78 79 public void setSportsmans(Set
sportsmans) {80 this.sportsmans = sportsmans;81 }82 83 @Override84 public String toString() {85 return "HotalInfo [hotaladdress=" + hotaladdress + ", hotalid="86 + hotalid + ", hotalname=" + hotalname + "]";87 }88 89 90 91 92 }
HotalInfo.java

4、在entity包下创建Sportsman.java

1 package com.entity;  2   3 import java.util.Date;  4   5 /**  6  * 2.多方:运动员  7  * @author Holly老师  8  *  9  */ 10 public class Sportsman { 11     /** 12      * 运动员编号 13      */ 14     private Integer sportid; 15     /** 16      * 运动员姓名 17      */ 18     private String sportname; 19     /** 20      * 国籍 21      */ 22     private String nationality; 23     /** 24      * 入住时间 25      */ 26     private Date intaketime; 27     /** 28      * 离开时间 29      */ 30     private Date leavetime; 31      32     /** 33      * 多对一:配置对象 34      */ 35     private HotalInfo hotalinfo; 36      37      38     public Sportsman() { 39     } 40  41     public Sportsman(String sportname, String nationality, Date intaketime, 42             Date leavetime, HotalInfo hotalinfo) { 43         this.sportname = sportname; 44         this.nationality = nationality; 45         this.intaketime = intaketime; 46         this.leavetime = leavetime; 47         this.hotalinfo = hotalinfo; 48     } 49  50     public Sportsman(Integer sportid, String sportname, String nationality, 51             Date intaketime, Date leavetime, HotalInfo hotalinfo) { 52         this.sportid = sportid; 53         this.sportname = sportname; 54         this.nationality = nationality; 55         this.intaketime = intaketime; 56         this.leavetime = leavetime; 57         this.hotalinfo = hotalinfo; 58     } 59  60     public Integer getSportid() { 61         return sportid; 62     } 63  64     public void setSportid(Integer sportid) { 65         this.sportid = sportid; 66     } 67  68     public String getSportname() { 69         return sportname; 70     } 71  72     public void setSportname(String sportname) { 73         this.sportname = sportname; 74     } 75  76     public String getNationality() { 77         return nationality; 78     } 79  80     public void setNationality(String nationality) { 81         this.nationality = nationality; 82     } 83  84     public Date getIntaketime() { 85         return intaketime; 86     } 87  88     public void setIntaketime(Date intaketime) { 89         this.intaketime = intaketime; 90     } 91  92     public Date getLeavetime() { 93         return leavetime; 94     } 95  96     public void setLeavetime(Date leavetime) { 97         this.leavetime = leavetime; 98     } 99 100     public HotalInfo getHotalinfo() {101         return hotalinfo;102     }103 104     public void setHotalinfo(HotalInfo hotalinfo) {105         this.hotalinfo = hotalinfo;106     }107 108     @Override109     public String toString() {110         return "Sportsman [hotalinfo=" + hotalinfo + ", intaketime="111                 + intaketime + ", leavetime=" + leavetime + ", nationality="112                 + nationality + ", sportid=" + sportid + ", sportname="113                 + sportname + "]";114     }115     116 117 }
Sportsman.java

5、在entity包下创建HotalInfo.hbm.xml

1 
2 3
4
5
6 7
8
9
10
11
SEQ_HOTALID12
13
14
15
16
17 18
19
20
21
22 23
24
25
26 27
28
HotalInfo.hbm.xml

6、在entity包下创建Sportsman.hbm.xml

1 
2 3
4
5
6
7
8
9
10
SEQ_SPORTID11
12
13
14
15
16
17
18 19
20
21
22
23
Sportsman.hbm.xml

7、在dao包下创建HotalInfoDao.java

1 package com.dao; 2  3 import java.util.List; 4  5 import com.entity.HotalInfo; 6  7 /** 8  * 一方:酒店数据访问层接口 9  * @author Holly老师10  *11  */12 public interface HotalInfoDao {13     /**14      * 1.查询所有15      * @return16      */17      public List
query();18 /**19 * 2.根据id查询20 * @param id21 * @return22 */23 HotalInfo queryById(Integer id);24 25 }
HotalInfoDao.java

8、在dao包下创建SportsmanDao.java

1 package com.dao; 2  3 import java.util.List; 4  5 import com.entity.Sportsman; 6  7 /** 8  * 多方:运动员数据访问层接口 9  * @author Holly老师10  *11  */12 public interface SportsmanDao {13     /**14      * 1.分页查询15      * @param pageSize 页面大小16      * @param pageNo 当前页17      * @return 分页集合18      */19     List
queryPage(Integer pageSize,Integer pageNo);20 21 /**22 * 2.插入一个对象23 * @param obj 携带数据的对象24 * @return 受影响的行数25 */26 int insert(Sportsman obj);27 28 /**29 * 3.根据id删除30 * @param id31 * @return32 */33 int delete(Sportsman obj);34 35 /**36 * 4.修改对象37 * @param obj 携带数据的对象38 * @return 受影响的行数39 */40 int update(Sportsman obj);41 42 /**43 * 5.根据id查询44 * @param id45 * @return46 */47 Sportsman queryById(Integer id);48 49 /**50 * 6.查询总条数51 */52 Integer totalCount();53 54 /**55 * 7.模糊条件分页查询56 * @param pageSize 页面大小57 * @param pageNo 当前页58 * @param likeparam 模糊条件59 * @return60 */61 List
queryPageLike(Integer pageSize,Integer pageNo,String likeparam);62 /**63 * 8.模糊条件查询总条数64 * @return65 */66 Integer totalCountLike(String likeparam);67 }
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
query() { List
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

1 package com.dao.impl;  2   3 import java.util.List;  4   5 import org.hibernate.Query;  6 import org.hibernate.Session;  7 import org.springframework.dao.DataAccessException;  8 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  9  10 import com.dao.SportsmanDao; 11 import com.entity.Sportsman; 12 /** 13  * 多方:运动员数据访问层实现类 14  * @author Holly老师 15  * 16  */ 17 public class SportsmanDaoImpl extends HibernateDaoSupport  18           implements SportsmanDao { 19     /** 20      * 1.删除 21      */ 22     public int delete(Sportsman obj) { 23         try { 24             getHibernateTemplate().delete(obj); 25              26             return 1; 27         } catch (DataAccessException e) { 28             e.printStackTrace(); 29             return 0; 30         } 31     } 32     /** 33      * 2.添加 34      */ 35     public int insert(Sportsman obj) { 36         try { 37             getHibernateTemplate().save(obj); 38             return 1; 39         } catch (DataAccessException e) { 40             e.printStackTrace(); 41             return 0; 42         } 43     } 44     /** 45      * 3.根据id查询 46      */ 47     public Sportsman queryById(Integer id) { 48         try { 49             //根据主键id查询 50             Sportsman man=getHibernateTemplate().get(Sportsman.class, id); 51             return man; 52         } catch (DataAccessException e) { 53             // TODO Auto-generated catch block 54             e.printStackTrace(); 55             return null; 56         } 57     } 58    /** 59     * 4.分页查询 60     */ 61     public List
queryPage(Integer pageSize, Integer pageNo) { 62 //4.1 获取session对象 63 Session session=getHibernateTemplate().getSessionFactory().openSession(); 64 65 //4.2 创建Query对象 66 Query query=session.createQuery("from Sportsman"); 67 68 //4.3 查询起始行数据3-5 69 query.setFirstResult((pageNo-1)*pageSize); 70 71 //4.4 查询结束行数据 72 query.setMaxResults(pageSize); 73 74 //4.5 分页查询 75 List
list=query.list(); 76 77 //4.6 关闭session(?) 78 79 //4.7 返回集合 80 return list; 81 } 82 /** 83 * 5.模糊分页查询 84 * 查询 运行员的信息 85 */ 86 public List
queryPageLike(Integer pageSize, Integer pageNo, 87 String likeparam) { 88 //5.1 获取session对象 89 Session session=getHibernateTemplate().getSessionFactory().openSession(); 90 91 //5.2 将string类型酒店id的类型转换为Integer 类型 92 Integer id=Integer.parseInt(likeparam); 93 // Integer id=likeparam; 94 95 //5.3定义hql语句 96 String hql="from Sportsman a where a.hotalinfo.hotalid=:id"; 97 98 //5.4 创建Query对象 99 Query query=session.createQuery(hql);100 101 //5.5为命名参数赋值(第一个id是sql中:id)102 query.setParameter("id", id);103 104 //5.6 查询起始行105 query.setFirstResult((pageNo-1)*pageSize);106 107 //5.7查询结束行108 query.setMaxResults(pageSize);109 110 //5.8 模糊分页查询111 List
list=query.list();112 113 //5.9 关闭session(?)114 //5.10 返回集合115 return list;116 }117 /**118 * 6.求出总记录数119 */120 public Integer totalCount() {121 //6.1 获取session对象122 Session session=getHibernateTemplate().getSessionFactory().openSession();123 124 //6.2 定义hql语句125 String hql="select count(*) from Sportsman";126 127 //6.3定义Query对象128 Query query=session.createQuery(hql);129 130 //6.4使用唯一结果查询方法131 String count=query.uniqueResult().toString();132 133 //6.5 Stirng 装换为integer134 Integer totalCount=Integer.parseInt(count);135 136 //6.6返回记录数137 return totalCount;138 }139 /**140 * 7.求模糊总记录条数141 */142 public Integer totalCountLike(String likeparam) {143 //7.1 获取session对象144 Session session=getHibernateTemplate().getSessionFactory().openSession();145 146 //7.2 定义hql语句147 String hql="select count(*) from Sportsman a " +148 "where a.hotalinfo.hotalid=:id";149 150 //7.3 创建Query对象151 Query query=session.createQuery(hql);152 153 //7.4 id类型转换154 Integer id=Integer.parseInt(likeparam);155 156 //7.5 为命名参数赋值157 query.setParameter("id", id);158 159 //7.6 查询唯一结果160 String count=query.uniqueResult().toString();161 162 //7.7 类型转换163 Integer totalCount=Integer.parseInt(count);164 165 //7.8 返回记录数166 return totalCount;167 }168 169 /**170 * 8.修改171 */172 public int update(Sportsman obj) {173 try {174 getHibernateTemplate().update(obj);175 return 1;176 } catch (DataAccessException e) {177 e.printStackTrace();178 return 0;179 }180 }181 182 }
SportsmanDaoImpl.java

11、在service包下创建HotalInfoService.java

1 package com.service; 2  3 import java.util.List; 4  5 import com.entity.HotalInfo; 6  7 /** 8  * 一方:酒店业务接口 9  * @author Holly老师10  *11  */12 public interface HotalInfoService {13     /**14      * 1.查询所有15      * @return16      */17      public List
query();18 /**19 * 2.根据id查询20 * @param id21 * @return22 */23 HotalInfo queryById(Integer id);24 25 26 27 }
HotalInfoService.java

12、在service包下创建SportsmanService.java

1 package com.service; 2  3 import java.util.List; 4  5 import com.entity.Sportsman; 6  7 /** 8  * 运动员数据访问层接口 9  * @author Holly老师10  *11  */12 public interface SportsmanService {13     /**14      * 1.分页查询15      * @param pageSize 页面大小16      * @param pageNo 当前页17      * @return 分页集合18      */19     List
queryPage(Integer pageSize,Integer pageNo);20 21 /**22 * 2.插入一个对象23 * @param obj 携带数据的对象24 * @return 受影响的行数25 */26 int insert(Sportsman obj);27 28 /**29 * 3.根据id删除30 * @param id31 * @return32 */33 int delete(Sportsman obj);34 35 /**36 * 4.修改对象37 * @param obj 携带数据的对象38 * @return 受影响的行数39 */40 int update(Sportsman obj);41 42 /**43 * 5.根据id查询44 * @param id45 * @return46 */47 Sportsman queryById(Integer id);48 49 /**50 * 6.查询总条数51 */52 Integer totalCount();53 54 /**55 * 7.模糊条件分页查询56 * @param pageSize 页面大小57 * @param pageNo 当前页58 * @param likeparam 模糊条件59 * @return60 */61 List
queryPageLike(Integer pageSize,Integer pageNo,String likeparam);62 /**63 * 8.模糊条件查询总条数64 * @return65 */66 Integer totalCountLike(String likeparam);67 }
SportsmanService.java

13、在service.impl包下创建HotalInfoServiceImpl.java

1 package com.service.impl; 2  3 import java.util.List; 4  5 import com.dao.HotalInfoDao; 6 import com.entity.HotalInfo; 7 import com.service.HotalInfoService; 8 /** 9  * 一方:业务逻辑接口10  * @author Holly老师11  *12  */13 public class HotalInfoServiceImpl implements HotalInfoService {14     /**15      * 在spring中注入数据访问16      */17     private HotalInfoDao oneDao;18     19     public HotalInfoDao getOneDao() {20         return oneDao;21     }22 23     public void setOneDao(HotalInfoDao oneDao) {24         this.oneDao = oneDao;25     }26 27     /**28     * 查询所有29     */30     public List
query() {31 32 return oneDao.query();33 }34 /**35 * 根据id查询36 */37 public HotalInfo queryById(Integer id) {38 39 return oneDao.queryById(id);40 }41 42 }
HotalInfoServiceImpl.java

14、在service.impl包下创建SportsmanServiceImpl.java

1 package com.service.impl; 2  3 import java.util.List; 4  5 import com.dao.SportsmanDao; 6 import com.entity.Sportsman; 7 import com.service.SportsmanService; 8 /** 9  *多方:业务逻辑接口实现类10  * @author Holly老师11  *12  */13 public class SportsmanServiceImpl implements SportsmanService {14     /**15      * Spring中注入数据访问层dao:多方16      */17     private SportsmanDao manyDao;18     19     public SportsmanDao getManyDao() {20         return manyDao;21     }22 23     public void setManyDao(SportsmanDao manyDao) {24         this.manyDao = manyDao;25     }26     /**27      * 1.删除28      */29     public int delete(Sportsman obj) {30         // TODO Auto-generated method stub31         return manyDao.delete(obj);32     }33 /**34  * 2.添加35  */36     public int insert(Sportsman obj) {37         return manyDao.insert(obj);38     }39 /**40  * 3.根据id查询41  */42     public Sportsman queryById(Integer id) {43         return manyDao.queryById(id);44     }45 /**46  * 4.普通分页查询47  */48     public List
queryPage(Integer pageSize, Integer pageNo) {49 return manyDao.queryPage(pageSize, pageNo);50 }51 /**52 * 5.模糊分页查询53 */54 public List
queryPageLike(Integer pageSize, Integer pageNo,55 String likeparam) {56 // TODO Auto-generated method stub57 return manyDao.queryPageLike(pageSize, pageNo, likeparam);58 }59 /**60 * 6.获取总条数61 */62 public Integer totalCount() {63 return manyDao.totalCount();64 }65 /**66 * 7.条件查询总条数67 */68 public Integer totalCountLike(String likeparam) {69 // TODO Auto-generated method stub70 return manyDao.totalCountLike(likeparam);71 }72 /**73 * 8.修改74 */75 public int update(Sportsman obj) {76 // TODO Auto-generated method stub77 return manyDao.update(obj);78 }79 80 }
SportsmanServiceImpl.java

15、在action包下创建SportsmanAction.java

1 package com.action;  2   3 import java.util.ArrayList;  4 import java.util.List;  5   6 import javax.servlet.http.HttpServletRequest;  7 import javax.servlet.http.HttpSession;  8   9 import org.apache.struts2.ServletActionContext; 10 import org.hibernate.mapping.Array; 11  12 import com.entity.HotalInfo; 13 import com.entity.Sportsman; 14 import com.opensymphony.xwork2.ActionSupport; 15 import com.service.HotalInfoService; 16 import com.service.SportsmanService; 17 /** 18  * 多方:控制层 19  * @author Holly老师 20  * 21  */ 22 public class SportsmanAction extends ActionSupport { 23     /** 24      * 1.在Spring中注入service层对象 25      */ 26     private HotalInfoService oneService; //一方service 27     private SportsmanService manyService; //多方service 28      29     /** 30      * 3.Struts 给一方对象注入数据 31      */ 32     private HotalInfo oneObj; 33      34     /** 35      * 3.Struts 给多方对象注入数据 36      * @return 37      */ 38     private Sportsman manyObj; 39      40      41     /** 42      * 4.查询单条 43      * @return 44      */ 45     public String findById(){ 46         //6.1 获取request对象 47         HttpServletRequest request=ServletActionContext.getRequest(); 48          49         //6.2 判断参数id是否接到 50         if(manyObj!=null){ 51             Integer id=manyObj.getSportid(); 52              53             //6.2.1 根据id查询 54             Sportsman many=manyService.queryById(id); 55             //6.2.2 对象放入 56             request.setAttribute("many", many); 57             return SUCCESS; 58              59         }else{ 60             System.out.println("id没有接到"); 61             return ERROR; 62         } 63     } 64      65      66     /** 67      * 5.分页查询和模糊分页查询 68      * @return 69      */ 70     public String likePage(){ 71        //5.1 获取request对象 72         HttpServletRequest request=ServletActionContext.getRequest(); 73        74         //5.2 获取pageNo参数 75         String no=request.getParameter("pageNo"); 76          77         //5.3 转换 78         Integer pageNo=1; 79         if(no!=null){ 80             pageNo=Integer.parseInt(no); 81         } 82          83          84         //5.4 获取输入框的的条件 85         Integer param=0; 86         if(manyObj!=null){ 87             //通过struts2注入的对象中获取值 88             param= manyObj.getHotalinfo().getHotalid(); 89             System.out.println("输入框值:"+param); 90         } 91          92          93         //由于后台传递的是String ,所以这里 94         String likeparam=param.toString(); 95          96         //5.5 定义页面大小 97         Integer pageSize=3; 98          99         //5.6 判断模糊分页和普通分页100         List
list=new ArrayList
();101 Integer totalCount=null;102 Integer totalPage=null;103 if(likeparam!=null && !likeparam.equals("0")){104 //5.6.1 获取条件区域id模糊分页查询集合105 list=manyService.queryPageLike(pageSize, pageNo, likeparam);106 107 for (Sportsman sportsman : list) {108 System.out.println(sportsman);109 }110 //5.6.2 查询总条数111 totalCount= manyService.totalCountLike(likeparam);112 113 //5.6.3 计算总页数114 totalPage=totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;115 116 //5.6.4 将前台需要的数据给request117 request.setAttribute("list", list);118 request.setAttribute("totalCount", totalCount);119 request.setAttribute("totalPage", totalPage);120 request.setAttribute("pageNo", pageNo);121 request.setAttribute("likeparam", likeparam);122 //**123 124 }else{125 //5.7 查询所有分页126 list=manyService.queryPage(pageSize, pageNo);127 128 //5.6.2 查询总条数129 totalCount= manyService.totalCount();130 131 //5.6.3 计算总页数132 totalPage=totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;133 134 // 获取session对象135 HttpSession session=request.getSession();136 137 //查询one放所有数据138 List
onelist=oneService.query();139 // 140 session.setAttribute("onelist", onelist);141 142 //将前台需要的数据给request143 request.setAttribute("list", list);144 request.setAttribute("totalCount", totalCount);145 request.setAttribute("totalPage", totalPage);146 request.setAttribute("pageNo", pageNo);147 request.setAttribute("likeparam", likeparam);148 149 }150 System.out.println("likeparam:"+likeparam);151 return SUCCESS;152 }153 154 155 /**156 * 6.修改157 * @return158 */159 public String updateMany(){160 if(manyObj!=null){161 HotalInfo one =oneService.queryById(manyObj.getHotalinfo().getHotalid());162 System.out.println("修改one:"+one);163 164 manyObj.setHotalinfo(one);165 HttpServletRequest request=ServletActionContext.getRequest();166 System.out.println("修改many:"+manyObj);167 168 Integer num=manyService.update(manyObj);169 return SUCCESS;170 }else{171 return ERROR;172 }173 174 }175 /**176 * 7.删除177 * @return178 */179 public String deleteMany(){180 if(manyObj!=null){181 //根据多方的id重新查询整条数据=对象182 Sportsman many=manyService.queryById(manyObj.getSportid());183 //删除对象184 Integer num=manyService.delete(many);185 return SUCCESS;186 }else{187 return ERROR;188 }189 190 }191 /**192 * 8.添加 193 */194 public String insertMany(){195 //判断多方对象是否为空196 if( manyObj!=null){197 System.out.println("save的manyObj:"+manyObj);198 //根据多方中一方对象属性的id查询,一方数据199 HotalInfo one=oneService.queryById(manyObj.getHotalinfo().getHotalid());200 201 //将查询到的一方数据赋值给多方的一方对象属性202 manyObj.setHotalinfo(one);203 204 //多方的添加操作205 Integer num=manyService.insert(manyObj) ;206 if(num>0){207 System.out.println("添加查询");208 return SUCCESS;209 }else{210 System.out.println("添加失败");211 return ERROR;212 }213 }else{214 System.out.println("save没接到manyObj:"+manyObj);215 return ERROR;216 217 }218 }219 220 221 public HotalInfoService getOneService() {222 return oneService;223 }224 public void setOneService(HotalInfoService oneService) {225 this.oneService = oneService;226 }227 public SportsmanService getManyService() {228 return manyService;229 }230 public void setManyService(SportsmanService manyService) {231 this.manyService = manyService;232 }233 public HotalInfo getOneObj() {234 return oneObj;235 }236 public void setOneObj(HotalInfo oneObj) {237 this.oneObj = oneObj;238 }239 public Sportsman getManyObj() {240 return manyObj;241 }242 public void setManyObj(Sportsman manyObj) {243 this.manyObj = manyObj;244 }245 246 247 248 249 }
SportsmanAction.java

16、在WebRoot下WEB-INF下创建applicationContext.xml

1 
2
20 21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 37
38
39
40
41 42
43
44
45
com/entity/HotalInfo.hbm.xml
46
com/entity/Sportsman.hbm.xml
47
48
49 50
51
52
53
true
54
true
55
org.hibernate.dialect.Oracle10gDialect
56
57
58
59 60
61
62
63
64 65 66
67
68
69
70
71
72
73
74 75
76
77
78
79
80
81
82
83
84 85
86
87
88
89
90
applicationContext.xml

17、在src下创建struts.xml

1 
2 3
4
5
6
7
8
9
error.jsp
10
11 12
13
16
19
20
21
index.jsp
22
23 24
25
26
page
27
28 29
30
31
update.jsp
32
33 34
35
36
page
37
38 39
40
41
page
42
43
44
struts.xml

18、在WebRoot下WEB-INF下创建web.xml

1 
2
7
8
9
org.springframework.web.context.ContextLoaderListener
10
11 12
13
struts2
14
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
15
16
17
struts2
18
*.action
19
20 21
22
characterEncodingFilter
23
org.springframework.web.filter.CharacterEncodingFilter
24
25
encoding
26
UTF-8
27
28
29
30
characterEncodingFilter
31
/
32
33
34
index.jsp
35
36
web.xml

19、在WebRoot下创建index.jsp

1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  3 <%  4 String path = request.getContextPath();  5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  6 %>  7   8   9  10    11      12      13     My JSP 'index.jsp' starting page 14     
15
16
17
18
19
22 23 24 25
26

运动员住宿管理系统

27
55 56
28
29
51
52
53
54
57
58
59
60
61
64
65 66
128
129
30
31 酒店:
47
48
添加运动员信息 49
50
序号 酒店名称 酒店地址 运动员姓名 入住时间 离开时间 操作
${i.sportid} ${i.hotalinfo.hotalname} ${i.hotalinfo.hotaladdress} ${i.sportname} ${i.intaketime} ${i.leavetime} 62 修改 63 删除
67 68
69
70 71 第${pageNo}/${totalPage}页    72
首页    73
74
75
上一页    76
77
78
上一页    79
80
81 82
83
84
下一页    85
86
87
上一页    88
89
90 91
末页    92 共${totalCount}条 93 94 95 96
97 98
99 100 第${pageNo}/${totalPage}页   101
首页   102
103
104
上一页   105
106
107
上一页   108
109
110 111
112
113
下一页   114
115
116
上一页   117
118
119 120
末页   121 共${totalCount}条122 123
124
125 126 127
130
131 132
index.jsp

20、在WebRoot下创建add.jsp

1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 6 %> 7  8  9 10   11     12     13     My JSP 'update.jsp' starting page14     15     
16
17
18
19
20
23 24 25 26 27
28

添加运动员信息

29
30
31
32
33
48
49
50
51
52
53
54
55
56
57
61
酒店: 34 47
运动员姓名:
国籍:
入住时间:
离店时间:
58 59 60
62
63
64 65
add.jsp

21、在WebRoot下创建update.jsp

1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 6 %> 7  8  9 10   11     12     13     My JSP 'update.jsp' starting page14     15     
16
17
18
19
20
23 24 25 26 27
28

运动员信息维护页

29
30
31
32
33
48
49
50
51
52
53
54
55
56
57
61
酒店: 34 47
运动员姓名:
国籍:
入住时间:
离店时间:
58 59 60
62
63
64 65
update.jsp

 

转载于:https://www.cnblogs.com/holly8/p/5547956.html

你可能感兴趣的文章
Android新增API之AudioEffect中文API与应用实例
查看>>
颜色空间RGB与HSV(HSL)的转换
查看>>
swift 用协议实现代理传值功能
查看>>
深入懂得android view 生命周期
查看>>
android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
查看>>
Android 中 更新视图的函数ondraw() 和dispatchdraw()的区别
查看>>
《Java源码解析》之NIO的Selector机制(Part1:Selector.open())
查看>>
webpack安装问题
查看>>
Qt学习记录--Qt::CaseSensitive
查看>>
你的灯还亮着吗阅读笔记之一
查看>>
python介绍
查看>>
Unity-Editor按钮和菜单显示
查看>>
SharePoint InfoPath 保存无法发布问题
查看>>
word2vec:主要概念和流程
查看>>
Java - MyBites 逆向工程
查看>>
104. Maximum Depth of Binary Tree
查看>>
Python--变量作用域
查看>>
2017-2018-1 20155235 《信息安全系统设计基础》第九周学习总结
查看>>
!!和??
查看>>
matlab演奏卡农 Cripple Pachebel's Canon on Matlab
查看>>