表之间关系
1. 一对多
- 一个部门有多个员工,一个员工只能属于某一个部门
- 一个班级有多个学生,一个学生只能属于一个班级
2. 多对多
- 一个老师教多个学生,一个学生可以被多个老师教
- 一个学生可以先择多门课程,一门课程可以被多个学生选择
3. 一对一
- 一个公司只能对应一个注册地址
表之间关系建表原则
1. 一对多
在多的一方创建一个外键,指向一的一方的主键
2. 多对多
创建一个中间表,中间表至少有两个字段,分别作为外键指向多对多双方的主键
3. 一对一
唯一外键对应
主键对应
一对多关系配置
一、 建立表
1. 客户
- 一个客户可以有多个联系人
CREATE TABLE `customer` (
`cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
`cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
`cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',
`cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',
`cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',
`cust_phone` varchar(64) DEFAULT NULL COMMENT '固定电话',
`cust_mobile` varchar(16) DEFAULT NULL COMMENT '移动电话',
PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
2. 销售联系人
- 一个联系人只能属于某一个客户
CREATE TABLE `linkman` (
`link_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '联系人编号(主键)',
`link_name` varchar(16) DEFAULT NULL COMMENT '联系人姓名',
`link_gender` char(1) DEFAULT NULL COMMENT '联系人性别',
`link_phone` varchar(16) DEFAULT NULL COMMENT '联系人办公电话',
`link_mobile` varchar(16) DEFAULT NULL COMMENT '联系人手机',
`link_email` varchar(64) DEFAULT NULL COMMENT '联系人邮箱',
`link_qq` varchar(16) DEFAULT NULL COMMENT '联系人qq',
`link_position` varchar(16) DEFAULT NULL COMMENT '联系人职位',
`link_memo` varchar(512) DEFAULT NULL COMMENT '联系人备注',
`link_cust_id` bigint(32) NOT NULL COMMENT '客户id',
PRIMARY KEY (`link_id`),
KEY `FK_cst_lkm` (`link_cust_id`),
CONSTRAINT `FK_cst_lkm` FOREIGN KEY (`link_cust_id`) REFERENCES `customer` (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
关系图
二、建立ORM
1.实体类与数据库中字段进行建立
客户实体类
@[email protected]
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;
}
销售实体类
@[email protected]
public class Linkman {
private Long link_id;
private String link_name;
private String link_gender;
private String link_phone;
private String link_mobile;
private String link_email;
private String link_qq;
private String link_position;
private String link_memo;
private String link_cust_id;
}
2.设置两表之间的关系
- 一个客户可以有多个联系人
@[email protected]
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 = new HashSet<>();
@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 + '\'' + '}';
}
}
- 一个联系人只能属于某一个客户
@[email protected]
public class Linkman {
private Long link_id;
private String link_name;
private String link_gender;
private String link_phone;
private String link_mobile;
private String link_email;
private String link_qq;
private String link_position;
private String link_memo;
private String link_cust_id;
// 一个联系人只对应一个客户
private Customer customer;
@Override
public String toString() {
return "Linkman{" + "link_id=" + link_id + ", link_name='" + link_name + '\'' + ", link_gender='" + link_gender
+ '\'' + ", link_phone='" + link_phone + '\'' + ", link_mobile='" + link_mobile + '\''
+ ", link_email='" + link_email + '\'' + ", link_qq='" + link_qq + '\'' + ", link_position='"
+ link_position + '\'' + ", link_memo='" + link_memo + '\'' + ", link_cust_id='" + link_cust_id + '\''
+ ", customer=" + customer + '}';
}
}
三、添加配置文件
1.客户(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>
<class name="com.xzh.hibernate.domain.Customer" table="customer">
<!--建立类属性哪一个是主键 还要跟数据库当中主键进行对象 -->
<id name="cust_id" column="cust_id">
<generator class="native" />
</id>
<!--建立类中的普通属性与数据库当中字段进行关联 -->
<property name="cust_name" column="cust_name" />
<property name="cust_source" column="cust_source" />
<property name="cust_industry" column="cust_industry" />
<property name="cust_level" column="cust_level" />
<property name="cust_phone" column="cust_phone" />
<property name="cust_mobile" column="cust_mobile" />
<!--一对多 -->
<set name="linkmens" cascade="save-update,delete" inverse="true"><!--set属性名称 -->
<key column="link_cust_id"></key><!--外键 -->
<one-to-many class="com.xzh.hibernate.domain.Linkman"></one-to-many>
</set>
</class>
</hibernate-mapping>
2.联系人(LinkMan)实体类配置文件
<?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>
<class name="com.xzh.hibernate.domain.Linkman" table="linkman">
<!--建立类属性哪一个是主键 还要跟数据库当中主键进行对象 -->
<id name="link_id" column="link_id">
<generator class="native" />
</id>
<!--建立类中的普通属性与数据库当中字段进行关联 -->
<property name="link_name" column="link_name" />
<property name="link_gender" column="link_gender" />
<property name="link_phone" column="link_phone" />
<property name="link_mobile" column="link_mobile" />
<property name="link_email" column="link_email" />
<property name="link_qq" column=" link_qq" />
<property name="link_position" column=" link_position" />
<property name="link_memo" column=" link_memo" />
<many-to-one name="customer" cascade="save-update"
class="com.xzh.hibernate.domain.Customer" column="link_cust_id" />
</class>
</hibernate-mapping>
四、添加核心配置文件
<!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>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3307/hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">abcd</property>
<!-- 配置Hibernate的方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 打印SQL -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化SQL -->
<property name="hibernate.format_sql">true</property>
<!-- 自动创建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- C3P0 -->
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!--在连接池中可用的数据库连接的最少数目 -->
<property name="c3p0.min_size">5</property>
<!--在连接池中所有数据库连接的最大数目 -->
<property name="c3p0.max_size">20</property>
<!--设定数据库连接的过期时间,以秒为单位, 如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 -->
<property name="c3p0.timeout">120</property>
<!--每3000秒检查所有连接池中的空闲连接 以秒为单位 -->
<property name="c3p0.idle_test_period">3000</property>
<!--设置事务的隔离级别-->
<property name="hibernate.connection.isolation">4</property>
<!--创建一个session绑定到当前线程-->
<property name="current_session_context_class">thread</property>
<!--加载映射文件 -->
<mapping resource="com/xzh/hibernate/domain/customer.hbm.xml" />
<mapping resource="com/xzh/hibernate/domain/linkman.hbm.xml" />
</session-factory>
</hibernate-configuration>
五、引入工具类
public class HibernateUtil {
public static final SessionFactory sessionFactory;
static {
//1.加载配置文件
Configuration configure = new Configuration().configure();
//2.创建sessionFactory --JDBC 连接池
sessionFactory = configure.buildSessionFactory();
}
public static Session openSession(){
Session session = sessionFactory.openSession();
return session;
}
public static Session getCurrentSession(){
Session session = sessionFactory.getCurrentSession();
return session;
}
}
六、编写测试类
Session currentSession = HibernateUtil.getCurrentSession();
Transaction transaction = currentSession.beginTransaction();
Customer customer1 = new Customer();
customer1.setCust_name("customer1");
Customer customer2 = new Customer();
customer2.setCust_name("customer2");
Customer customer3 = new Customer();
customer3.setCust_name("customer3");
Linkman linkman1 = new Linkman();
linkman1.setLink_name("linkman1");
Linkman linkman2 = new Linkman();
linkman2.setLink_name("linkman2");
Linkman linkman3 = new Linkman();
linkman3.setLink_name("linkman3");
/* 配置关系 双向维护*/
customer1.getLinkmens().add(linkman1);
customer1.getLinkmens().add(linkman2);
customer2.getLinkmens().add(linkman3);
linkman1.setCustomer(customer1);
linkman2.setCustomer(customer1);
linkman3.setCustomer(customer2);
/* 保存数据 */
currentSession.save(customer1);
currentSession.save(customer2);
currentSession.save(customer3);
currentSession.save(linkman1);
currentSession.save(linkman2);
currentSession.save(linkman3);
transaction.commit();
懒加载:
在 linkman.hbm.xml 中修改
<many-to-one name="customer" cascade="save-update"
class="com.xzh.hibernate.domain.Customer" column="link_cust_id" lazy="false"/>
Session currentSession = HibernateUtil.getCurrentSession();
Transaction transaction = currentSession.beginTransaction();
Linkman linkman = currentSession.get(Linkman.class, 3L);
transaction.commit();
System.out.println(linkman.getLink_name());
System.out.println(linkman.getCustomer().getCust_name());
原文地址:https://www.cnblogs.com/xzh0717/p/10808406.html
时间: 2024-09-29 08:19:50