在​​【SSH (六)】struts2 整合 spring 并 连接数据库​​的基础上加入hibernate3。

1,首先下载jar包:

这是一个比较麻烦的过程,之前已经加入了struts2和spring的jar包,现在要加入hibernate3相关的。核心的是hibernate3.jar包,但是这个jar会依赖很多其他的jar包,一般会发生两个问题:

(1)导入的依赖jar包不全,这个只能根据bug信息依次添加;

(2)导入的jar包之间版本冲突;最好还是从之前的struts2-all包里添加,如果没有早从其他地方下载,尽量保持版本一致。

这里要用的与hibernate有关的jar如下图:

【SSH (七)】 struts2整合hibernate3_jar包

最后会有项目源码,可下载到上述jar,或者整个lib文件夹。


2,配置hibernate:

在beans-data.xml里添加sessionFactory的配置。

<bean id="sessionFactory"    
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>com/bean/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
</bean>


完整的在源码里。


3,定义实体:

User.class:

package com.bean;

public class User {

private int id;
private String username;
private String password;
private String info;

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 getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}

public String toString(){
return this.username+" "+this.password;
}

}


hibernate的映射文件:

<?xml version="1.0" encoding='GBK'?>    
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="com.bean">
<class name="User" table="[user]">
<id name="id" column="id">
<generator class="identity"></generator>
</id>
<property name="username" column="username" type="java.lang.String"
length="16"></property>
<property name="password" column="password" type="java.lang.String"
length="16"></property>
<property name="info" column="info" type="java.lang.String"
length="16"></property>
</class>
</hibernate-mapping>



4,修改dao层:

package com.dao;

import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.jdbc.core.JdbcTemplate;

import com.formbean.UserForm;

import com.bean.User;

public class LoginDaoImp implements LoginDao{

@Resource
private JdbcTemplate JdbcTemplate;
@Resource
private SessionFactory sessionFactory;
@Override
public User getUserByUandP(UserForm userForm) {
// TODO Auto-generated method stub
User user = new User();
user.setInfo("heheda");
user.setUsername("ly");
user.setPassword("123");

// String sql = "SELECT * FROM tttt";
// List<Map<String, Object>> list = JdbcTemplate.queryForList(sql);
// for (Map<String, Object> map : list) {
// System.out.println(map.get("hehe"));
// }

String hql = "from User";
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Query query = session.createQuery(hql);
List<User> list = query.list();
for (User user2 : list) {
System.out.println(user2);
}
transaction.commit();

session.close();
sessionFactory.close();
return new User();
}

}


用from子句查询user表的全部数据。


访问方式没有变化。

【SSH (七)】 struts2整合hibernate3_hibernate_02

确实是数据库中的数据,访问成功。


数据库截图:

【SSH (七)】 struts2整合hibernate3_jar包_03


源码:

​http://pan.baidu.com/s/1qX4Mv4S​