`
luxhua
  • 浏览: 86197 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Spring与Hibernate的集成01

阅读更多
1.资源端的构建
--创建数据库
create database spring;

--使用数据库
use spring;

drop table account;

--创建表
create table account(
accountid int(4) primary key auto_increment,
username varchar(20),
password varchar(20)
);

2.映射文件与POJO
利用myeclipse工具来生成相应的pojo与映射文件

3.定义DAO接口IAccount
public interface IAccountDao {
	//注册账户
	public void registerAccount(String username, String password, String desc);
	//根据名字查找
	public List queryAccountbyusername(String username);

}


提供DAO的实现类,2种方法:
A.基于HibernateTemplate辅助类,
使用HibernateTemplate 的好处:
(1)确保当前Hibernate的 Session 对象的正确打开和关闭,并直接参与到事务管理中去。
(2)Template实例是线程安全的,
(3)对于那些简单的诸如find、load、saveOrUpdate或者delete操作的直接调用。

a)使用Spring提供的一个简便的 HibernateDaoSupport基类,数据访问对象可以继承它取得HibernateTemplate.
public class AccountDaoImpl extends HibernateDaoSupport implements IAccountDao {

			public void registerAccount(String username, String password, String desc) {
				
				if(isEmpty(username) && isEmpty(password)) {
					System.out.println("请输入用户名以及密码!");
					return;
				}
				
				Account account = new Account();
				account.setUsername(username);
				account.setPassword(password);
				account.setDescription(desc);
				
				StringBuffer sizehql = new StringBuffer("select count(*) from Account a");
				
				List list = this.getHibernateTemplate().find(sizehql.toString());
				
				account.setAccountid((Long)list.get(0) + 1);
				
				this.getHibernateTemplate().save(account);

			}
			
			public List queryAccountbyusername(String username) {
				StringBuffer hql = new StringBuffer("from Account a where a.username='" + username + "'");
				
				return this.getHibernateTemplate().find(hql.toString());
			}
			
			private boolean isEmpty (String str) {
				return (str == null) || str.trim().equals("");
			}

		}


4.配置
<?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"
		xsi:schemaLocation="
		http://www.springframework.org/schema/beans	http://www.springframework.org/schema/beans/spring-beans-

2.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
		>
		<!-- configure the datasource -->
		<bean id="datasource"
	<!--配置数据源-->			class="org.springframework.jdbc.datasource.DriverManagerDataSource">
				<property name="driverClassName">
					<value>com.mysql.jdbc.Driver</value>
				</property>
				<property name="url">
					<value>jdbc:mysql://localhost:3306/spring</value>
				</property>
				<property name="username">
					<value>root</value>
				</property>
				<property name="password">
					<value>root</value>
				</property>
		</bean>
		
		<bean id="sessionfactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
			<property name="dataSource">
				<ref local="datasource"/>
			</property>
			<!-- 映射的资源 -->
			<property name="mappingResources">
				<list>
					<value>integration/hibernate/persistence/Account.hbm.xml</value>
				</list>
			</property>
			<property name="hibernateProperties">
				<props>
					<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				</props>
			</property>
		</bean>
		<!-- dao 配置 -->
		<bean id="accountdao" class="bo.dao.hibernate.AccountDaoImpl">
			<property name="sessionFactory">
				<ref local="sessionfactory"/>
			</property>
		</bean>
</beans>


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics