Recently, I have to digest a little more knowledge. I write notes in my blog for easy viewing
You can download the hibernate jar package you need on the official website
Configuring Sqlserver database in Hibernate
<?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>
<!-- Configuration database JDBC drive -->
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:50044;DatabaseName=NetData</property>
<!-- Configure database connection url -->
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<!-- Configuration database user name -->
<property name = "hibernate.connection.username">sa</property>
<!-- Configure database password -->
<property name = "hibernate.connection.password">xxxxxx</property>
<!-- Output runtime build sql Sentence -->
<property name="show_sql">true</property>
<!-- Configure database dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<!-- Configure the number of connection pools -->
<property name="hibernate.jdbc.batch_size">16</property>
<!-- List mapping files -->
<mapping resource="com/javaBean/Card.hbm.xml"/>
</session-factory>
</hibernate-configuration>
List the mapping file mapping is to accurately map to the xxx.hbm.xml file in the javabean. When writing the mapping, you can use the Ctrl + mouse arrow to see if it can map to the xxx.hbm.xml file
Card.hbm.xml mapping file (this is a self created one)
<?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>
<!-- For persistent classes Card Configuration of mappings for -->
<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>
In this way, configure the hibernate environment and name a HibernateInitialize class. The implementation method is as follows:
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 object
static {
try{
//Load Hibernate profile
Configuration cfg = new Configuration().configure();
sessionFactory = cfg.buildSessionFactory();//Instantiate SessionFactory object
}catch(Exception e) {
System.err.println("Failed to create session factory");
e.printStackTrace();
}
}
/**
* Get 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;
}
/**
* Rebuild session factory
*/
private static void rebuildSessionFactory() {
// Method stubs generated automatically by TODO
try {
// Load Hibernate profile
Configuration cfg = new Configuration().configure();
sessionFactory = cfg.buildSessionFactory();
}catch(Exception e) {
System.out.println("Failed to rebuild session factory");
e.printStackTrace();
}
}
/**
* Get SessionFactory object
* @return SessionFactory object
*/
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* Close Session
* @throws HibernateException
*/
public static void closeSession() throws HibernateException{
Session session =(Session)threadLocal.get();
threadLocal.set(null);
if(session != null) {
session.close();
}
}
}
Configuring MySQL database in Hibernate
<?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>
<!-- Configure database connection url -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- Configuration database JDBC drive -->
<property name ="connection.url">jdbc:mysql://xxx.xxx.xxx.xxx:3306/wcmdb</property>
<!-- Configuration database name -->
<property name = "connection.username">root</property>
<!-- Database connection password -->
<property name = "connection.password">xxxxx</property>
<!-- Hibernate dialect -->
<property name = "dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Printing sql Sentence -->
<property name="show_sql">true</property>
<!-- Connection pool size -->
<property name="connection.pool_size">1</property>
<!-- Mapping file -->
<mapping resource="com/javaBean/Login.hbm.xml"/>
<mapping resource="com/javaBean/Goods.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Specific implementation:
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();
}