同时完成多表的创建保存,表关系设置(SSH框架整合)

一、简介

Javaweb中开发中必然会有多个javabean对象,而对象之间必然会存在的关系。那么对象之间的关系在关系型数据库中,如MySql、Oracle、Sql Server数据库中是如何体现这里关系的呢。

Javabean一般在数据库中是以表的形式体现,而java对象之间的关系(不管是简单的还是复杂的关系)都是以表外键约束来体现的。

二、Java对象关系与数据库表之间的关系映射

从实现事务来分,事务之间存在的多对多、多对一、一对一三种关系。比如学生与老师的关系,是多对多的关系(一个学生有多位老师,而一位老师教多位学生),员工与部门的关系,是多对一关系(多个员工对应一个部门),公民与身份证的关系,是一对一关系。在实现开发中,建议使用多对一描述对象关系。尽量的避免使用多与多与一对多的关系。

1>多对多关系映射(双向) 实现开发中建议使用单向的。比如只在Teacher类中保有Student等..

 

2>多对一关系映射(单向) 实现开发中也建议使用单向的,在多的一方建立关系

 

3>一对一关系映射

 

三、多表操作实现思路

如果需同时保存两张表,一个用户表,一个订单表,当点击保存按钮时,通过ajax发送请求到后台控制器controller,在控制器里面先去判断该用户是否存在,即先调用判断用户是否存在的方法,然后对该方法的返回值进行判断,如果该用户存在,则保存订单表,即调用保存订单表的方法;如果该用户不存在,先要保存用户表,即调用保存用户表的方法,然后对该方法的返回值进行判断,成功的话再去保存订单表。

四、代码演示

游客类:

package com.bw.bean;

/**
 * 旅客类
 * 
 * @author 
 *
 */
public class Traveller {
	private int tid;
	private String tname;
	private String sex;
	private String birth;
	private String callphone;
	private String iDCard;

	public int getTid() {
		return tid;
	}

	public void setTid(int tid) {
		this.tid = tid;
	}

	public String getTname() {
		return tname;
	}

	public void setTname(String tname) {
		this.tname = tname;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getBirth() {
		return birth;
	}

	public void setBirth(String birth) {
		this.birth = birth;
	}

	public String getCallphone() {
		return callphone;
	}

	public void setCallphone(String callphone) {
		this.callphone = callphone;
	}

	public String getiDCard() {
		return iDCard;
	}

	public void setiDCard(String iDCard) {
		this.iDCard = iDCard;
	}

	public Traveller(int tid, String tname, String sex, String birth, String callphone, String iDCard) {
		super();
		this.tid = tid;
		this.tname = tname;
		this.sex = sex;
		this.birth = birth;
		this.callphone = callphone;
		this.iDCard = iDCard;
	}

	public Traveller() {
		super();
	}

	@Override
	public String toString() {
		return "Traveller [tid=" + tid + ", tname=" + tname + ", sex=" + sex + ", birth=" + birth + ", callphone="
				+ callphone + ", iDCard=" + iDCard + "]";
	}

}

配置文件的写法: 

<?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.bw.bean.Traveller" table="m_traveller">
        <id name="tid" type="java.lang.Integer">
            <column name="tid" />
            <generator class="native"></generator>
        </id>
        <property name="tname" type="java.lang.String">
            <column name="tname" />
        </property>
        <property name="sex" type="java.lang.String">
            <column name="sex" />
        </property>
        <property name="birth" type="java.lang.String">
            <column name="birth" />
        </property>
        <property name="callphone" type="java.lang.String">
            <column name="calphone" />
        </property>
        <property name="iDCard" type="java.lang.String">
            <column name="iDCard" />
        </property>
    </class>
</hibernate-mapping>

旅游路线类:

package com.bw.bean;

import java.util.Set;

/**
 * 旅游路线类
 * 
 * @author 
 *
 */
public class TravelPath {
	private int pid;
	private String pname;
	private String content;
	private String bmStart;
	private String bmStop;
	private String action;
	private String acStop;
	private double price;
	private int count;

	private Set<Traveller> set;

	public int getPid() {
		return pid;
	}

	public void setPid(int pid) {
		this.pid = pid;
	}

	public String getPname() {
		return pname;
	}

	public void setPname(String pname) {
		this.pname = pname;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getBmStart() {
		return bmStart;
	}

	public void setBmStart(String bmStart) {
		this.bmStart = bmStart;
	}

	public String getBmStop() {
		return bmStop;
	}

	public void setBmStop(String bmStop) {
		this.bmStop = bmStop;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getAcStop() {
		return acStop;
	}

	public void setAcStop(String acStop) {
		this.acStop = acStop;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public int getCount() {
		this.count = this.set.size();
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public Set<Traveller> getSet() {
		return set;
	}

	public void setSet(Set<Traveller> set) {
		this.set = set;
	}

	public TravelPath(int pid, String pname, String content, String bmStart, String bmStop, String action,
			String acStop, double price, int count, Set<Traveller> set) {
		super();
		this.pid = pid;
		this.pname = pname;
		this.content = content;
		this.bmStart = bmStart;
		this.bmStop = bmStop;
		this.action = action;
		this.acStop = acStop;
		this.price = price;
		this.count = count;
		this.set = set;
	}

	@Override
	public String toString() {
		return "TravelPath [pid=" + pid + ", pname=" + pname + ", content=" + content + ", bmStart=" + bmStart
				+ ", bmStop=" + bmStop + ", action=" + action + ", acStop=" + acStop + ", price=" + price + ", count="
				+ count + ", set=" + set + "]";
	}

	public TravelPath() {
		super();
	}

}

配置文件:

<?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.bw.bean.TravelPath" table="m_travelPath">
        <id name="pid" type="java.lang.Integer">
            <column name="pid" />
            <generator class="native"></generator>
        </id>
        <property name="pname" type="java.lang.String">
            <column name="pname" />
        </property>
        <property name="content" type="java.lang.String">
            <column name="content" />
        </property>
        <property name="bmStart" type="java.lang.String">
            <column name="bmStart" />
        </property>
        <property name="bmStop" type="java.lang.String">
            <column name="bmStop" />
        </property>
        <property name="action" type="java.lang.String">
            <column name="action" />
        </property>
        <property name="acStop" type="java.lang.String">
            <column name="acStop" />
        </property>
        <property name="price" type="java.lang.Double">
            <column name="price" />
        </property>
        <set name="set" table="p_t" lazy="false">
            <key column="p_id"></key>
            <many-to-many class="com.bw.bean.Traveller" column="t_id" lazy="false"></many-to-many>
        </set>
    </class>
</hibernate-mapping>

接着配置一下hibernate的核心配置。

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
/**
*    Hibernate核心配置文件
*
*/
<hibernate-configuration>

	<session-factory>
		
		<property name="dialect">
			org.hibernate.dialect.MySQLDialect
		</property>
		<property name="connection.url">
			jdbc:mysql://localhost:3306/b_practice
		</property>
		<property name="connection.username">root</property>
		<property name="connection.password">123456</property>
		<property name="connection.driver_class">
			com.mysql.jdbc.Driver
		</property>
		<property name="show_sql">true</property>
		<property name="hibernate.hbm2ddl.auto">update</property>
		<!-- 默认是auto  -->
		<property name="javax.persistence.validation.mode">none</property>
		<mapping resource="com/bw/bean/Traveller.hbm.xml"/>
		<mapping resource="com/bw/bean/TravelPath.hbm.xml"/>
	</session-factory>

</hibernate-configuration>

五、写在最后:

配置好了之后,运行我们的服务器,Hibernate会自动帮我们生成表格,多对多默认是三张表格(一张主表,一张中间表,一张从表),主表从表可以进行增删改查的操作,中间表只能做增删查的操作,注意没有修改的操作。

下面我们看一下生成的表关系为多对多的表格:

首先是主表:

 

 

 

 

 

 

 

 

 

 

 

 

接着是从表: 

 

 

 

 

 

 

 

 

 

 

 

 

两张表格的外键都是一样的,没有外键约束。

 

 

 

最后是中间表:

 

 

 

 

 

 

 

 

 

 

 

 

 

最后是中间表的外键:

 

 

 

 

 

总结:

从上面的图片列表中可以看出,表关系为多对多时,主表与从表的关联关系主要靠中间表来维护,中间表起到了主表的作用,但是要注意:中间表只能做增删查的操作,没有修改!没有修改!没有修改!想要做修改功能的话,默认是先对中间表进行查询,然后删除对应数据,在添加要修改的数据。

这样整体就完成了多表的同时保存,多对多的增删改查。

 

点个赞嘛,点赞的人都好看。^_^

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值