首页 > 学院 > 开发设计 > 正文

ssh整合二(spring整合hibernate)

2019-11-08 00:38:23
字体:
来源:转载
供稿:网友

接上篇 在上篇文章中我们说过:

ssh整合的关系是:sPRing整合hibernate,struts 整合spring。

在这里我们首先实现第一个关系,spring整合hibernate. 下面用一个例子来叙述。 实现的业务是客户的注册功能。

1.创建表

sql代码:创建一个表,共四个字段。主键,用户名,密码和年龄。

create table t_user( id int primary key auto_increment, username varchar(50), passWord varchar(32), age int );

2.po类

这里写图片描述

javabean

四个字段加对应的getter和setter方法

public class User { private Integer id; private String username; private String password; private Integer age;映射文件<?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.scx.domain.User" table="t_user"> <id name="id"> <generator class="native"></generator> </id> <property name="username"></property> <property name="password"></property> <property name="age"></property> </class></hibernate-mapping>

3.dao层

spring提供 HibernateTemplate 用于操作PO对象,类似Hibernate session对象。

public class UserDaoImpl implements UserDao{ //需要spring注入模版 private HibernateTemplate hibernateTemplate; public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } public void save(User user) { hibernateTemplate.save(user); }}

4.service层

只有一个用户的注册业务

public class UserServiceImpl implements UserService{ private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } //用户注册 public void register(User user) { userDao.save(user); }}

5.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> <!-- 1.基本四项 --> <property name="hibernate.connection.driver_class">com.MySQL.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ssh</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123</property> <!-- 2.配置方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 3.sql语句 --> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <!-- 4.自动生成表(一般没用) --> <property name="hibernate.hbm2ddl.auto">true</property> <!-- 5.本地线程绑定 --> <property name="hibernate.current_session_context_class">thread</property> <!-- 6.导入映射文件 --> <mapping resource="com/scx/domain/User.cfg.xml"/> </session-factory></hibernate-configuration>

6.applicationcontext.xml

最重要的部分就在这里了、 在前面的dao层中我们说了hibernateTemplate需要spring注入。通过学习hibernate我们知道而hibernateTemplate需要从session工厂获得。所以首先要有一个session工厂bean.session工厂bean要获得的hibernate.cfg.xml可以通过configLocation参数来设置。

<?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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 1.加载hibernate.cfg.xml 配置sessionfactory工厂 *configLocation确定配置文件的位置 --> <bean name="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean> <!-- 2.创建模版 *底层使用session,session由sessionfactory获得 --> <bean name="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 3.dao --> <bean name="userDao" class="com.scx.dao.impl.UserDaoImpl"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean> <!-- 4.service --> <bean name="userService" class="com.scx.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"></property> </bean> <!-- 5.1事务管理 HibernateTransactionManager --> <bean name="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 5.2事务详情 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="register"/> </tx:attributes> </tx:advice> <!-- 5.3aop编程 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.scx.service..*.*(..))"/> </aop:config></beans>

7.测试

使用整合junit的方法进行测试不明白请看这里

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations= "classpath:applicationcontext.xml")public class TestApp { @Autowired private UserService userService; @Test public void test(){ User user=new User(); user.setAge(18); user.setUsername("张三"); user.setPassword("123456"); userService.register(user); }}

运行结果: 这里写图片描述 控制台成功输出sql语句。查看数据库这里写图片描述 结果正确。 到目前完成了spring整合hibernate。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表