首页 > 编程语言 > 详细

SSH整合之spring整合hibernate

时间:2015-11-21 22:35:01      阅读:354      评论:0      收藏:0      [点我收藏+]

SSH整合要导入的jar包:

技术分享技术分享

MySQL中创建数据库

技术分享
create database ssh_db;
ssh_db

一、spring整合hibernate带有配置文件hibernate.cfg.xml

1、项目结构:

技术分享

2、新建接口UserDao及实现类UserDaoImpl;实现类中有HibernateTemplate 字段及setter方法,是用来执行数据库操作的。  

技术分享
package com.hjp.dao;

import com.hjp.domain.User;

/**
 * Created by JiaPeng on 2015/11/21.
 */
public interface UserDao {
    void save(User user);
}
接口UserDao
技术分享
package com.hjp.dao.impl;

import com.hjp.dao.UserDao;
import com.hjp.domain.User;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

import java.sql.SQLException;

/**
 * Created by JiaPeng on 2015/11/21.
 */
public class UserDaoImpl implements UserDao {

    HibernateTemplate hibernateTemplate;

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

    @Override
    public void save(User user) {
        this.hibernateTemplate.save(user);
    }
}
UserDao实现类UserDaoImpl

 3、新建接口UserService及实现类UserServiceImpl;实现类中有UserDao字段级setter方法,调用userDao中的公共方法

技术分享
package com.hjp.service;

import com.hjp.domain.User;

/**
 * Created by JiaPeng on 2015/11/21.
 */
public interface UserService {
    void register(User user);
}
接口UserService
技术分享
package com.hjp.service.impl;

import com.hjp.dao.UserDao;
import com.hjp.domain.User;
import com.hjp.service.UserService;

/**
 * Created by JiaPeng on 2015/11/21.
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void register(User user) {
        this.userDao.save(user);
    }
}
UserService实现类UserServiceImpl

4、新建hibernate.cfg.xml文件

技术分享
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!--基本四项-->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ssh_db</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">hjp123</property>
        <!--方言-->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <!--sql优化,显示,格式化-->
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <!--自动创建表-->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!--整合C3P0-->
        <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
        <!--添加映射文件-->
        <mapping resource="com/hjp/domain/User.hbm.xml"></mapping>
    </session-factory>
</hibernate-configuration>
hibernate.cfg.xml

5、新建applicationContext.xml文件

技术分享
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--获得sessionFactory,spring加载hibernate.cfg.xml文件-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    </bean>
    <!--hibernateTemplate模板-->
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!--dao-->
    <bean id="userDao" class="com.hjp.dao.impl.UserDaoImpl">
        <property name="hibernateTemplate" ref="hibernateTemplate"></property>
    </bean>
    <!--service-->
    <bean id="userService" class="com.hjp.service.impl.UserServiceImpl">
       <property name="userDao" ref="userDao"></property>
    </bean>
    <!--事务管理,首先有事务管理器-->
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!--事务通知-->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="register"/>
        </tx:attributes>
    </tx:advice>
    <!--aop配置-->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.hjp.service..*.*(..))"></aop:advisor>
    </aop:config>
</beans>
applicationContext

6、新建测试类TestApp

技术分享
package com.hjp.test;

import com.hjp.domain.User;
import com.hjp.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by JiaPeng on 2015/11/21.
 */
public class TestApp {
    @Test
    public void demo1() {
        User user=new User();
        user.setUsername("Jack");
        user.setPassword("123");
        String xmlPath = "applicationContext.xml";
        ApplicationContext applicationContext= (ApplicationContext) new ClassPathXmlApplicationContext(xmlPath);
        UserService userService= (UserService) applicationContext.getBean("userService");
        userService.register(user);
    }
}
TestApp

 

SSH整合之spring整合hibernate

原文:http://www.cnblogs.com/hujiapeng/p/4984276.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!