hibernate demo配置和使用 -----类与表映射关系
hibernate.cfg.xml
<!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节点代表一个数据库 --> <session-factory> <!-- 1. 数据库连接配置 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///hib_demo</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <!-- 数据库方法配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 2. 其他相关配置 --> <!-- 2.1 显示hibernate在运行时候执行的sql语句 --> <property name="hibernate.show_sql">true</property> <!-- 2.2 格式化sql --> <property name="hibernate.format_sql">true</property> <!-- 2.3 自动建表 --> <property name="hibernate.hbm2ddl.auto">create</property> </session-factory> </hibernate-configuration>
组件映射:
car.hbm.xml
<?xml version="1.0"?> <!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.compomer" > <class name="car" table="t_car"> <id name="id" > <generator class="native"/> </id> <property name="name" ></property> <component name="wheel"> <property name="size"></property> <property name="count"></property> </component> </class> </hibernate-mapping>
car.java
package cn.itcast.compomer; public class car { private int id; private String name; private wheel wheel; 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 wheel getWheel() { return wheel; } public void setWheel(wheel wheel) { this.wheel = wheel; } }
wheel.java
package cn.itcast.compomer; public class wheel { private int count; private int size; public int getCount() { return count; } public void setCount(int count) { this.count = count; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } }
app.java
public class app { private static SessionFactory sf; static { sf=new Configuration().configure().addClass(car.class).buildSessionFactory();
//该处一定要加上 addclass(car.class) 会自动去查找car.hbm.xml
} @Test public void testsave() throws Exception{ Session session=sf.openSession(); session.beginTransaction(); wheel wheel = new wheel(); wheel.setSize(38); wheel.setCount(4); car car= new car(); car.setName("bmw"); car.setWheel(wheel); session.save(car); session.getTransaction().commit(); session.close(); } }
二,继承映射
animal.java
public abstract class animal { 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; } }
cat.java
public class cat extends animal{ private String catchmouse; public String getCatchmouse() { return catchmouse; } public void setCatchmouse(String catchmouse) { this.catchmouse = catchmouse; } }
cat.hbm.xml
<?xml version="1.0"?> <!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.extend1" > <class name="cat" table="t_cat"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <property name="catchmouse"></property> </class> </hibernate-mapping>
hibernate.cfg.xml
<hibernate-configuration> <!-- 通常,一个session-factory节点代表一个数据库 --> <session-factory> <!-- 1. 数据库连接配置 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///hib_demo</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <!-- 数据库方法配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 2. 其他相关配置 --> <!-- 2.1 显示hibernate在运行时候执行的sql语句 --> <property name="hibernate.show_sql">true</property> <!-- 2.2 格式化sql <property name="hibernate.format_sql">true</property> --> <!-- 2.3 自动建表 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 3. 加载所有映射 <mapping resource="cn/itcast/acollection/user.hbm.xml"/> --> </session-factory> </hibernate-configuration>
app.java
public class app { private static SessionFactory sf; static{ sf= new Configuration().configure().addClass(cat.class).buildSessionFactory(); } @Test public void getsave(){ Session session=sf.openSession(); session.beginTransaction(); /* cat cat= new cat(); cat.setName("dahuamao"); cat.setCatchmouse("zhuoxiaolaoshu "); session.save(cat);*/ //hibernate.cfg.xml <property name="hibernate.hbm2ddl.auto">create</property> 创建表内数据时使用该配置
Query q= session.createQuery("from cat");
List<cat> list=q.list();
System.out.println(list);
//hibernate.cfg.xml <property name="hibernate.hbm2ddl.auto">update</property> 查询表内数据时使用该配置
session.getTransaction().commit();
session.close();
}
}
animal.class+ cat.class + monkey.class
public class monkey extends animal{ private String eatbanana; public String getEatbanana() { return eatbanana; } public void setEatbanana(String eatbanana) { this.eatbanana = eatbanana; } }
三个类对应 一张数据表
<?xml version="1.0"?> <!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.extend2.copy" > <class name="animal" table="t_animal"> <id name="id"> <generator class="native"></generator> </id> < 三个类对应到一张表 需要使用到 筛选器 > <discriminator column="type_"></discriminator> <property name="name"></property> <subclass name="cat" discriminator-value="cat_"> <property name="catchmouse"></property> </subclass> <subclass name="monkey" discriminator-value="monkey_"> <property name="eatbanana"></property> </subclass> </class> </hibernate-mapping>
cat cat= new cat(); cat.setName("大茂华"); cat.setCatchmouse("捉小老鼠"); monkey m= new monkey(); m.setName("猴子"); m.setEatbanana("吃了10个香蕉"); session.save(cat); session.save(m);
三个类对应三个表 animal表 : 1 cat 2 monkey ; cat 表 ; monkey 表
<hibernate-mapping package="cn.itcast.extend2.copy" > <class name="animal" table="t_animal"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <joined-subclass name="cat" table="t_cat"> <key column ="t_animal_id"></key> <property name="catchmouse"></property> </joined-subclass> <joined-subclass name="monkey" table="t_monkey"> <key column="t_animal_id"></key> <property name="eatbanana"></property> </joined-subclass> </class> </hibernate-mapping>
每个子类映射一张表,父类不对应表(2张表)
<hibernate-mapping package="cn.itcast.extend2.copy" > <class name="animal" table="t_animal"> <id name="id"> <!-- 如果用union-subclass节点 主键生成策略不能为自增长 不能使用native --> <generator class="uuid"></generator> </id> <property name="name"></property> <union-subclass name="cat" table="t_cat"> <property name="catchmouse"></property> </union-subclass> <union-subclass name="monkey" table="t_monkey"> <property name="eatbanana"></property> </union-subclass> </class> </hibernate-mapping>
public abstract class animal { private String id; // 使用uuid后 int id 需要更改为 String id ; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }