首先建立java Project工程

点击Finish完成
添加Hibernate和Spring所需要的jar包还有Mysql连接的jar包

创建Dao层,Dao层实现,Model层,Service层
DAO层代码:IUserDao
- package org.zhy.demo.dao;
-
- import java.sql.SQLException;
-
- import org.zhy.demo.dao.model.UserInfo;
- public interface IUserDao {
-
- public void saveUser(UserInfo user) throws SQLException;
-
- public void delUser(UserInfo user) throws SQLException;
-
- public void editUsre(UserInfo user) throws SQLException;
-
- public UserInfo getUserById(int id) throws SQLException;
-
- }
IUuserDao层实现(暂时空实现)
- package org.zhy.demo.dao.impl;
-
- import java.sql.SQLException;
-
- import org.zhy.demo.dao.IUserDao;
- import org.zhy.demo.dao.model.UserInfo;
-
- /**
- * IUserDao实现,配置完Spring及Hibernate文件后实现此类
- * @author Administrator
- *
- */
- public class IUserDaoImpl implements IUserDao {
-
- @Override
- public void saveUser(UserInfo user) throws SQLException {
-
- }
-
- @Override
- public void delUser(UserInfo user) throws SQLException {
-
- }
-
- @Override
- public void editUsre(UserInfo user) throws SQLException {
-
- }
-
- @Override
- public UserInfo getUserById(int id) throws SQLException {
- return null;
- }
-
- }
Model代码
- package org.zhy.demo.dao.model;
-
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
-
- @Entity
- public class UserInfo {
-
-
- private int id;
- private String name;
- private String title;
-
- @Id
- @GeneratedValue
- 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 String getTitle() {
- return title;
- }
-
- public void setTitle(String title) {
- this.title = title;
- }
-
- }
下面添加Spring配置文件
在Src目录下添加SpringContext.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: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-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
- ">
- <aop:config></aop:config>
-
- <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
-
- <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://localhost/student"></property>
- <property name="username" value="root" />
- <property name="password" value="root" />
-
- <property name="minIdle" value="5" />
-
- <property name="maxIdle" value="30" />
- <!--
- 当数据库连接因为某种原因断掉之后,再重新从连接池中拿另外一个连接时实际上这个连接可能
- 已经无效,所以为了确保所拿到的连接全都有效需要在获取连接,返回连接以及连接空闲时进行
- 有效性验证 下面3个设置为ture时进行验证,默认为false
- -->
-
- <property name="testOnBorrow" value="true" />
-
- <property name="testOnReturn" value="true" />
-
- <property name="testWhileIdle" value="true" />
-
- </bean>
-
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
- <property name="dataSource" ref="datasource" />
-
- <property name="annotatedClasses">
- <list>
- <value>org.zhy.demo.dao.model.UserInfo</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>
-
- <prop key="hibernate.hbm2ddl.auto">update</prop>
- <prop key="hibernate.current_session_context_class">thread</prop>
-
- <prop key="hibernate.max_fetch_depth">1</prop>
-
- <prop key="hibernate.use_sql_comments">true</prop>
- </props>
- </property>
- </bean>
-
- <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
- <property name="sessionFactory" ref="sessionFactory"></property>
- </bean>
-
- <aop:config>
- <aop:pointcut id="txMethod" expression="execution(* org.zhy.demo.dao.impl.*DaoImpl.*(..))" />
- <aop:advisor advice-ref="txAdvice" pointcut-ref="txMethod"/>
- </aop:config>
-
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="save*" propagation="REQUIRED" />
- <tx:method name="update*" propagation="REQUIRED" />
- <tx:method name="add*" propagation="REQUIRED" />
- <tx:method name="delete*" propagation="REQUIRED" />
- <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
- <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
- <tx:method name="*" />
- </tx:attributes>
- </tx:advice>
-
-
- <bean name="userDao" class="org.zhy.demo.dao.impl.IUserDaoImpl" >
- <property name="sessionFactory" ref="sessionFactory"></property>
- </bean>
- <bean name="userService" class="org.zhy.demo.service.UserService" />
-
- </beans>
修改DAO实现类
- package org.zhy.demo.dao.impl;
-
- import java.sql.SQLException;
-
- import org.hibernate.SessionFactory;
- import org.springframework.orm.hibernate3.HibernateTemplate;
- import org.zhy.demo.dao.IUserDao;
- import org.zhy.demo.dao.model.UserInfo;
-
- public class IUserDaoImpl implements IUserDao {
-
- private HibernateTemplate hibernateTemplate;
-
- public void setSessionFactory(SessionFactory sessionFactory) {
- this.hibernateTemplate = new HibernateTemplate(sessionFactory);
- }
-
- @Override
- public void saveUser(UserInfo user) throws SQLException {
- hibernateTemplate.save(user);
- }
-
- @Override
- public void delUser(UserInfo user) throws SQLException {
- hibernateTemplate.delete(user);
- }
-
- @Override
- public void editUsre(UserInfo user) throws SQLException {
- hibernateTemplate.update(user);
- }
-
- @Override
- public UserInfo getUserById(int id) throws SQLException {
- UserInfo user = (UserInfo) hibernateTemplate.get(UserInfo.class, id);
- return user;
- }
-
- }
junit测试
- @Test
- public void saveUserTest() throws SQLException{
- ApplicationContext context= new ClassPathXmlApplicationContext("/SpringContext.xml");
- UserService service =(UserService) context.getBean("userService");
-
- UserInfo user = new UserInfo();
- user.setName("T`");
- user.setTitle("CSDN BLOG");
- service.addUserInfo(user);
- }
项目源码地址:http://download.csdn.net/detail/qq7342272/4539552
源码中的注释及代码是比较全的
出自:http://blog.csdn.net/qq7342272/article/details/7928814
Spring整合Hibernate图文步骤
原文:http://www.cnblogs.com/OnlyCT/p/4359736.html