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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值