配置hibernate
Hibernate 主要是ORM,object-relationship-model.对象关系模型。为了熟悉Hibernate,下面简单的做了一个测试。
jar包。
1 <!DOCTYPE hibernate-configuration PUBLIC 2 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 4 5 <hibernate-configuration> 6 7 <session-factory> 8 <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver</property> 9 <property name="hibernate.connection.url"> jdbc:mysql://localhost:3306/test</property> 10 <property name="hibernate.connection.username"> root</property> 11 <property name="hibernate.connection.password"> 1234</property> 12 13 <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect</property> 14 <property name="show_sql">true</property> 15 <property name="hibernate.hbm2ddl.auto">update</property> 16 17 <mapping resource="com/shuanlei/pojo/Student.hbm.xml"/> 18 19 </session-factory> 20 </hibernate-configuration>
1 public class Department { 2 private Integer id; 3 private String departDesc; 4 5 private Set<Employee> employees= new HashSet<Employee>();
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping 6 package="com.shuanlei.pojo"> 7 8 <class name="department" table="t_department" lazy="true"> 9 10 <id name="id"> 11 <generator class="native"/> 12 </id> 13 14 <property name="departDesc" column="departDesc"></property> 15 <set name="employees"> 16 <key> 17 <column name="emp_id"></column> 18 </key> 19 <one-to-many class="Employee"/> 20 </set> 21 </class> 22 23 </hibernate-mapping>
1 public class Employee { 2 private Integer id; 3 private String uname; 4 5 private Department department;
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping 6 package="com.shuanlei.pojo"> 7 8 <class name="Employee" table="t_employee" lazy="true"> 9 10 <id name="id"> 11 <generator class="native"/> 12 </id> 13 14 <property name="uname" column="uname"></property> 15 <property name="pwd" 16 column="pwd"/> 17 <many-to-one name="department" class="Department"> 18 <column name="emp_id"></column> 19 </many-to-one> 20 </class> 21 22 </hibernate-mapping>
测试代码
1 public class TestHiber { 2 3 Session session = HibUtil.getSession(); 4 5 public void add(Student stu) { 6 try { 7 session.getTransaction().begin(); 8 9 session.save(stu); 10 session.getTransaction().commit(); 11 12 } catch (HibernateException e) { 13 session.getTransaction().rollback(); 14 e.printStackTrace(); 15 } finally { 16 17 session.close(); 18 } 19 } 20 21 public static void main(String[] args) { 22 Student stu = new Student(); 23 stu.setPwd("222"); 24 stu.setUname("aba"); 25 TestHiber testHiber = new TestHiber(); 26 testHiber.add(stu); 27 } 28 29 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。