Spring-hibernate使用全xml配置结合方式搭建开发

 Spring-hibernate使用全xml配置结合方式搭建开发
1.新建mysql数据库表:
DROP TABLE IF EXISTS `person`;
CREATE TABLE `person` (
  `pid` int(11) NOT NULL,
  `name` varchar(20) DEFAULT NULL,
  `description` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`pid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2.新建web工程导入jar包。jdbc.properties 在MATE-INF下:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/ssh
jdbc.username=root
jdbc.password=tiger
3.applicationContext.xml配置数据源,ioc和aop,也可以在这里引用 hibernate.cfg.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">
		<!-- 数据源 -->
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>classpath:jdbc.properties</value>
		</property>
	</bean>
	<bean id="dataSource" destroy-method="close"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>
	
	<!-- sessionFactory配置 -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSource"/>
		<!-- ORM映射文件 -->
		<property name="mappingResources">
			<list>
				<value>com/zl/test2/entity/User.hbm.xml</value>
			</list>
		</property>
		<!-- 其他配置 -->
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hbm2ddl.auto">update</prop>
			</props>
		</property>
	</bean>
	<!--  也可以使用这种方式配置,多了一个xml
	<bean id="sessionFactory2"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation">
			<value>classpath:hibernate.cfg.xml</value>
		</property>
	</bean>-->
	
	<!-- DAO数据处理层 -->
	<bean id="userDao2" class="com.zl.test2.dao.impl.UserDaoImplForHibernate">
		<property name="sessionFactory">
			<ref bean="sessionFactory"/>
		</property>
	</bean>

	
	<!-- =====================================1织入 -->
	<aop:config>
		<!-- 定义切入点 expression="切入表达式" -->
		<aop:pointcut expression="execution(* com.zl.test2.dao.*.*(..))" id="txCutPoint"/>
		<!-- 织入:将通知切入到指定的切入点 -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txCutPoint"/>
	</aop:config>
	
	<!-- 通知【细化到方法】 ====================2-->
	<tx:advice id="txAdvice" transaction-manager="transcationManager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRED"/>
			<tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
			<tx:method name="get*" read-only="true" propagation="SUPPORTS"/>
			<tx:method name="delete*" propagation="REQUIRED"/>
			<tx:method name="update*" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice>
	
	<!-- 事务管理器  =============1-->
	<bean id="transcationManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref bean="sessionFactory"/>
		</property>
	</bean>
	
	
</beans>			
4.entity:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
	<class name="com.zl.test2.entity.User">
		<id column="id" name="id">
			<generator class="identity"></generator>
		</id>
		<property name="name" type="string"></property>
		<property name="age" type="int"></property>
		<property name="phone" type="string"></property>
	</class>
</hibernate-mapping>
package com.zl.test2.entity;

/**
 * 用户类
 * 
 * @author wenber
 * 
 */
public class User {

	private int id;

	private String name;

	private int age;

	private String phone;

	public User() {
	}

	public User(String name, int age, String phone) {
		super();
		this.name = name;
		this.age = age;
		this.phone = phone;
	}

	public User(int id, String name, int age, String phone) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.phone = phone;
	}

	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 int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

}
5.dao层:
package com.zl.test2.dao.impl;

import java.util.List;

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

import com.zl.test2.dao.IUserDao;
import com.zl.test2.entity.User;

public class UserDaoImplForHibernate extends HibernateDaoSupport implements
		IUserDao {

	@Override
	public List<User> findUserList() {
		return this.getHibernateTemplate().find("from User");
	}

	@Override
	public void saveUser(User user) {

		this.getHibernateTemplate().save(user);
		// this.getHibernateTemplate().save(user);
	}

}
6.调用:
package com.zl.test2.view;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zl.test2.dao.IUserDao;
import com.zl.test2.entity.User;

public class PersonServiceTest {
	public static void main(String[] args) {
		testSavePerson();
	}

	public static void testSavePerson() {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"com/zl/test2/applicationContext.xml");
		IUserDao userDao = (IUserDao) context.getBean("userDao2");
		User u = new User(12, "zl", 1, "ee");
		userDao.saveUser(u);
	}
}

7:源代码下载: 点击打开链接

http://download.csdn.net/detail/qq_31968809/9772706




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值