内容列表
![](https://img-blog.csdn.net/20170809124217630?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTVJaWlh6eng=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
实体类
Cpb类
package com.mingde.po;
public class Cpb {
private int pid;
private int oid;
private String proname;
private String prono;
private int pronumber;
private Ddbiao dd=new Ddbiao();
public Cpb() {
super();
}
public Cpb(int pid, String proname, String prono, int pronumber) {
super();
this.pid = pid;
this.proname = proname;
this.prono = prono;
this.pronumber = pronumber;
}
public Cpb(String proname, String prono, int pronumber) {
super();
this.proname = proname;
this.prono = prono;
this.pronumber = pronumber;
}
public Cpb(int pid, int oid, String proname, String prono, int pronumber) {
super();
this.pid = pid;
this.oid = oid;
this.proname = proname;
this.prono = prono;
this.pronumber = pronumber;
}
public Ddbiao getDd() {
return dd;
}
public void setDd(Ddbiao dd) {
this.dd = dd;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public int getOid() {
return oid;
}
public void setOid(int oid) {
this.oid = oid;
}
public String getProname() {
return proname;
}
public void setProname(String proname) {
this.proname = proname;
}
public String getProno() {
return prono;
}
public void setProno(String prono) {
this.prono = prono;
}
public int getPronumber() {
return pronumber;
}
public void setPronumber(int pronumber) {
this.pronumber = pronumber;
}
@Override
public String toString() {
return "Cpb [pid=" + pid + ", oid=" + oid + ", proname=" + proname + ", prono=" + prono + ", pronumber="
+ pronumber + ", dd=" + dd + "]";
}
}
Ddbiao类
package com.mingde.po;
import java.sql.Date;
import java.util.HashSet;
import java.util.Set;
public class Ddbiao {
private int oid;
private String ordername ;
private String orderno ;
private String address ;
private int storehouse ;
private Date createddate;
private Set<Cpb> cpds=new HashSet<>();
public Ddbiao() {
super();
}
public Ddbiao(int oid, String ordername, String orderno, String address, int storehouse, Date createddate) {
super();
this.oid = oid;
this.ordername = ordername;
this.orderno = orderno;
this.address = address;
this.storehouse = storehouse;
this.createddate = createddate;
}
public Set<Cpb> getCpds() {
return cpds;
}
public void setCpds(Set<Cpb> cpds) {
this.cpds = cpds;
}
public int getOid() {
return oid;
}
public void setOid(int oid) {
this.oid = oid;
}
public String getOrdername() {
return ordername;
}
public void setOrdername(String ordername) {
this.ordername = ordername;
}
public String getOrderno() {
return orderno;
}
public void setOrderno(String orderno) {
this.orderno = orderno;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getStorehouse() {
return storehouse;
}
public void setStorehouse(int storehouse) {
this.storehouse = storehouse;
}
public Date getCreateddate() {
return createddate;
}
public void setCreateddate(Date createddate) {
this.createddate = createddate;
}
@Override
public String toString() {
return "Ddbiao [oid=" + oid + ", ordername=" + ordername + ", orderno=" + orderno + ", address=" + address
+ ", storehouse=" + storehouse + ", createddate=" + createddate + "]";
}
}
.hbm.xml配置
Cpb.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-8-8 21:48:11 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.mingde.po.Cpb" table="CPB">
<id name="pid" type="int">
<column name="PID" />
<generator class="sequence" >
<param name="sequence">sequ06</param>
</generator>
</id>
<property name="proname" type="java.lang.String">
<column name="PRONAME" />
</property>
<property name="prono" type="java.lang.String">
<column name="PRONO" />
</property>
<property name="pronumber" type="int">
<column name="PRONUMBER" />
</property>
<many-to-one name="dd" class="com.mingde.po.Ddbiao" fetch="join" >
<column name="OID" />
</many-to-one>
</class>
</hibernate-mapping>
Ddbiao.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-8-8 21:48:11 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.mingde.po.Ddbiao" table="DDBIAO">
<id name="oid" type="int">
<column name="OID" />
<generator class="sequence" >
<param name="sequence">sequ_classes</param>
</generator>
</id>
<property name="ordername" type="java.lang.String">
<column name="ORDERNAME" />
</property>
<property name="orderno" type="java.lang.String">
<column name="ORDERNO" />
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS" />
</property>
<property name="storehouse" type="int">
<column name="STOREHOUSE" />
</property>
<property name="createddate" type="java.sql.Date">
<column name="CREATEDDATE" />
</property>
<!-- 一对多关联关系映射 -->
<!-- 一对多关联关系,需要设置立即加载,才能在加载一方的同时加载与其关系的多方对象(lazy="false") ,用于查询
cascade="all-delete-orphan":代表在对当前的dept对象执行增、删,改操作时,对其关联的对象也执行一样的操作,
并且,如果多方失去了与一方的关联,那么多方的外键为null,这时将会删除外键为null的多余的多方对象
-->
<set name="cpds" table="CPB" inverse="true" lazy="false" cascade="all-delete-orphan" fetch="join" >
<key>
<column name="OID" />
</key>
<one-to-many class="com.mingde.po.Cpb" />
</set>
</class>
</hibernate-mapping>
hibernate.cfg.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="hibernate.connection.username">scott</property>
<property name="hibernate.connection.password">123</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="com/mingde/po/Cpb.hbm.xml" />
<mapping resource="com/mingde/po/Ddbiao.hbm.xml" />
</session-factory>
</hibernate-configuration>
struts.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<constant name="struts.devMode" value="true"></constant>
<package name="struts" extends="struts-default">
<action name="*_*" class="com.mingde.action.DAction" method="{2}">
<result name="{2}">/WEB-INF/{1}/{2}.jsp</result>
<result name="to_list" type="redirect">{1}_list</result>
</action>
</package>
</struts>
Action类
package com.mingde.action;
import java.util.ArrayList;
import java.util.List;
import com.mingde.dao.IBaseDao;
import com.mingde.dao.impl.BaseDaoImpl;
import com.mingde.po.Cpb;
import com.mingde.po.Ddbiao;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class DAction extends ActionSupport{
private IBaseDao bd=new BaseDaoImpl();
private List<Ddbiao> dlist=new ArrayList<>();
private Ddbiao dd=new Ddbiao();
//定义要进行添加和修改的属性
private Integer[] pid;
private String[] proname;
private String[] prono;
private Integer[] pronumber;
//列表显示
public String list()throws Exception{
dlist=bd.findAll("from Ddbiao");
return "list";
}
//到添加页面
public String toAdd()throws Exception{
return "toAdd";
}
//到修改页面
public String toUpdate()throws Exception{
dd=(Ddbiao) bd.findOne(dd.getClass(), dd.getOid());
return "toUpdate";
}
//删除
public String del()throws Exception{
dd=(Ddbiao) bd.findOne(dd.getClass(), dd.getOid());
bd.del(dd);
return "to_list";
}
//添加
public String add()throws Exception{
bd.save(dd);
for(int i=1;i<proname.length;i++){
Cpb c= new Cpb(proname[i], prono[i], pronumber[i]);
c.setDd(dd);
bd.save(c);
}
return "to_list";
}
//修改
public String update()throws Exception{
for(int i=1;i<proname.length;i++){
Cpb c= new Cpb(pid[i],proname[i], prono[i], pronumber[i]);
c.setDd(dd);
dd.getCpds().add(c);//修改一定要“你中有我,我中有你”的关系,两个类一定要互相关联
}
bd.update(dd);
return "to_list";
}
public List<Ddbiao> getDlist() {
return dlist;
}
public Integer[] getPid() {
return pid;
}
public void setPid(Integer[] pid) {
this.pid = pid;
}
public String[] getProname() {
return proname;
}
public void setProname(String[] proname) {
this.proname = proname;
}
public String[] getProno() {
return prono;
}
public void setProno(String[] prono) {
this.prono = prono;
}
public Integer[] getPronumber() {
return pronumber;
}
public void setPronumber(Integer[] pronumber) {
this.pronumber = pronumber;
}
public Ddbiao getDd() {
return dd;
}
public void setDd(Ddbiao dd) {
this.dd = dd;
}
public void setDlist(List<Ddbiao> dlist) {
this.dlist = dlist;
}
}
Dao层
IBaseDao
package com.mingde.dao;
import java.util.List;
public interface IBaseDao {
List findAll(String hql);
void save(Object obj);
Object findOne(Class<?> class1, int did);
void update(Object dc);
void del(Object dc);
}
BaseDaoImpl.java
package com.mingde.dao.impl;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.mingde.dao.IBaseDao;
import com.mingde.utils.HibernateSessionFactory;
public class BaseDaoImpl implements IBaseDao {
@Override
public List findAll(String hql) {
List list=null;
//构造session对象
Session session =HibernateSessionFactory.getSession();
//构造事务
Transaction tx=null;
try {
//开启事务
tx=session.beginTransaction();
//执行命令
list=session.createQuery(hql).list();
//提交事务
tx.commit();
} catch (Exception e) {
e.printStackTrace();
//事务回滚
tx.rollback();
}finally{
//关闭事务
HibernateSessionFactory.closeSessionFactory();
}
return list;
}
@Override
public void save(Object obj) {
//构造session对象
Session session =HibernateSessionFactory.getSession();
//构造事务
Transaction tx=null;
try {
//开启事务
tx=session.beginTransaction();
//执行命令
session.save(obj);
//提交事务
tx.commit();
} catch (Exception e) {
e.printStackTrace();
//事务回滚
tx.rollback();
}finally{
//关闭事务
HibernateSessionFactory.closeSessionFactory();
}
}
@Override
public Object findOne(Class<?> class1, int did) {
Object d=null;
//构造session对象
Session session =HibernateSessionFactory.getSession();
//构造事务
Transaction tx=null;
try {
//开启事务
tx=session.beginTransaction();
//执行命令
d=session.get(class1, did);
//提交事务
tx.commit();
} catch (Exception e) {
e.printStackTrace();
//事务回滚
tx.rollback();
}finally{
//关闭事务
HibernateSessionFactory.closeSessionFactory();
}
return d;
}
@Override
public void update(Object obj) {
System.out.println(obj);
//构造session对象
Session session =HibernateSessionFactory.getSession();
//构造事务
Transaction tx=null;
try {
//开启事务
tx=session.beginTransaction();
//执行命令(修改)(若用update来修改,无法修改关联的表,用merge修改可修改关联的表,包括添加新的内容和减少内容信息)
session.merge(obj);
//提交事务
tx.commit();
} catch (Exception e) {
e.printStackTrace();
//事务回滚
tx.rollback();
}finally{
//关闭事务
HibernateSessionFactory.closeSessionFactory();
}
}
@Override
public void del(Object obj) {
//构造session对象
Session session =HibernateSessionFactory.getSession();
//构造事务
Transaction tx=null;
try {
//开启事务
tx=session.beginTransaction();
//执行命令
session.delete(obj);
//提交事务
tx.commit();
} catch (Exception e) {
e.printStackTrace();
//事务回滚
tx.rollback();
}finally{
//关闭事务
HibernateSessionFactory.closeSessionFactory();
}
}
}
Utils包的HibernateSessionFactory.java
package com.mingde.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateSessionFactory {
private static SessionFactory sessionFactory ;
private static Configuration config=new Configuration();
private static ThreadLocal<Session> threadLocal=new ThreadLocal<>();
static{
config.configure();
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
sessionFactory=config.buildSessionFactory(serviceRegistry);
}
//获取session
public static Session getSession(){
Session session = threadLocal.get();
if(session==null || !session.isOpen()){
if(sessionFactory==null){
rebuildSessionFactory();
}
}
session=(sessionFactory!=null)?sessionFactory.openSession():null;
threadLocal.set(session);
return session;
}
//重建sessionFactory
private static void rebuildSessionFactory() {
config.configure();
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
sessionFactory = config.buildSessionFactory(serviceRegistry);
}
//关闭sessionFactory
public static void closeSessionFactory(){
Session session = threadLocal.get();
threadLocal.set(null);
if(session!=null)session.close();
}
}
JSP页面
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix='s' uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script>
function $(v){
return document.getElementById(v);
}
function fun(v){
$(v).style.display=$(v).style.display=="none"?"":'none';
}
</script>
<body>
<h2>列表</h2>
<table align="center" width=800 border=1 rules="all">
<tr>
<td colspan=6><a href="dd_toAdd">新建</a></td>
</tr>
<tr>
<th>客户订单姓名</th><th>客户订单号</th><th>收货地址</th><th>源仓库</th><th>创建时间</th><th>操作</th>
</tr>
<s:iterator value="dlist">
<tr align='center' οnclick="fun('d_${oid }')">
<td><s:property value="ordername" /></td>
<td><s:property value="orderno" /></td>
<td><s:property value="address" /></td>
<td><s:property value="storehouse" /></td>
<td><s:date name="createddate" format="yyyy-mm-dd"/></td>
<td>
<s:a href="dd_toUpdate?dd.oid=%{oid}">修改</s:a>
<s:a href="dd_del?dd.oid=%{oid}" οnclick="return confirm('确定删除?')">删除</s:a>
</td>
</tr>
<tr id="d_${oid }" style="display:none">
<td colspan=6 >
<table align="center" width=85% >
<tr>
<th>ID</th><th>商品名称</th><th>商品编号</th><th>商品数量</th>
</tr>
<s:iterator value="cpds">
<tr align="center">
<td><s:property value="pid"/></td>
<td><s:property value="proname"/></td>
<td><s:property value="prono"/></td>
<td><s:property value="pronumber"/></td>
</tr>
</s:iterator>
</table>
</td>
</tr>
</s:iterator>
</table>
</body>
</html>
toAdd.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script>
function $(v){
return document.getElementById(v);
}
function fun(){
var tr=$("demo").cloneNode(true);
tr.style.display="table";
$('demo').parentNode.appendChild(tr);
}
function fun2(v){
var tr=v.parentNode.parentNode;
tr.parentNode.removeChild(tr);
}
</script>
<body>
<s:form action="dd_add" theme="simple">
客户订单姓名:<s:textfield name="dd.ordername"></s:textfield><br>
客户订单号:<s:textfield name="dd.orderno"></s:textfield><br>
收货地址:<s:textfield name="dd.address"></s:textfield><br>
源仓库:<s:select name="dd.storehouse" list="#{'1':'广州仓','2':'武汉仓','3':'北京仓','4':'重庆仓','5':'吉林仓'}" value='1'></s:select><br>
创建时间:<s:textfield name="dd.createddate"></s:textfield><br>
<s:submit value="添加"></s:submit><br>
<a href="javascript:void(0)" οnclick="fun()">添加新的产品</a><hr>
<table>
<tr id="demo" style="display:none">
<td>
商品名称:<s:textfield name="proname"></s:textfield><br>
商品编号:<s:textfield name="prono"></s:textfield><br>
商品数量:<s:textfield name="pronumber"></s:textfield><br>
<a href="javascript:void(0)" οnclick="fun2(this)">删除</a>
</td>
</tr>
</table>
</s:form>
</body>
</html>
toUpdate.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script>
function $(v){
return document.getElementById(v);
}
function fun(){
var tr=$("demo").cloneNode(true);
tr.style.display="table";
$('demo').parentNode.appendChild(tr);
}
function fun2(v){
var tr=v.parentNode.parentNode;
tr.parentNode.removeChild(tr);
}
</script>
<body>
<s:form action="dd_add" theme="simple">
客户订单姓名:<s:textfield name="dd.ordername"></s:textfield><br>
客户订单号:<s:textfield name="dd.orderno"></s:textfield><br>
收货地址:<s:textfield name="dd.address"></s:textfield><br>
源仓库:<s:select name="dd.storehouse" list="#{'1':'广州仓','2':'武汉仓','3':'北京仓','4':'重庆仓','5':'吉林仓'}" value='1'></s:select><br>
创建时间:<s:textfield name="dd.createddate"></s:textfield><br>
<s:submit value="添加"></s:submit><br>
<a href="javascript:void(0)" οnclick="fun()">添加新的产品</a><hr>
<table>
<tr id="demo" style="display:none">
<td>
商品名称:<s:textfield name="proname"></s:textfield><br>
商品编号:<s:textfield name="prono"></s:textfield><br>
商品数量:<s:textfield name="pronumber"></s:textfield><br>
<a href="javascript:void(0)" οnclick="fun2(this)">删除</a>
</td>
</tr>
</table>
</s:form>
</body>
</html>