<!-- 数据库的相关配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="${dbcp.maxActive}" />
<property name="maxIdle" value="${dbcp.maxIdle}" />
<property name="defaultAutoCommit" value="false" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:org/wangzai/webapp/mybatis/mybatis-commons.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
<mappers>
<mapper resource="org/wangzai/webapp/mybatis/mappers/UserInfoMapper.xml"/>
</mappers>
</configuration>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<!--namespace的作用是为这张表取一个名字,好在持久化层中进行访问 -->
<mapper namespace="org.wangzai.webapp.module.test.dao">
<!-- 插入一行数据 -->
<insert id="doAdd" parameterType="org.wangzai.webapp.module.test.domain.UserInfo" keyProperty="id">
INSERT INTO userInfo(name,age,sdesc) values(#{name},#{age},#{sdesc})
</insert>
<!-- 删除一条数据 -->
<delete id="delete" parameterType="int">
DELETE FROM userInfo WHERE id =#{id}
</delete>
<!-- 更新一条数据 -->
<update id="update" parameterType="org.wangzai.webapp.module.test.domain.UserInfo">
UPDATE userInfo SET name=#{name},age=#{age},
sdesc=#{sdesc}
WHERE id=#{id};
</update>
<!-- 查询该表的总行数 -->
<select id="getCount" resultType="int">
SELECT COUNT(*)FROM userInfo WHERE id='0'
</select>
<!-- 查询一条数据 -->
<select id="getById" parameterType="int" resultType="org.wangzai.webapp.module.test.domain.UserInfo">
SELECT id,name,age,desc
FROM userInfo WHERE id=#{id}
</select>
<!-- 查询一组数据 -->
<select id="findAll" resultType="org.wangzai.webapp.module.test.domain.UserInfo" >
SELECT * FROM userInfo
</select>
</mapper>
public class UserInfoDaoImpl extends SqlSessionDaoSupport implements UserInfoDao {
public void doAdd(UserInfo userInfo) {
this.getSqlSession().insert("org.wangzai.webapp.module.test.dao.doAdd", userInfo);
}
public void doDelete(int id) {
this.getSqlSession().delete("org.wangzai.webapp.module.test.dao.delete", id);
}
public void doEdit(UserInfo userInfo) {
this.getSqlSession().update("org.wangzai.webapp.module.test.dao.update", userInfo);
}
public List<UserInfo> findAll() {
return this.getSqlSession().selectList("org.wangzai.webapp.module.test.dao.findAll");
}
}
@Test
public void testDoAdd(){
ApplicationContext context = new ClassPathXmlApplicationContext("org/wangzai/webapp/commons/application-commons.xml");
UserInfoDao userInfoDao = (UserInfoDao) context.getBean("userInfoDao");
UserInfo userInfo = new UserInfo(1, "wangzai", 1, "soga");
userInfoDao.doAdd(userInfo);
System.out.println("Good work");
}