标签:test 最大 integer 文件 技术分享 用户名 运行 configure nbsp
1.1Hibernate的持久化类状态
1.1.1Hibernate的持久化类状态
1.1.2 三种持久化对象的状态
1.1.3区分三种持久化对象的状态
新建cn.hibernate3.demo1.Book.java类
package cn.hibernate3.demo1; /** * 实体类 * @author love */ public class Book { private Integer id; private String name; private Double price; private String author; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } }
配置Book.hbm.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!-- 引入约束 --> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="cn.hibernate3.demo1.Book" table="book"> <id name="id" column="id"> <generator class="native"/> </id> <property name="name" column="name" type="java.lang.String"/> <property name="price" column="price" type="java.lang.Double"/> <property name="author" column="author" type="java.lang.String"/> </class> </hibernate-mapping>
配置核心映射文件(hibernate.cfg.xml文件)
<?xml version="1.0" encoding="UTF-8"?> <!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> <!-- 配置数据库的基本信息 --> <!-- 驱动的名称 --> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property> <!-- 访问数据库的url --> <property name="hibernate.connection.url"> jdbc:mysql:///hibernate_day02 </property> <!-- 用户名 --> <property name="hibernate.connection.username">root</property> <!-- 密码 --> <property name="hibernate.connection.password">root</property> <!-- 方言 --> <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property> <!-- C3P0连接池设定--> <!-- 使用c3po连接池 配置连接池提供的供应商--> <property name="connection.provider_class"> org.hibernate.connection.C3P0ConnectionProvider </property> <!--在连接池中可用的数据库连接的最少数目 --> <property name="c3p0.min_size">5</property> <!--在连接池中所有数据库连接的最大数目 --> <property name="c3p0.max_size">20</property> <!--设定数据库连接的过期时间,以秒为单位, 如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 --> <property name="c3p0.timeout">120</property> <!--每3000秒检查所有连接池中的空闲连接 以秒为单位--> <property name="c3p0.idle_test_period">3000</property> <!-- 可选配置 --> <!-- 显示SQL --> <property name="hibernate.show_sql">true</property> <!-- 格式化SQL --> <property name="hibernate.format_sql">true</property> <!-- hbm:映射 2:to ddl:create drop alter --> <property name="hibernate.hbm2ddl.auto">update</property> <mapping resource="cn/hibernate3/demo1/Book.hbm.xml" /> </session-factory> </hibernate-configuration>
新建HibernateUtils.java类
package cn.utils; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; /** * Hibernate抽取工具类 */ public class HibernateUtils { private static Configuration configuration; private static SessionFactory sessionFactory; static{ configuration = new Configuration().configure(); sessionFactory = configuration.buildSessionFactory(); } public static Session openSession(){ return sessionFactory.openSession(); } public static void main(String[] args) { openSession(); } }
运行HibernateUtils工具类,即可发现创建了book表
测试
package cn.hibernate3.demo1; import org.hibernate.Session; import org.hibernate.Transaction; import org.junit.Test; import cn.utils.HibernateUtils; public class HibernateTest1 { @Test //区分持久化对象的三种装填 public void demo1(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //向数据库中保存一本图书 Book book = new Book();//瞬时态:没有唯一标识OID,没有与session关联 book.setName("Hibernate开发"); book.setAuthor("哈哈"); book.setPrice(65.0); session.save(book);//持久态:有唯一标识OID,与session关联 tx.commit(); session.close(); book.setName("Struts2开发");//脱管态:有唯一标识,没有与session关联 } }
1.1.4
标签:test 最大 integer 文件 技术分享 用户名 运行 configure nbsp
原文地址:http://www.cnblogs.com/xuweiweiailixing/p/6761301.html