1.配置spring.xml中的dataSource和sessionFactory
<!-- 配置dataSource选用c3p0方式 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/makefriends?useUnicode=true&characterEncoding=utf8" />
<property name="user" value="root" />
<property name="password" value="123" />
<property name="minPoolSize" value="2" />
<property name="maxPoolSize" value="50" />
<property name="initialPoolSize" value="10" />
<property name="maxIdleTime" value="60" />
<property name="acquireIncrement" value="2" />
</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/yun/model/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
</props>
</property>
</bean>
2.编写HibernateUtil公共类
package com.yun.utility;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HibernateUtil {
private static SessionFactory factory=null;
static {
try {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("config/springmvc.xml");
factory = (SessionFactory) ctx.getBean("sessionFactory");
} catch (HibernateException e) {
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory() {
return factory;
}
public static Session getSession() {
Session session = (factory != null) ? factory.openSession() : null;
return session;
}
public static void closeSession(Session session) {
if (session != null) {
if (session.isOpen()) {
session.close();
}
}
}
}
3.编写类进行测试
package com.yun.dao.user.impl;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateTemplate;
import com.yun.dao.user.IUserDao;
import com.yun.model.User;
import com.yun.utility.HibernateUtil;
public class IUserDaoImpl implements IUserDao {
public void save(User user) {
// TODO Auto-generated method stub
Session session=HibernateUtil.getSession();
Transaction tran= session.beginTransaction();
session.save(user);
tran.commit();
session.close();
}
public List<User> findBySex(String userSex) {
// TODO Auto-generated method stub
Session session=HibernateUtil.getSession();
Transaction tran= session.beginTransaction();
Query q=session.createQuery("from User");
List<User> userList=q.list();
session.close();
return userList;
}
public User findById(Integer userId) {
// TODO Auto-generated method stub
Session session=HibernateUtil.getSession();
Transaction tran= session.beginTransaction();
Query q=session.createQuery("from User where userId="+userId);
User userList=(User) q.uniqueResult();
session.close();
return userList;
}
}