java 应用使用hibernate 实例

来源:互联网 发布:动感单车品牌知乎 编辑:程序博客网 时间:2024/05/17 01:34


第一步 :下载相关的库,并导入工程。这里就不详细说明了 


 hibernate 地址:本人版本4.2.2 final   http://www.hibernate.org/


第二步:hibernate.cfg.xml 的配置   


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC 
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>


<!-- 通过hibernate 直接连接数据库 -->
<!-- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> -->
<!-- <property name="hibernate.connection.url">jdbc:mysql://localhost/db_meigui -->
<!-- </property> -->
<!-- <property name="hibernate.connection.username">root</property> -->
<!-- <property name="hibernate.connection.password">123456</property> -->
<!-- <property name="connection.characterEncoding">utf-8 -->
<!-- </property> -->




<!-- 通过proxool 连接数据库 -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.ProxoolConnectionProvider</property>
<property name="hibernate.proxool.pool_alias">timalias</property>
<property name="hibernate.proxool.xml">com/hibernate/proxool.xml</property>




<!-- <property name="javax.persistence.validation.mode">none</property> -->
<!-- 自动创建以及更新数据库表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 指定相关的数据表结构 -->
<mapping resource="com/hibernate/UserInfo.hbm.xml"></mapping>


</session-factory>
</hibernate-configuration>


UserInfo.hbm.xml  文件内容

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC


                            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"


                            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >


<hibernate-mapping package="com.hibernate">


<class name="User" table="tbl_user">
<id name="orgid" column="orgId" type="java.lang.Long">
<generator class="native" />
</id>


<property name="name" column="name" type="java.lang.String"
not-null="true" />


</class>
</hibernate-mapping>



proxool.xml 文件内容


<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
<proxool>
<alias>timalias</alias>
<!--数据源的别名 -->
<driver-url>jdbc:mysql://localhost/db_meigui</driver-url>
<!--url连接串 -->
<driver-class>com.mysql.jdbc.Driver</driver-class>
<!--驱动类 -->
<driver-properties>
<property name="user" value="root" />
<!--用户名 -->
<property name="password" value="123456" />
<!--密码 -->
</driver-properties>
<!--最大连接数(默认5个),超过了这个连接数,再有请求时,就排在队列中等候,最大的等待请求数由maximum-new-connections决定 -->
<maximum-connection-count>5</maximum-connection-count>
<!--最小连接数(默认2个) -->
<minimum-connection-count>2</minimum-connection-count>
<!--proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回收,超时的销毁 默认30秒 -->
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<!--没有空闲连接可以分配而在队列中等候的最大请求数,超过这个请求数的用户连接就不会被接受 -->
<maximum-new-connections>10</maximum-new-connections>
<!--最少保持的空闲连接数(默认2个) -->
<prototype-count>2</prototype-count>
<!--在使用之前测试 -->
<test-before-use>flase</test-before-use>
<!--用于保持连接的测试语句 -->
<house-keeping-test-sql>select * tbl_user where rigID=0</house-keeping-test-sql>
</proxool>
</something-else-entirely>


java 代码


public class HibernateUtil {


private static SessionFactory sessionFactory;


public HibernateUtil() {


}


static {
Configuration config = new Configuration()
.configure("com/hibernate.cfg.xml");


ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(config.getProperties()).buildServiceRegistry();
sessionFactory = config.buildSessionFactory(serviceRegistry);

}


public static SessionFactory getSessionFactory() {
return sessionFactory;
}


public static Session getSession() {
return sessionFactory.openSession();
}


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HibernateUtil hiUtil = new HibernateUtil();


String sql = "from User";
Query query = hiUtil.getSession().createQuery(sql);
query.list();
List<User> list = query.list();
for (User u : list) {
System.out.println(u.getName());
}
}


}


如果有什么问题,请细心检查一下红色字体的部分是否路径对了。


原创粉丝点击