• 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;

       }

    }

  • 相关阅读:
    install jprofiler for ubuntu
    android manifest相关属性
    install nginx for ubuntu
    Android shape
    mobile web for no cookie session
    Android布局属性
    什么是强类型,强类型集合
    radl (三) (转)
    几个.net 基础问题,自己回答了一些,请大家指教
    c#接口和抽象类的区别
  • 原文地址:https://www.cnblogs.com/forever2h/p/6757105.html
Copyright © 2020-2023  润新知