非注解,持久层为ibatis,如果为hibernate少三种
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/new"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="com/mydomain/data/SqlMapConfig.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 第一种将sqlMapClient直接注入到dao实现类中,并继承SqlMapClientDaoSupport dao实现类继承自SqlMapClientDaoSupport-->
<!-- <bean id="accountDao" class="example.SqlMapAccountDao">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean> -->
<!-- 第二种将sqlMapClient直接注入到sqlMapClientDaoSupport实现类中将sqlMapClientDaoSupport的bean定义为抽象,dao的bean利用parent继承抽象bean sqlMapClientDaoSupport,dao类继承SqlMapClientDaoSupport dao实现类继承自SqlMapClientDaoSupport-->
<!-- <bean id="sqlMapClientDaoSupport" class="org.springframework.orm.ibatis.support.SqlMapClientDaoSupport" abstract="true">
<property name="sqlMapClient" ref="sqlMapClient" />
</bean>
<bean id="accountDao" class="example.SqlMapAccountDao" parent="sqlMapClientDaoSupport">
</bean> -->
<!-- 第三种定义一个抽象bean,有一个属性为工厂类,即sqlMapClient,在使dao bean继承abstractDao bean,dao实现类继承自SqlMapClientDaoSupport-->
<!-- <bean id="abstractDao" abstract="true" >
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
<bean id="accountDao" class="example.SqlMapAccountDao" parent="abstractDao">
</bean> -->
<!-- 第四种将sqlMapClient直接注入到dao实现类中,并继承SqlMapClientTemplate bean继承自SqlMapClientTemplate-->
<!-- <bean id="accountDao" class="example.SqlMapAccountTemplateDao">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean> -->
<!-- 第五种将sqlMapClient直接注入到sqlMapClientTemplate,并继承SqlMapClientTemplate bean继承自SqlMapClientTemplate,同第二种-->
<!-- <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate" abstract="true">
<property name="sqlMapClient" ref="sqlMapClient" />
</bean>
<bean id="accountDao" class="example.SqlMapAccountTemplateDao" parent="sqlMapClientTemplate">
</bean> -->
<!-- 第六种定义一个抽象bean,有一个属性为工厂类,即sqlMapClient,在使dao bean继承abstractDao bean,dao实现类继承自SqlMapClientTemplate-->
<!-- <bean id="abstractDao" abstract="true" >
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
<bean id="accountDao" class="example.SqlMapAccountTemplateDao" parent="abstractDao">
</bean> -->
<!-- 第七种手动在dao实现类中定义一个SqlMapClientTemplate的属性,提供set方法,并对其进行注入sqlMapClientTemplate,并将sqlMapClient注入进SqlMapClientTemplate类中 -->
<!-- <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate" >
<property name="sqlMapClient" ref="sqlMapClient" />
</bean>
<bean id="accountDao" class="example.SqlMapAccountBaseDao" >
<property name="sqlMapClientTemplate" ref="sqlMapClientTemplate"/>
</bean> -->
<!-- 它没有类似于第二种的方式,因为它没有继承任何类 -->
<!-- 第八种类似于第七种加第三种的的方式 -->
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate" >
<property name="sqlMapClient" ref="sqlMapClient" />
</bean>
<bean id="abstractDao" abstract="true" >
<property name="sqlMapClientTemplate" ref="sqlMapClientTemplate"/>
</bean>
<bean id="accountDao" class="example.SqlMapAccountBaseDao" parent="abstractDao">
</bean>
<!-- 第九种是手动定义SqlMapClientDaoSupport为dao实现类的属性,并将sqlMapClient注入SqlMapClientDaoSupport中,配置和代码省略 -->
<!-- 第十种是第九种变成抽象的,配置和代码省略 -->
</beans>
接口AccountDao
public interface AccountDao {
//查询所有
public List selectAllAccounts () throws DataAccessException;
public Account getAccount(int id) throws DataAccessException;
public void insertAccount(Account account) throws DataAccessException;
}
继承自SqlMapClientDaoSupport类的SqlMapAccountDao实现类
public class SqlMapAccountDao extends SqlMapClientDaoSupport implements AccountDao{
@Override
public List<Account> selectAllAccounts() throws DataAccessException {
List<Account> list = getSqlMapClientTemplate().queryForList("Account.selectAllAccounts");
System.out.println("这里用的是SqlMapAccountDao");
return list;
}
@Override
public Account getAccount(int id) throws DataAccessException {
return (Account) getSqlMapClientTemplate().queryForObject("Account.selectAccountById", id);
}
@Override
public void insertAccount(Account account) throws DataAccessException {
getSqlMapClientTemplate().update("Account.insertAccount", account);
}
}
继承自SqlMapClientTemplate类的SqlMapAccountTemplateDao实现类
public class SqlMapAccountTemplateDao extends SqlMapClientTemplate implements AccountDao{
@Override
public List<Account> selectAllAccounts() throws DataAccessException {
List<Account> list = queryForList("Account.selectAllAccounts");
System.out.println("这里用的是SqlMapAccountTemplateDao");
return list;
}
@Override
public Account getAccount(int id) throws DataAccessException {
return (Account) queryForObject("Account.selectAccountById", id);
}
@Override
public void insertAccount(Account account) throws DataAccessException {
update("Account.insertAccount", account);
}
}
定义了SqlMapClientTemplate的属性类SqlMapAccountBaseDao:
public class SqlMapAccountBaseDao implements AccountDao{
private SqlMapClientTemplate sqlMapClientTemplate;
/**
* @return the sqlMapClientTemplate
*/
public SqlMapClientTemplate getSqlMapClientTemplate() {
return sqlMapClientTemplate;
}
/**
* @param sqlMapClientTemplate the sqlMapClientTemplate to set
*/
public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {
this.sqlMapClientTemplate = sqlMapClientTemplate;
}
@Override
public List<Account> selectAllAccounts() throws DataAccessException {
List<Account> list = sqlMapClientTemplate.queryForList("Account.selectAllAccounts");
System.out.println("这里用的是SqlMapAccountBaseDao");
return list;
}
@Override
public Account getAccount(int id) throws DataAccessException {
return (Account) sqlMapClientTemplate.queryForObject("Account.selectAccountById", id);
}
@Override
public void insertAccount(Account account) throws DataAccessException {
sqlMapClientTemplate.update("Account.insertAccount", account);
}
}
还差一个类,代码和SqlMapAccountBaseDao类似,只不过属性为SqlMapClientDaoSupport而已.
注意:4,5,6方法只能用在ibatis方法上,因为SqlMapClientTemplate类里面定义了工厂类,所以可以使用,而hibernate的模板里没有定义工厂类,因此不可以使用.
在此,我更推荐第三种方式,这样看着比较舒服,而且用着也方便,也省去了很多麻烦.