先整合hibernate 在搞定struts2
首先创建个实体类
User.class
package com.javalong.ssh.entity;
import java.io.Serializable;
public class User implements Serializable{
private static final long serialVersionUID = 2444775223411332979L;
private int id;
private String username;
private String password;
private String phone;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password="
+ password + ", phone=" + phone + ", email=" + email + "]";
}
}
使用过hibernate的都知道每个实体类对应一个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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.javalong.ssh.entity.User" table="t_user" catalog="ygh">
<id name="id" type="integer">
<column name="id" />
<generator class="identity"></generator>
</id>
<property name="username" type="string">
<column name="username" length="20" />
</property>
<property name="password" type="string">
<column name="password" length="20" />
</property>
<property name="email" type="string">
<column name="email" length="20" />
</property>
<property name="phone" type="string">
<column name="phone" length="20" />
</property>
</class>
</hibernate-mapping>
我使用的是mysql数据库,建表语句:
上面准备工作都做完后,开始配置ssh.xml。
首先配置 dataSource,数据源。
使用连接池,需要引入commons-dbcp-1.2.2.jar
既然要连接mysql,当然要mysql的驱动咯。引入mysql-connector-java-5.1.8-bin.jar
简单的一个使用连接池的数据源bean配置完成。
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
<property name="url" value="jdbc:mysql://localhost:3306/ygh"></property>
<property name="minIdle" value="2"></property>
<property name="maxIdle" value="3"></property>
</bean>
既然,我们要使用的hibernate,那么我们就需要sessionFactory为我们创建session,下面我们还需要配置sessionFactory的bean。
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/javalong/ssh/entity/User.hbm.xml</value>
</list>
</property>
</bean>
这里需要引入2个包,一个是spring.jar,一个是hibernate3.jar.
这里还有许多包未引入,等下在报错后,我们一个一个引入。
有了sessionFactory后,我们其实是可以直接将其注入到某个类中,然后直接使用sessionFactory 产生session,然后用hibernate的只是操作数据库。
但是spring为我们提供了hibernate模板类,可以更加方便使用hibernate,需要继承HibernateDaoSupport
我们先写个接口:
publicinterface UserDao {
void insert(User user);
void update(User user);
void delete(intid);
List<User> findAll();
User findById(intid);
}
接着写hibernate实现
package com.javalong.ssh.dao;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.javalong.ssh.entity.User;
public class UserHibernateImpl extends HibernateDaoSupport implements UserDao{
@Override
public void insert(User user) {
this.getHibernateTemplate().save(user);
}
@Override
public void update(User user) {
}
@Override
public void delete(int id) {
}
@Override
public List<User> findAll() {
return null;
}
@Override
public User findById(int id) {
User user=(User)this.getHibernateTemplate().get(User.class, id);
return user;
}
}
这里简单的实现findById方法。
使用这个类的前提是需要注入sessionFactory,所以我们要在ssh.xml里面配置下。
<bean id="userHibernateImpl" class="com.javalong.ssh.dao.UserHibernateImpl">
<property name="sessionFactory" ref="mySessionFactory"></property>
</bean>
好 ,下面我们可以测试下我们spring整合hibernate的成果了。
测试代码:
public class TestHibernate {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("ssh.xml");
UserDao userDao=(UserDao)context.getBean("userHibernateImpl");
User user=userDao.findById(1);
System.out.println(user);
}
}
运行:
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
缺少commons-logging.jar,加入
运行:
Caused by: java.lang.ClassNotFoundException: org.apache.commons.pool.impl.GenericObjectPool
缺少commons-pool.jar,加入
运行:
Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException
缺少dom4j-1.6.1.jar,加入
运行:
Caused by: java.lang.ClassNotFoundException: javax.transaction.TransactionManager
缺少jta.jar,加入
运行:
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.SequencedHashMap
缺少jta.jar,加��
运行
Caused by: java.lang.ClassNotFoundException: net.sf.cglib.proxy.CallbackFilter
缺少cglib-nodep-2.1_3.jar,加入
运行
Hibernate:
select
user0_.id as id0_0_,
user0_.username as username0_0_,
user0_.password as password0_0_,
user0_.email as email0_0_,
user0_.phone as phone0_0_
from
ygh.t_user user0_
where
user0_.id=?
User [id=1, username=java_along, password=1234, phone=18768102874, email=840727854@qq.com]
成功输出。。。。
一步步加入jar包 虽然会很麻烦,但是能更好的帮你了解这项技术。
至此,hibernate整合完成~