ssh整合(完整)

29 篇文章 0 订阅
4 篇文章 0 订阅

ssh整合

1. hibernate po (domain/bean)

User:
package com.hcx.domain;

public class User {
	/*
	 * create table t_user(
		  id int primary key auto_increment,
		  username varchar(50),
		  password varchar(32),
		  age int 
		);
	 */
	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.hbm.xml:
<?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.hcx.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>

2.dao层:

       需要HibernateTemplate 相当于session操作PO类 --> 必须提供setter方法,让spring注入,xml配置模板

              save()  update delete  saveOrUpdate   find

       继承HibernateDaoSupport,需要注入SessionFactory,底层自动创建模板

              dao方法中 this.getHibernateTemplate()

UserDao:

 

package com.hcx.dao;

import com.hcx.domain.User;

public interface UserDao {
	
	/**
	 * 保存
	 * @param user
	 */
	public void save(User user);

}

 

 

 

UserDaoImpl:

 

package com.hcx.dao.impl;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.hcx.dao.UserDao;
import com.hcx.domain.User;

// 底层需要SessionFactory,自动创建HibernateTemplate模板
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

	@Override
	public void save(User user) {
		this.getHibernateTemplate().save(user);
	}

}

 

 

 

3.service 层,spring配置

       配置properties<context:property-placeholder location>

       配置数据源:ComboPooledDataSource

       配置SessionFactory:LocalSessionFactoryBean

       配置事务管理

UserService:

 

package com.hcx.service;

import com.hcx.domain.User;

public interface UserService {
	
	/**
	 * 注册
	 * @param user
	 */
	public void register(User user);

}

 

 

 

UserServiceImpl:

 

package com.hcx.service.impl;

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

public class UserServiceImpl implements UserService {

	private UserDao userDao;
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	@Override
	public void register(User user) {
		userDao.save(user);
	}

}

 

 


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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       					   http://www.springframework.org/schema/beans/spring-beans.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
       					   http://www.springframework.org/schema/context 
       					   http://www.springframework.org/schema/context/spring-context.xsd">
	
	<!-- 1.1加载properties文件 -->
	<!-- 1.2 配置数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql:///ee19_spring_day03"></property>
		<property name="user" value="root"></property>
		<property name="password" value="1234"></property>
	</bean>
	
	<!-- 1.3配置 LocalSessionFactoryBean,获得SessionFactory 
		* configLocation确定配置文件位置
			<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
		1)dataSource 数据源
		2)hibernateProperties hibernate其他配置项
		3) 导入映射文件
			mappingLocations ,确定映射文件位置,需要“classpath:” ,支持通配符 【】
				<property name="mappingLocations" value="classpath:com/hcx/domain/User.hbm.xml"></property>
				<property name="mappingLocations" value="classpath:com/hcx/domain/*.hbm.xml"></property>
			mappingResources ,加载执行映射文件,从src下开始 。不支持通配符*
				<property name="mappingResources" value="com/hcx/domain/User.hbm.xml"></property>
			mappingDirectoryLocations ,加载指定目录下的,所有配置文件
				<property name="mappingDirectoryLocations" value="classpath:com/hcx/domain/"></property>
			mappingJarLocations , 从jar包中获得映射文件
	-->
	<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>
				<prop key="hibernate.current_session_context_class">thread</prop>
			</props>
		</property>
		<property name="mappingLocations" value="classpath:com/hcx/domain/*.hbm.xml"></property>
	</bean>

	
	<!-- 3 dao -->
	<bean id="userDao" class="com.hcx.dao.impl.UserDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<!-- 4 service -->
	<bean id="userService" class="com.hcx.service.impl.UserServiceImpl">
		<property name="userDao" ref="userDao"></property>
	</bean>
	
	<!-- 5 事务管理 -->
	<!-- 5.1 事务管理器 :HibernateTransactionManager -->
	<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 5.2 事务详情 ,给ABC进行具体事务设置 -->
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="register"/>
		</tx:attributes>
	</tx:advice>
	<!-- 5.3 AOP编程,ABCD 筛选 ABC  -->
	<aop:config>
		<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.hcx.service..*.*(..))"/>
	</aop:config>
	

</beans>

 

 

 

4.web层,aciton

       struts.xml  <action name="" class="全限定类名"> 前面看到一位struts,底层使用spring

       默认与spring整合之后,按照【名称】自动注入

sturts.xml:

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<!-- 开发模式 -->
    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default">
    	<!-- 底层自动从spring容器中通过名称获得内容, getBean("userAction") -->
    	<action name="userAction_*" class="com.hcx.web.action.UserAction" method="{1}">
    		<result name="success">/messag.jsp</result>
    	</action>
    </package>
</struts>

 

UserAction:

package com.hcx.web.action;

import com.hcx.domain.User;
import com.hcx.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport implements ModelDriven<User> {

	//1 封装数据
	private User user = new User();

	@Override
	public User getModel() {
		return user;
	}
	
	//2 service
	private UserService userService;
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	//
	
	/**
	 * 注册
	 * @return
	 */
	public String register(){
		userService.register(user);
		return "success";
	}
	

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值