标签:cti mini javabean min character save 4.0 hibernate market
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <property name="connection.username">root</property> 8 <property name="connection.password">root</property> 9 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 10 <!-- 三个斜杠为简写方式 --> 11 <property name="connection.url">jdbc:mysql:///hibernate?userUnicode=true& 12 characterEncoding=UTF-8</property> 13 <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 14 15 <property name="show_sql">true</property> 16 <property name="format_sql">true</property> 17 18 <!-- 生成表结构策略 --> 19 <!--create:表示启动的时候先drop,再create 20 create-drop: 也表示创建,只不过再系统关闭前执行一下drop 21 update: 这个操作启动的时候会去检查schema是否一致,如果不一致会做scheme更新 22 validate: 启动时验证现有schema与你配置的hibernate是否一致,如果不一致就抛出异常,并不做更新 23 --> 24 <property name="hbm2ddl.auto">update</property> 25 26 <!-- 默认的数据库 --> 27 <property name="hibernate.default_schema">hibernate</property> 28 </session-factory> 29 </hibernate-configuration>
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- Generated 2016-11-12 23:40:02 by Hibernate Tools 3.4.0.CR1 --> 5 <hibernate-mapping> 6 <class name="Students" table="STUDENTS"> 7 <id name="sid" type="int"> 8 <column name="SID" /> 9 <generator class="assigned" /> 10 </id> 11 <property name="sname" type="java.lang.String"> 12 <column name="SNAME" /> 13 </property> 14 <property name="gender" type="java.lang.String"> 15 <column name="GENDER" /> 16 </property> 17 <property name="birthday" type="java.util.Date"> 18 <column name="BIRTHDAY" /> 19 </property> 20 <property name="address" type="java.lang.String"> 21 <column name="ADDRESS" /> 22 </property> 23 </class> 24 </hibernate-mapping>
1 <session-factory> 2 ... 3 <mapping resource="Students.hbm.xml"/> 4 </session-factory>
1 import java.util.Date; 2 import org.hibernate.Session; 3 import org.hibernate.SessionFactory; 4 import org.hibernate.Transaction; 5 import org.hibernate.cfg.Configuration; 6 import org.hibernate.service.ServiceRegistry; 7 import org.hibernate.service.ServiceRegistryBuilder; 8 import org.junit.After; 9 import org.junit.Before; 10 import org.junit.Test; 11 12 public class StudentsTest { 13 private SessionFactory sessionFactory; 14 private Session session; 15 private Transaction transaction; 16 17 @Before 18 public void init() { 19 //创建配置对象 20 Configuration config = new Configuration().configure(); 21 22 //创建服务注册对象 23 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings( 24 config.getProperties()).buildServiceRegistry(); 25 26 //创建会话工厂对象 27 sessionFactory = config.buildSessionFactory(serviceRegistry); 28 29 //回话对象 30 session = sessionFactory.openSession(); 31 32 //打开事物 33 transaction = session.beginTransaction(); 34 } 35 36 @After 37 public void destory() { 38 transaction.commit(); 39 session.close(); 40 sessionFactory.close(); 41 } 42 43 @Test 44 public void TestSaveStudents() { 45 Students students = new Students(3, "小白", "男", new Date(), "武当山"); 46 session.save(students); 47 } 48 }
标签:cti mini javabean min character save 4.0 hibernate market
原文地址:http://www.cnblogs.com/sirxb/p/6172069.html