SSH整合之spring整合hibernate
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
二、spring整合hibernate,没有hibernate.cfg.xml配置文件
从含有hibernate.cfg.xml配置文件中项目进行删改
1、删除hibernate.cfg.xml配置文件,修改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">
<!--配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssh_db"></property>
<property name="user" value="root"></property>
<property name="password" value="hjp123"></property>
</bean>
<!--获得sessionFactory,spring不再加载hibernate.cfg.xml文件-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--其他属性设置-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!--添加映射文件,一般用mappingLocations
mappingResources 加载映射文件,从src下获取,必须确定指定资源 com/hjp/domain/User.hbm.xml
mappingLocations 建议带着"classpath:",可用通配符
classpath:com/hjp/*/User.hbm.xml 代表目录任意
classpath:com/hjp/domain/*.hbm.xml 代表文件名任意
mappingDirectoryLocations 设置目录 classpath:com/hjp/domain
mappingJarLocations 从jar中获得配置文件
-->
<property name="mappingLocations" value="classpath:com/hjp/domain/User.hbm.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.xml
2、删除hibernate模板,修改applicationContext.xml文件及使UserDao继承HibernateDaoSupport类
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 org.springframework.orm.hibernate3.support.HibernateDaoSupport; import java.sql.SQLException; /**
* Created by JiaPeng on 2015/11/21.
*/
public class UserDaoImpl extends HibernateDaoSupport implements UserDao { // HibernateTemplate hibernateTemplate;
//
// public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
// this.hibernateTemplate = hibernateTemplate;
// } @Override
public void save(User user) {
this.getHibernateTemplate().save(user);
}
}
UserDaoImpl
<?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">
<!--配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssh_db"></property>
<property name="user" value="root"></property>
<property name="password" value="hjp123"></property>
</bean>
<!--获得sessionFactory,spring不再加载hibernate.cfg.xml文件-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--其他属性设置-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!--添加映射文件,一般用mappingLocations
mappingResources 加载映射文件,从src下获取,必须确定指定资源 com/hjp/domain/User.hbm.xml
mappingLocations 建议带着"classpath:",可用通配符
classpath:com/hjp/*/User.hbm.xml 代表目录任意
classpath:com/hjp/domain/*.hbm.xml 代表文件名任意
mappingDirectoryLocations 设置目录 classpath:com/hjp/domain
mappingJarLocations 从jar中获得配置文件
-->
<property name="mappingLocations" value="classpath:com/hjp/domain/User.hbm.xml"></property>
</bean>
<!--hibernateTemplate模板
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>-->
<!--dao
UserDaoImpl继承了HibernateDaoSupport类,所以只注入sessionFactory即可,在UserDaoImpl实现类
中可以通过getHibernateTemplate()方法得到hibernate模板对象
-->
<bean id="userDao" class="com.hjp.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></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.xml
补充
package com.hjp.domain; /**
* Created by JiaPeng on 2015/11/21.
*/
public class User {
private Integer id;
private String username;
private String password;
private Integer age; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} 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 Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
}
}
User
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.hjp.domain.User" table="t_user">
<id name="id">
<generator class="native"/>
</id>
<property name="username" length="50"/>
<property name="password" length="32"/>
<property name="age"/>
</class>
</hibernate-mapping>
SSH整合之spring整合hibernate的更多相关文章
- Maven环境下搭建SSH框架之Spring整合Hibernate
		
© 版权声明:本文为博主原创文章,转载请注明出处 1.搭建环境 Spring:4.3.8.RELEASE Hibernate:5.1.7.Final MySQL:5.7.17 注意:其他版本在某些特性 ...
 - 【Saas-export项目】--项目整合(spring整合MVC)
		
转: [Saas-export项目]--项目整合(spring整合MVC) 文章目录 Spring整合SpringMVC(export_web_manager子工程) (1)log4j.propert ...
 - 二 SSH整合:Spring整合Hibernate,无障碍整合&无核心配置整合,Hibernate模版常用方法,
		
重建SSH项目 java项目可以直接复制,但是web项目除了改名字还要该配置,如下: 方式一:无障碍整合:带Hibernate配置文件 <?xml version="1.0" ...
 - JAVAEE——SSH三大框架整合(spring+struts2+hibernate)
		
一.整合原理 二.导包(41个) 1.hibernate (1)hibernate/lib/required (2)hibernate/lib/jpa | java persist api java的 ...
 - ssh整合思想 Spring与Hibernate的整合 项目在服务器启动则自动创建数据库表
		
Spring整合Hibernate Spring的Web项目中,web.xml文件会自动加载,以出现欢迎首页.也可以在这个文件中对Spring的配置文件进行监听,自启动配置文件, 以及之前Struts ...
 - ssh整合思想 Spring与Hibernate的整合ssh整合相关JAR包下载 .MySQLDialect方言解决无法服务器启动自动update创建表问题
		
除之前的Spring相关包,还有structs2包外,还需要Hibernate的相关包 首先,Spring整合其他持久化层框架的JAR包 spring-orm-4.2.4.RELEASE.jar ( ...
 - SSH框架之Spring+Struts2+Hibernate整合篇
		
回顾 -Hibernate框架 ORM: 对象关系映射.把数据库表和JavaBean通过映射的配置文件映射起来, 操作JavaBean对象,通过映射的配置文件生成SQL语句,自动执行.操作数据库. 1 ...
 - SSH(Struts、Spring、Hibernate)三大框架整合
		
1. 新建数据库ssh_db -> 新建表user_tb(id为主键,自动递增) 2. 导入jar包(struts.hibernate 和 spring) 3. 注册页面reg.jsp,将表单的 ...
 - SSH Struts2+hiberante+Spring整合
		
使用SSH框架编写学生信息: 一.新建Java工程: (1)建立好Java各层级之间的结构:业务处理层dao,数据模型层domain,页面请求处理层(Struts2 MVC层)action,servi ...
 
随机推荐
- 在创维E900-S悦Me盒子上安装第三方软件
			
0x00 不甘寂寞 创维E900-S这款悦Me盒子功能还算可以,但不能接受它禁止安装第三方软件这一点.网上搜了半天,可能是比较新的机型没人关注,找不到任何方法,只好自己动手试试. 0x01 Fiddl ...
 - 每日一SQL-善用DATEADD和DATEDIFF
			
转自:http://www.dotblogs.com.tw/lastsecret/archive/2010/10/04/18097.aspx 上個星期去Tech-Day聽了幾場有趣的課,其中一堂是楊志 ...
 - Linux常用指令---工作
			
查看所有用户cat /etc/passwd 复制整个目录cp -ri A/B/* A1/B1/ 若复制过程中询问是否覆盖,输入y按回车 另外若A A1不在同一目录下,最好填绝对路径,就是/xxx/xx ...
 - Linux命令学习
			
Linux命令学习 Ubuntu常用快捷键 •Ctrl+Alt+T: 打开终端 •Ctrl+Shift+T: 新建标签页 •Tab: 终端中命令补全 •Alt+数字N: 终端中切换到第N个标签页 •↑ ...
 - 如何使用GitHub?
			
我们一直用GitHub作为免费的远程仓库,如果是个人的开源项目,放到GitHub上是完全没有问题的.其实GitHub还是一个开源协作社区,通过GitHub,既可以让别人参与你的开源项目,也可以参与别人 ...
 - Cordova开发总结(插件篇)
			
最近刚刚做完一个用Cordova开发了一款电子商务的应用.在选用Cordova前,我有考察过,国内的Appcan, Apicloud等等的解决方案.其实Appcan,ApiCloud的混合方案挺完整的 ...
 - IL指令大全
			
IL是.NET框架中中间语言(Intermediate Language)的缩写.使用.NET框架提供的编译器可以直接将源程序编译为.exe或.dll文件,但此时编译出来的程序代码并不是CPU能直接执 ...
 - 状态机——Javascript词法扫描示例
			
所谓的状态机实质其实很很简单,其存在的目的也是把大量复杂的处理分散,使处理变得简单化一些.状态机只有一个当前状态,并且在当前状态下根据输入进行处理,然后再决定是否改变当前状态,然后再处理下一个输入,如 ...
 - [bzoj 2005][NOI 2010]能量采集(容斥原理+递推)
			
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2005 分析:首先易得ans=∑gcd(x,y)*2+1 然后我就布吉岛了…… 上网搜了下题解, ...
 - CreateCompatibleDC与BitBlt  学习
			
CreateCompatibleDC与BitBlt CreateCompatibleDC 创建一个与指定设备一致的内存设备描述表. HDC CreateCompatibleDC(HDC hdc //设 ...