【SSH (七)】 struts2整合hibernate3

来源:互联网 发布:linux用户权限管理 编辑:程序博客网 时间:2024/05/21 12:39

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

1,首先下载jar包:

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

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

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

这里要用的与hibernate有关的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{@Resourceprivate JdbcTemplate JdbcTemplate;@Resourceprivate SessionFactory sessionFactory;@Overridepublic User getUserByUandP(UserForm userForm) {// TODO Auto-generated method stubUser 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表的全部数据。


访问方式没有变化。


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


数据库截图:



源码:

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

0 0
原创粉丝点击