最近要消化的知识有点多,多以特地将笔记写道 博客中,方便查看
需要到的hibernate jar 包 ,大家可以自行到官网下载
Hibernate 中 配置 Sqlserver 数据库
<?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>
<!-- 配置数据库JDBC驱动 -->
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:50044;DatabaseName=NetData</property>
<!-- 配置数据库连接url -->
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<!-- 配置数据库用户名 -->
<property name = "hibernate.connection.username">sa</property>
<!-- 配置数据库密码 -->
<property name = "hibernate.connection.password">xxxxxx</property>
<!-- 输出运行时生成sql语句 -->
<property name="show_sql">true</property>
<!-- 配置数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<!-- 配置连接池数量 -->
<property name="hibernate.jdbc.batch_size">16</property>
<!-- 列出映射文件 -->
<mapping resource="com/javaBean/Card.hbm.xml"/>
</session-factory>
</hibernate-configuration>
列出映射文件mapping
是要准确映射到 javabean中的 xxx.hbm.xml 文件,在写完映射的时候可以用Ctrl + 鼠标箭头最准 看看是否能映射到 xxx.hbm.xml 文件
Card.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>
<!-- 对持久化类Card的映射的配置 -->
<class name="com.javaBean.Card" table="Card">
<!-- -->
<id name="ID" column="ID" type="string">
<generator class="assigned"/>
</id>
<property name="Password" type="string" not-null="true" length="50">
<column name="Password"/>
</property>
<property name="Balance" type="int" not-null="true">
<column name="Balance"/>
</property>
<property name="UserName" type="string" not-null="true" length="50">
<column name="UserName"/>
</property>
</class>
</hibernate-mapping>
这样子 配置,再将hibernate 的环境配置好,命名一个HibernateInitialize类,实现方法如下:
package com.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateInitialize {
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static SessionFactory sessionFactory = null;//SessionFactory对象
static {
try{
//加载Hibernate配置文件
Configuration cfg = new Configuration().configure();
sessionFactory = cfg.buildSessionFactory();//实例化SessionFactory对象
}catch(Exception e) {
System.err.println("创建会话工厂失败");
e.printStackTrace();
}
}
/**
* 获取Session
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException{
Session session = (Session) threadLocal.get();
if(session ==null || !session.isOpen()) {
if(sessionFactory ==null) {
rebuildSessionFactory();
}
session =(sessionFactory!=null)?sessionFactory.openSession():null;
threadLocal.set(session);
}
return session;
}
/**
* 重建会话工厂
*/
private static void rebuildSessionFactory() {
// TODO 自动生成的方法存根
try {
// 加载Hibernate配置文件
Configuration cfg = new Configuration().configure();
sessionFactory = cfg.buildSessionFactory();
}catch(Exception e) {
System.out.println("重建会话工厂失败");
e.printStackTrace();
}
}
/**
* 获取SessionFactory对象
* @return SessionFactory对象
*/
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* 关闭Session
* @throws HibernateException
*/
public static void closeSession() throws HibernateException{
Session session =(Session)threadLocal.get();
threadLocal.set(null);
if(session != null) {
session.close();
}
}
}
Hibernate 中 配置 MySQL数据库
<?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>
<!-- 配置数据库连接url -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 配置数据库JDBC驱动 -->
<property name ="connection.url">jdbc:mysql://xxx.xxx.xxx.xxx:3306/wcmdb</property>
<!-- 配置数据库名称 -->
<property name = "connection.username">root</property>
<!-- 数据库连接密码 -->
<property name = "connection.password">xxxxx</property>
<!-- Hibernate 方言 -->
<property name = "dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 打印sql语句 -->
<property name="show_sql">true</property>
<!-- 连接池大小 -->
<property name="connection.pool_size">1</property>
<!-- 映射文件 -->
<mapping resource="com/javaBean/Login.hbm.xml"/>
<mapping resource="com/javaBean/Goods.hbm.xml"/>
</session-factory>
</hibernate-configuration>
具体实现:
Session session=null;
try {
session=HibernateInitialize.getSession();
Query q = session.createSQLQuery("select * from goods").addEntity(Goods.class);
@SuppressWarnings("unchecked")
List<Goods> ls = q.list();
for(Goods li : ls) {
System.out.println( li.getId() +"," +li.getGoodsname() +","+li.getPrice() +"," +li.getPicture() );
}
req.setAttribute("ls",ls);
req.getRequestDispatcher("/GoodsInfo.jsp").forward(req, resp);
}catch(Exception e) {
e.printStackTrace();
}finally {
HibernateInitialize.closeSession();
}