标签:
刚开始接触hibernate,顿时对ORM(对象关系映射 Object Relational Mapping )有了几分崇拜之情,但心也是够累的!!不说太多,直接贴图!!
看到这里,内心也是崩溃的!!一万只尼玛奔过,过时的方法不能用(能力问题,没看懂异常)?!!!
通过千百次的测试,尼玛,网上的资料也太不负责了!!直接copy的hibernate.cfg.xml竟然是错的!!这尼玛<hibernate-configuration>标签也害的我挣扎了那么久!!
1 <?xml version=‘1.0‘ encoding=‘utf-8‘?> 2 //这尼玛标签<hibernate-configuration>也没人性了 3 <hibernate-configuration 4 xmlns="http://www.hibernate.org/xsd/hibernate-configuration" 5 xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd" 6 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 7 <session-factory> 8 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 9 <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> 10 <property name="connection.username">root</property> 11 <property name="connection.password">TAN19911104</property> 12 13 <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 14 15 <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> 16 17 <property name="show_sql">true</property> 18 19 <mapping resource="com/hibernate/Exp150702/Student.hbm.xml"/> 20 </session-factory> 21 </hibernate-configuration>
没办法,英语苦逼的差,看不懂官方的英文文档(其实是在官网上溜了一圈,没找到这部分文档在哪)
上点自认为正确的吧!!希望能对吾等菜鸟们有点帮助。
hibernate.cfg.xml配置文件
1 <?xml version=‘1.0‘ encoding=‘utf-8‘?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 8 <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> 9 <property name="connection.username">root</property> 10 <property name="connection.password">TAN19911104</property> 11 12 <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 13 14 <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> 15 16 <property name="show_sql">true</property> 17 18 <mapping resource="com/hibernate/Exp150702/Student.hbm.xml"/> 19 </session-factory> 20 </hibernate-configuration>
Student.hbm.xml映射文件
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 6 <hibernate-mapping package="com.hibernate.Exp150702"> 7 <class name="Student"> 8 <id name="id"></id> 9 <property name="name"></property> 10 <property name="birthday"></property> 11 </class> 12 </hibernate-mapping>
Student类
1 package com.hibernate.Exp150702; 2 3 import java.util.Date; 4 5 public class Student { 6 7 8 private int id; 9 private String name; 10 private Date birthday; 11 12 13 public int getId() { 14 return id; 15 } 16 public void setId(int id) { 17 this.id = id; 18 } 19 public String getName() { 20 return name; 21 } 22 public void setName(String name) { 23 this.name = name; 24 } 25 public Date getBirthday() { 26 return birthday; 27 } 28 public void setBirthday(Date birthday) { 29 this.birthday = birthday; 30 } 31 32 33 }
测试类
测试类中也出现了一个比较奇怪的问题!!当使用new SimpleDateFormate("YYYY-MM-DD")时,最终保存在数据库中的数据会与实际值不同。但将其中的样式“YYYY-MM-DD"
更改为”yyyy-MM-dd“时,就不会有错误的出现!!本人试过,发现确实如此!!如非如此,望告知!!
1 package com.hibernate.Exp150702; 2 import java.text.SimpleDateFormat; 3 import java.util.Date; 4 5 import org.hibernate.Session; 6 import org.hibernate.SessionFactory; 7 import org.hibernate.cfg.Configuration; 8 import org.junit.Test; 9 10 public class TestDemo { 11 12 13 @SuppressWarnings("deprecation") 14 @Test 15 public void test() throws Exception{ 16 17 Student s = new Student(); 18 s.setId(1); 19 s.setName("小元"); 20 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 21 Date date = sdf.parse("1967-11-04"); 22 s.setBirthday(date); 23 Configuration cfg = new Configuration(); 24 SessionFactory sf = cfg.configure().buildSessionFactory(); 25 Session session = sf.openSession(); 26 session.beginTransaction(); 27 session.save(s); 28 session.getTransaction().commit(); 29 session.close(); 30 } 31 }
标签:
原文地址:http://www.cnblogs.com/tanzy114/p/4617539.html