視頻觀看地址:http://edu.51cto.com/course/14674.htmlhtml
package cn.org.kingdom.dao; import java.util.List; import cn.org.kingdom.pojo.User; public interface UserDAO { public int insertUser(User vo) throws Exception; public int updateUser(User vo) throws Exception ; public int deleteUser(User vo) throws Exception ; public User selectUserById(int userid) throws Exception ; public List<User> selectAll() throws Exception; public int getAllCounts() throws Exception ; }
package cn.org.kingdom.dao.impl; import java.util.List; import org.apache.ibatis.session.SqlSession; import cn.org.kingdom.dao.UserDAO; import cn.org.kingdom.pojo.User; public class UserDAOImpl implements UserDAO { private SqlSession sqlSession ; public UserDAOImpl(SqlSession sqlSession) { this.sqlSession = sqlSession; } public SqlSession getSqlSession() { return sqlSession; } public void setSqlSession(SqlSession sqlSession) { this.sqlSession = sqlSession; } @Override public int insertUser(User vo) throws Exception { return 0; } @Override public int updateUser(User vo) throws Exception { return 0; } @Override public int deleteUser(User vo) throws Exception { return 0; } @Override public User selectUserById(int userid) throws Exception { return null; } @Override public List<User> selectAll() throws Exception { // TODO Auto-generated method stub return null; } @Override public int getAllCounts() throws Exception { // TODO Auto-generated method stub return 0; } }
編寫添加的方法java
@Override public int insertUser(User vo) throws Exception { return sqlSession.insert(User.class.getName()+".insertUser",vo); }
mapper.xml中配置sql
<insert id="insertUser" parameterType="cn.org.kingdom.pojo.User"> insert into tb_user(userid,user_name,age,pwd,sex,birthday) values(seq_user.nextval,#{userName},#{age},#{pwd},#{sex},#{birthday}) </insert>
編寫測試類apache
package cn.org.kingdom.test; import static org.junit.Assert.*; import java.io.InputStream; import java.util.Date; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test; import cn.org.kingdom.dao.UserDAO; import cn.org.kingdom.dao.impl.UserDAOImpl; import cn.org.kingdom.pojo.User; public class MyBatisTest01 { SqlSessionFactory sqlSessionFactory = null ; SqlSession sqlSession = null ; UserDAO dao = null ; @Before public void setUp() throws Exception { //加載資源 String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); sqlSession = sqlSessionFactory.openSession(); dao = new UserDAOImpl(sqlSession); } @After public void tearDown() throws Exception { //關閉 sqlSession.close(); } @Test public void testInsertUser() { User vo = new User("阿珂", "123456", 18, "女", new Date()); try { dao.insertUser(vo); //提交事務 sqlSession.commit(); } catch (Exception e) { e.printStackTrace(); sqlSession.rollback(); } } }
實現類方法實現session
@Override public int updateUser(User vo) throws Exception { return sqlSession.update(User.class.getName()+".updateUser",vo); }
mapper.xml文件mybatis
<update id="updateUser"> update tb_user set user_name=#{userName},age=#{age},pwd=#{pwd},sex=#{sex},birthday=#{birthday} where userid=#{userid} </update>
測試方法app
@Test public void testUpdateUser() { User vo = new User(7,"冰封戰神", "123456", 18, "男", new Date()); try { dao.updateUser(vo); //提交事務 sqlSession.commit(); } catch (Exception e) { e.printStackTrace(); sqlSession.rollback(); } }
實現類方法dom
@Override public int deleteUser(int userid) throws Exception { return sqlSession.delete(User.class.getName()+".deleteUser",userid); }
mapper.xmlide
<update id="deleteUser"> delete from tb_user where userid=#{userid} </update>
測試方法測試
@Test public void testDeleteUserById() { int sid = 7 ; try { dao.deleteUser(sid); //提交事務 sqlSession.commit(); } catch (Exception e) { e.printStackTrace(); sqlSession.rollback(); } }
實現類
@Override public List<User> selectAll() throws Exception { return sqlSession.selectList(User.class.getName()+".selectAll"); }
mapper.xml文件
<select id = "selectAll" resultType="cn.org.kingdom.pojo.User"> select userid,user_name as userName,age,pwd,sex,birthday from tb_user </select>
測試代碼
@Test public void testSelectAll() throws Exception { List<User> list = dao.selectAll(); for (User user : list) { System.out.println(user); } }
實現類
@Override public int getAllCounts() throws Exception { return sqlSession.selectOne(User.class.getName()+".getAllCounts"); }
mapper.xml
<select id = "getAllCounts" resultType="int"> select count(1) from tb_user </select>
測試類
@Test public void testGetCount() throws Exception{ int count = dao.getAllCounts() ; System.out.println(count); }