hibernate c3p0配置

要使用c3p0,首先在hibernate中选择required的jar和optional的c3p0目录的jar包,再加上mysql的驱动。
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-configuration
 PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory>
	<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
	<property name="connection.url">jdbc:mysql://localhost:3306/simpledb</property>
	<property name="connection.username">root</property>
	<property name="connection.password">system</property>
	
	<property name="hibernate.c3p0.max_size">20</property>
	<property name="hibernate.c3p0.min_size">5</property>
	<property name="hibernate.c3p0.timeout">5000</property>
	<property name="hibernate.c3p0.max_statements">100</property>
	<property name="hibernate.c3p0.idle_test_period">3000</property>
	<property name="hibernate.acquire_increment">2</property>
	<property name="hibernate.c3p0.validate">true</property>
	
	<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialects</property>
	<property name="hbm2ddl.auto">create</property>
	
	<mapping resource="com/lin/domain/News.hbm.xml"></mapping>
	</session-factory>
</hibernate-configuration>
准备映射的类。
package com.lin.domain;

public class News {
	private int id;
	private String title;
	private String content;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

}

映射文件:News.hbm,xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2014-3-24 19:26:00 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.lin.domain.News" table="news">
        <id name="id" type="int">
            <column name="id" />
            <generator class="increment" />
        </id>
        <property name="title" type="java.lang.String">
            <column name="title" />
        </property>
        <property name="content" type="java.lang.String">
            <column name="content" />
        </property>
    </class>
</hibernate-mapping>
测试代码:

                System.out.println("starting ............");
Configuration conf = new Configuration().configure();
SessionFactory factory = conf.buildSessionFactory();
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
News n = new News();
n.setId(1);
n.setTitle("wanku");
n.setContent("this is wanku project!!!");
session.save(n);
tx.commit();
session.close();
factory.close();
System.out.println("ending................");

hibernate c3p0配置,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。