1、Java实体bean类
package com.project.pojo; import java.io.Serializable; import java.util.HashSet; import java.util.Set; public class Role implements Serializable{ private int id; private String name ; private Set<Resource> resources = new HashSet<>(); public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<Resource> getResources() { return resources; } public void setResources(Set<Resource> resources) { this.resources = resources; } public void setResources(Resource resource) { this.getResources().add(resource); } }
package com.project.pojo; import java.io.Serializable; public class Resource implements Serializable{ private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
2、hbm.xml配置
<?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="com.project.pojo"> <class name="Role" table="t_role"> <id name="id"> <generator class="native" /> </id> <property name="name" /> <!-- table为连接表的表名 --> <set name="resources" table="t_role_resource"> <!-- key指明当前类在关联表中的外键 --> <key column="roid" /> <!-- column 多对多的另一方在关联表中的外键 --> <many-to-many column="reid" class="Resource"></many-to-many> </set> </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="com.project.pojo"> <class name="Resource" table="t_resource"> <id name="id"> <generator class="native" /> </id> <property name="name" /> </class> </hibernate-mapping>
3、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> <!-- 1、数据库连接信息 --> <!-- 指定数据方言 --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://192.168.1.59:3306/hibernate?characterEncoding=UTF8</property> <property name="connection.username">root</property> <property name="connection.password">1234</property> <!-- 2、通用配置信息 --> <!-- 打印sql语句 --> <property name="show_sql">true</property> <!-- 格式化sql语句 --> <property name="format_sql">true</property> <!-- 映射文件信息 --> <mapping resource="com/project/pojo/Resource.hbm.xml" /> <mapping resource="com/project/pojo/Role.hbm.xml" /> </session-factory > </hibernate-configuration>
4、测试
package com.project.test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.project.pojo.Resource;
import com.project.pojo.Role;
import com.project.util.HibernateUtil;
public class HibernateTest {
Session session = null;
Transaction ts = null;
@Before
public void setUp(){
session = HibernateUtil.getSession();
ts = session.beginTransaction();
}
@After
public void tearDown(){
HibernateUtil.closeSession();
}
@Test
public void testCreateDB(){
Configuration cfg = new Configuration().configure();
//使得hibernate映射信息转换为数据库识别的dll语言
SchemaExport se = new SchemaExport(cfg);
//第一个参数:是否打印dll语句
//第二个参数:是否将dll到数据库中执行
se.create(true, true);
}
@Test
public void testInit(){
try {
Role ro1 = new Role();
ro1.setName("普通用户");
Role ro2 = new Role();
ro2.setName("VIP");
Resource re1 = new Resource();
re1.setName("查看");
Resource re2 = new Resource();
re2.setName("新增");
Resource re3 = new Resource();
re3.setName("修改");
Resource re4 = new Resource();
re4.setName("删除");
session.save(re1);
session.save(re2);
session.save(re3);
session.save(re4);
//设置关系
ro1.setResources(re1);
ro1.setResources(re2);
ro2.setResources(re1);
ro2.setResources(re3);
ro2.setResources(re4);
session.save(ro1);
session.save(ro2);
ts.commit();
} catch (Exception e) {
e.printStackTrace();
if(ts!=null)ts.rollback();
}
}
@Test
public void testSelect(){
Role r = (Role) session.get(Role.class, 1);
System.out.println(r.getName());
System.out.println("-----------");
for(Resource re : r.getResources()){
System.out.println(re.getName());
}
}
}