spring 之 spring整合hibernate

Posted forever_2h

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring 之 spring整合hibernate相关的知识,希望对你有一定的参考价值。

spring整合hibernate步骤如下:

  1. 新建java项目
  2. 导入jar包
  3. 编写vo类
    @Entity
    @Table(name="t_user")
    public class User implements Serializable{
        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        private int id;
        private String name;
        private int age;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
    }
  4. Hibernate映射文件
    <hibernate-mapping>
        <class name="cn.wh.vo.User" table="t_user">
            <id name="id">
                <generator class="native"></generator>
            </id>
            <property name="name"/>
            <property name="age"/>
        </class>
    </hibernate-mapping>

     

  5. 编写hibernate的配置文件
    <hibernate-configuration>
    <session-factory>
        <property name="myeclipse.connection.profile">mysql</property>
        <property name="connection.url">
            jdbc:mysql://localhost:3306/hibernate4
        </property>
        <property name="connection.username">root</property>
        <property name="connection.password">1111</property>
        <property name="connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <property name="hibernate.dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <mapping resource="cn/sxt/vo/User.hbm.xml" />
    </session-factory>
    </hibernate-configuration>
  6. 编写dao层
    public class UserDaoImpl implements UserDao{
        private SessionFactory sessionFactory;
        @Override
        public List<User> findAll() {
            return sessionFactory.openSession().createQuery("from User").list();
        }
        public void setSessionFactory(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
        }
    }

     

  7. 编写service
    public class UserServiceImpl implements UserService {
        private UserDao userDao;
        @Override
        public List<User> findAll() {
            return userDao.findAll();
        }
        public void setUserDao(UserDao userDao) {
            this.userDao = userDao;
        }
    }

     

  8. 编写spring的配置文件
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd 
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd">
        <!-- 整合hibernate第一种及配置方式 
        <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
        </bean>
        -->
        <!-- 整合hibernate第二种方式 -->
        <!-- 配置数据源 -->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/hibernate4"/>
            <property name="username" value="root"/>
            <property name="password" value="1111"/>
        </bean>
        <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <!-- 映射文件映射 
            <property name="mappingLocations">
                <list>
                    <value>classpath:cn/wh/vo/User.hbm.xml</value>
                </list>
            </property>
            -->
            <property name="annotatedClasses">
                <list>
                    <value>cn.wh.vo.User</value>
                </list>
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.format_sql">true</prop>
                </props>
            </property>
        </bean>
        <bean id="userDao" class="cn.wh.dao.impl.UserDaoImpl">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean> 
        <bean id="userService" class="cn.wh.service.impl.UserServiceImpl">
            <property name="userDao" ref="userDao"></property>
        </bean>
    </beans>

     

  9. 测试:
    public class UserServiceTest {
        @Test
        public void testFindAll(){
            ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
            UserService userService = ac.getBean(UserService.class);
            List<User> list = userService.findAll();
            System.out.println(list.size());
        }
    }

     

@Entity

@Table(name="t_user")

publicclass User implements Serializable{

   @Id

   @GeneratedValue(strategy=GenerationType.AUTO)

   privateintid;

   private String name;

   privateintage;

   publicint getId() {

      returnid;

   }

   publicvoid setId(int id) {

      this.id = id;

   }

   public String getName() {

      returnname;

   }

   publicvoid setName(String name) {

      this.name = name;

   }

   publicint getAge() {

      returnage;

   }

   publicvoid setAge(int age) {

      this.age = age;

   }

}

以上是关于spring 之 spring整合hibernate的主要内容,如果未能解决你的问题,请参考以下文章

Spring Hiernate整合

Spring Hiernate整合

Spring整合Hibernate

SSH整合

Spring与Hibernate整合,实现Hibernate事务管理

(转)hibernate-5.0.7+struts-2.3.24+spring-4.2.4三大框架整合