国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

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。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 通海县| 军事| 蒲城县| 贵德县| 琼海市| 同德县| 湾仔区| 渝北区| 桃园县| 离岛区| 盐池县| 宁乡县| 通辽市| 正宁县| 小金县| 惠州市| 额敏县| 肥西县| 儋州市| 易门县| 射洪县| 武陟县| 井陉县| 汤阴县| 中西区| 若羌县| 莫力| 安庆市| 通州区| 永平县| 赤壁市| 航空| 河南省| 崇州市| 宁海县| 上饶市| 本溪市| 澎湖县| 凭祥市| 浑源县| 当雄县|