符合中小企业对网站设计、功能常规化式的企业展示型网站建设
本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...
商城网站建设因基本功能的需求不同费用上面也有很大的差别...
手机微信网站开发、微信官网、微信商城网站...
人们总是在失去时,才了解到事情的本质 |
需求:动物、猫、猴子
Animal.java
package com.rk.hibernate.n_inheritance1; 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
package com.rk.hibernate.n_inheritance1; public class Cat extends Animal { private String catchMouse; public String getCatchMouse() { return catchMouse; } public void setCatchMouse(String catchMouse) { this.catchMouse = catchMouse; } @Override public String toString() { return "Cat [id=" + getId() + ", name=" + getName() + ", catchMouse=" + catchMouse + "]"; } }Cat.hbm.xml
App.java
package com.rk.hibernate.n_inheritance1; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.junit.Test; public class App { private static SessionFactory sf; static { sf = new Configuration() .configure() .addClass(Cat.class) .buildSessionFactory(); } @Test public void testSave() { Session session = sf.openSession(); session.beginTransaction(); //猫 Cat cat = new Cat(); cat.setName("大花猫"); cat.setCatchMouse("抓小老鼠"); //保存 session.save(cat); session.getTransaction().commit(); session.close(); } @Test public void testGetCat() { Session session = sf.openSession(); session.beginTransaction(); //通过HQL查询Cat Query q = session.createQuery("from Cat"); List生成的T_Cat表
总结:简单继承映射,有多少个子类,写多少个映射文件! |
下面3种方法都是只使用一个映射文件(Animal.hbm.xml文件),但是不同的方法生成的数据库表的数量不同。
什么情况用?
子类教多,且子类较为简单,即只有个别属性!
好处:因为使用一个映射文件, 减少了映射文件的个数。
缺点:(不符合数据库设计原则)
Animal.java
package com.rk.hibernate.n_inheritance2; 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
package com.rk.hibernate.n_inheritance2; public class Cat extends Animal { private String catchMouse; public String getCatchMouse() { return catchMouse; } public void setCatchMouse(String catchMouse) { this.catchMouse = catchMouse; } @Override public String toString() { return "Cat [id=" + getId() + ", name=" + getName() + ", catchMouse=" + catchMouse + "]"; } }Monkey.java
package com.rk.hibernate.n_inheritance2; public class Monkey extends Animal { private String eatBanana; public String getEatBanana() { return eatBanana; } public void setEatBanana(String eatBanana) { this.eatBanana = eatBanana; } }Animal.hbm.xml
App.java
package com.rk.hibernate.n_inheritance2; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.junit.Test; public class App { private static SessionFactory sf; static { sf = new Configuration() .configure() .addClass(Animal.class) .buildSessionFactory(); } @Test public void testSave() { Session session = sf.openSession(); session.beginTransaction(); //猫 Cat cat = new Cat(); cat.setName("大花猫"); cat.setCatchMouse("抓小老鼠"); //猴子 Monkey monkey = new Monkey(); monkey.setName("淘气猴"); monkey.setEatBanana("吃黄香蕉"); //保存 session.save(cat); session.save(monkey); session.getTransaction().commit(); session.close(); } }生成的T_Animals表
总结:写法较为简单:所有子类用一个映射文件,且映射到一张表!但数据库设计不合理!不推荐用。 |
Animal.java
package com.rk.hibernate.n_inheritance3; 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
package com.rk.hibernate.n_inheritance3; public class Cat extends Animal { private String catchMouse; public String getCatchMouse() { return catchMouse; } public void setCatchMouse(String catchMouse) { this.catchMouse = catchMouse; } @Override public String toString() { return "Cat [id=" + getId() + ", name=" + getName() + ", catchMouse=" + catchMouse + "]"; } }Monkey.java
package com.rk.hibernate.n_inheritance3; public class Monkey extends Animal { private String eatBanana; public String getEatBanana() { return eatBanana; } public void setEatBanana(String eatBanana) { this.eatBanana = eatBanana; } }Animal.hbm.xml
App.java
package com.rk.hibernate.n_inheritance3; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.junit.Test; public class App { private static SessionFactory sf; static { sf = new Configuration() .configure() .addClass(Animal.class) .buildSessionFactory(); } @Test public void testSave() { Session session = sf.openSession(); session.beginTransaction(); //猫 Cat cat = new Cat(); cat.setName("大花猫"); cat.setCatchMouse("抓小老鼠"); //猴子 Monkey monkey = new Monkey(); monkey.setName("淘气猴"); monkey.setEatBanana("吃黄香蕉"); //保存 session.save(cat); session.save(monkey); session.getTransaction().commit(); session.close(); } }生成的T_Animal_Each表
生成的T_Cat_Each表
生成的T_Monkey_Each表
总结:一个映射文件,存储所有的子类; 子类父类都对应表; 缺点:表结构比较负责,插入一条子类信息,需要用2条sql: 往父类插入、往子类插入! |
Animal.java
package com.rk.hibernate.n_inheritance4; public abstract class Animal { private 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; } }Cat.java
package com.rk.hibernate.n_inheritance4; public class Cat extends Animal { private String catchMouse; public String getCatchMouse() { return catchMouse; } public void setCatchMouse(String catchMouse) { this.catchMouse = catchMouse; } @Override public String toString() { return "Cat [id=" + getId() + ", name=" + getName() + ", catchMouse=" + catchMouse + "]"; } }Monkey.java
package com.rk.hibernate.n_inheritance4; public class Monkey extends Animal { private String eatBanana; public String getEatBanana() { return eatBanana; } public void setEatBanana(String eatBanana) { this.eatBanana = eatBanana; } }Animal.hbm.xml
App.java
package com.rk.hibernate.n_inheritance4; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.junit.Test; public class App { private static SessionFactory sf; static { sf = new Configuration() .configure() .addClass(Animal.class) .buildSessionFactory(); } @Test public void testSave() { Session session = sf.openSession(); session.beginTransaction(); //猫 Cat cat = new Cat(); cat.setName("大花猫"); cat.setCatchMouse("抓小老鼠"); //猴子 Monkey monkey = new Monkey(); monkey.setName("淘气猴"); monkey.setEatBanana("吃黄香蕉"); //保存 session.save(cat); session.save(monkey); session.getTransaction().commit(); session.close(); } }生成的T_Animal_Final表,虽然会生成这个表,但是不会存储数据
生成的T_Cat_Final表
生成的T_Monkey_Final表
总结:所有的子类都写到一个映射文件;父类不对应表; 每个子类对应一张表。 |
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。