spring2.5注释配置SqlMapClientDaoSupport

实现一基础dao类:
public class BaseDao extends SqlMapClientDaoSupport { 
	@Autowired//为了注入SqlMapClient所以多了一个baseDao
	public void setSqlMapClientBase(SqlMapClient sqlMapClient) {
		super.setSqlMapClient(sqlMapClient);
	}
	
	
}

需要注入SqlMapClientDaoSupport的DAO类extends BaseDao即可
@Service
public class UserDAOImpl extends BaseDao implements UserDAO {..}


applicationContext.xml中简单的配置:
<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="org.gjt.mm.mysql.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/test" />
		<property name="username" value="root" />
		<property name="password" value="root" />
	</bean>
	<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="configLocation">
			<value>ibatis/SqlMapConfig.xml
			</value>
		</property>
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
	</bean> 
	<!-- 使用annotation 自动注册bean,并检查@Required,@Autowired的属性已被注入 -->
	<context:component-scan base-package="dao"/>


单元测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/applicationContext.xml"})
public class UserDAOTest {
	@Autowired
	private UserDAO userDAO;
	/**
	 * {@link dao.UserDAO#selectByExample(model.User)} 的测试方法。
	 */
	@Test
	public final void testSelectByExample() {
		List<User> l=userDAO.selectByExample(new User());
		System.out.println(l.get(0).getEmail());
		assertEquals(1,l.size());
	}


见网上众多兄弟再找,特此留下,飘过~

你可能感兴趣的:(java,DAO,xml,ibatis,单元测试)