學習一對多的關聯關係映射並操做 java
一:關係表達mysql
1.表中的表達sql
2.實體中的表達數據庫
二:導包session
三:建立實體類app
package cn.itcast.domain; import java.util.Set; /** * @author 做者 * @version 建立時間:2018年7月19日 下午3:38:12 * 類說明: */ public class Customer { private Long cust_id; private String cust_name; private String cust_source; private String cust_industry; private String cust_level; private String cust_phone; private String cust_mobile; private Set<LinkMan> linkmens; public Set<LinkMan> getLinkmens() { return linkmens; } public void setLinkmens(Set<LinkMan> linkmens) { this.linkmens = linkmens; } public Long getCust_id() { return cust_id; } public void setCust_id(Long cust_id) { this.cust_id = cust_id; } public String getCust_name() { return cust_name; } public void setCust_name(String cust_name) { this.cust_name = cust_name; } public String getCust_source() { return cust_source; } public void setCust_source(String cust_source) { this.cust_source = cust_source; } public String getCust_industry() { return cust_industry; } public void setCust_industry(String cust_industry) { this.cust_industry = cust_industry; } public String getCust_level() { return cust_level; } public void setCust_level(String cust_level) { this.cust_level = cust_level; } public String getCust_phone() { return cust_phone; } public void setCust_phone(String cust_phone) { this.cust_phone = cust_phone; } public String getCust_mobile() { return cust_mobile; } public void setCust_mobile(String cust_mobile) { this.cust_mobile = cust_mobile; } @Override public String toString() { return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_source=" + cust_source + ", cust_industry=" + cust_industry + ", cust_level=" + cust_level + ", cust_phone=" + cust_phone + ", cust_mobile=" + cust_mobile + "]"; } }
package cn.itcast.domain; /** * @author 做者 * @version 建立時間:2018年7月20日 下午3:38:58 * 類說明: */ public class LinkMan { /* * CREATE TABLE `cst_linkman` ( `lkm_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '聯繫人編號(主鍵)', `lkm_name` varchar(16) DEFAULT NULL COMMENT '聯繫人姓名', `lkm_cust_id` bigint(32) NOT NULL COMMENT '客戶id', `lkm_gender` char(1) DEFAULT NULL COMMENT '聯繫人性別', `lkm_phone` varchar(16) DEFAULT NULL COMMENT '聯繫人辦公電話', `lkm_mobile` varchar(16) DEFAULT NULL COMMENT '聯繫人手機', `lkm_email` varchar(64) DEFAULT NULL COMMENT '聯繫人郵箱', `lkm_qq` varchar(16) DEFAULT NULL COMMENT '聯繫人qq', `lkm_position` varchar(16) DEFAULT NULL COMMENT '聯繫人職位', `lkm_memo` varchar(512) DEFAULT NULL COMMENT '聯繫人備註', PRIMARY KEY (`lkm_id`), KEY `FK_cst_linkman_lkm_cust_id` (`lkm_cust_id`), CONSTRAINT `FK_cst_linkman_lkm_cust_id` FOREIGN KEY (`lkm_cust_id`) REFERENCES `cst_customer` (`cust_id`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; */ private Long lkm_id; private Character lkm_gender; private String lkm_name; private String lkm_phone; private String lkm_mobile; private String lkm_email; private String lkm_qq; private String lkm_position; private String lkm_memo; private Customer customer; public Long getLkm_id() { return lkm_id; } public void setLkm_id(Long lkm_id) { this.lkm_id = lkm_id; } public Character getLkm_gender() { return lkm_gender; } public void setLkm_gender(Character lkm_gender) { this.lkm_gender = lkm_gender; } public String getLkm_name() { return lkm_name; } public void setLkm_name(String lkm_name) { this.lkm_name = lkm_name; } public String getLkm_phone() { return lkm_phone; } public void setLkm_phone(String lkm_phone) { this.lkm_phone = lkm_phone; } public String getLkm_mobile() { return lkm_mobile; } public void setLkm_mobile(String lkm_mobile) { this.lkm_mobile = lkm_mobile; } public String getLkm_email() { return lkm_email; } public void setLkm_email(String lkm_email) { this.lkm_email = lkm_email; } public String getLkm_qq() { return lkm_qq; } public void setLkm_qq(String lkm_qq) { this.lkm_qq = lkm_qq; } public String getLkm_position() { return lkm_position; } public void setLkm_position(String lkm_position) { this.lkm_position = lkm_position; } public String getLkm_memo() { return lkm_memo; } public void setLkm_memo(String lkm_memo) { this.lkm_memo = lkm_memo; } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } }
四:編寫配置文件框架
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.itcast.domain"> <class name = "LinkMan" table = "cst_linkman"> <id name = "lkm_id"> <generator class="native"></generator> </id> <property name="lkm_gender" column="lkm_gender"></property> <property name="lkm_name" column="lkm_name"></property> <property name="lkm_phone" column="lkm_phone"></property> <property name="lkm_mobile" column="lkm_mobile"></property> <property name="lkm_email" column="lkm_email"></property> <property name="lkm_qq" column="lkm_qq"></property> <property name="lkm_position" column="lkm_position"></property> <property name="lkm_memo" column="lkm_memo"></property> <!-- name:引用屬性名 column:外鍵列名 class:與我關聯的完整類名 --> <many-to-one name="customer" column="lkm_cust_id" class="Customer"> </many-to-one> </class> </hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.itcast.domain"> <class name = "Customer" table = "cst_customer"> <id name = "cust_id"> <generator class="native"></generator> </id> <property name="cust_name" column="cust_name"></property> <property name="cust_source" column="cust_source"></property> <property name="cust_industry" column="cust_industry"></property> <property name="cust_level" column="cust_level"></property> <property name="cust_phone" column="cust_phone"></property> <property name="cust_mobile" column="cust_mobile"></property> <!-- 指定實體中集合屬性名 --> <set name="linkmens"> <!-- 指定外鍵的列名 --> <key column="lkm_cust_id"></key> <!-- 明確關係是一對多 Class指定當前配置文件的類與哪一個類是一對多--> <one-to-many class="LinkMan"/> </set> </class> </hibernate-mapping>
<?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">com.mysql.jdbc.Driver</property> <!-- 數據庫url --> <property name="hibernate.connection.url">jdbc:mysql:///crm_hibernate</property> <!-- 數據庫鏈接用戶名 --> <property name="hibernate.connection.username">root</property> <!-- 數據庫鏈接密碼 --> <property name="hibernate.connection.password">root</property> <!-- 數據庫方言 不一樣的數據庫中,sql語法略有區別. 指定方言能夠讓hibernate框架在生成sql語句時.針對數據庫的方言生成. sql99標準: DDL 定義語言 庫表的增刪改查 DCL 控制語言 事務 權限 DML 操縱語言 增刪改查 注意: MYSQL在選擇方言時,請選擇最短的方言. --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- #hibernate.show_sql true #hibernate.format_sql true --> <!-- 將hibernate生成的sql語句打印到控制檯 --> <property name="hibernate.show_sql">true</property> <!-- 將hibernate生成的sql語句格式化(語法縮進) --> <property name="hibernate.format_sql">true</property> <!-- ## auto schema export 自動導出表結構. 自動建表 #hibernate.hbm2ddl.auto create 自動建表.每次框架運行都會建立新的表.之前表將會被覆蓋,表數據會丟失.(開發環境中測試使用) #hibernate.hbm2ddl.auto create-drop 自動建表.每次框架運行結束都會將全部表刪除.(開發環境中測試使用) #hibernate.hbm2ddl.auto update(推薦使用) 自動生成表.若是已經存在不會再生成.若是表有變更.自動更新表(不會刪除任何數據). #hibernate.hbm2ddl.auto validate 校驗.不自動生成表.每次啓動會校驗數據庫中表是否正確.校驗失敗. --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 引入orm元數據 路徑書寫: 填寫src下的路徑 --> <mapping resource="cn/itcast/domain/Customer.hbm.xml" /> <mapping resource="cn/itcast/domain/LinkMan.hbm.xml" /> </session-factory> </hibernate-configuration>
五:編寫測試類dom
package cn.itcast.one2many; import org.hibernate.Session; import org.hibernate.Transaction; import org.junit.Test; import cn.itcast.domain.Customer; import cn.itcast.domain.LinkMan; import cn.itcast.utils.HibernateUtils; /** * @author 做者 * @version 建立時間:2018年7月20日 下午4:28:54 * 類說明: */ public class TestRelationMap { @Test public void fun1() { //1.獲取session Session session = HibernateUtils.openSession(); //2.開啓事務 Transaction tx = session.beginTransaction(); //3.操做 Customer customer = new Customer(); LinkMan linkman1 = new LinkMan(); LinkMan linkman2 = new LinkMan(); customer.setCust_name("XX公司"); linkman1.setLkm_name("張三"); linkman1.setLkm_name("李四"); customer.getLinkmens().add(linkman1); customer.getLinkmens().add(linkman2); linkman1.setCustomer(customer); linkman2.setCustomer(customer); session.save(customer); session.save(linkman1); session.save(linkman2); //4.提交事務 關閉資源 tx.commit(); session.close(); } }