package cn.edu.hpu.mybatis.dao; import cn.edu.hpu.mybatis.PO.User; //用户管理的Dao接口 public interface UserDao { //根据Id查询用户信息 public User findUserById(int id) throws Exception; //添加用户信息 public void insertUser(User user) throws Exception; //删除用户信息 public void deleteUser(int id) throws Exception; //修改用户信息 public void updateUser(User user) throws Exception; }
package cn.edu.hpu.mybatis.dao; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import cn.edu.hpu.mybatis.PO.User; public class UserDaoImpl implements UserDao{ //需要向dao实现类中注入SqlSessionFactory工厂 //这里我们暂时没用spring,我们通过构造方法注入 private SqlSessionFactory sqlSessionFactory; public UserDaoImpl(SqlSessionFactory sqlSessionFactory){ this.sqlSessionFactory=sqlSessionFactory; } @Override public void deleteUser(int id) throws Exception { SqlSession sqlSession=sqlSessionFactory.openSession(); //传入id删除用户 sqlSession.delete("test.deleteUser",id); //提交事务 sqlSession.commit(); sqlSession.close(); } @Override public User findUserById(int id) throws Exception { SqlSession sqlSession=sqlSessionFactory.openSession(); User user=sqlSession.selectOne("test.findUserById",id); //释放资源 sqlSession.close(); return user; } @Override public void insertUser(User user) throws Exception { SqlSession sqlSession=sqlSessionFactory.openSession(); sqlSession.insert("test.insertUser",user); //提交事务 sqlSession.commit(); //释放资源 sqlSession.close(); } @Override public void updateUser(User user) throws Exception { SqlSession sqlSession=sqlSessionFactory.openSession(); sqlSession.update("test.updateUser",user); //提交事务 sqlSession.commit(); sqlSession.close(); } }
package cn.edu.hpu.mybatis.test; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Before; import org.junit.Test; import cn.edu.hpu.mybatis.PO.User; import cn.edu.hpu.mybatis.dao.UserDao; import cn.edu.hpu.mybatis.dao.UserDaoImpl; public class UserDaoImplTest { private SqlSessionFactory sqlSessionFactory; //注解Before是在执行本类所有测试方法之前先调用这个方法 @Before public void setup() throws Exception{ //创建SqlSessionFactory String resource="SqlMapConfig.xml"; //将配置文件加载成流 InputStream inputStream = Resources.getResourceAsStream(resource); //创建会话工厂,传入mybatis配置文件的信息 sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); } @Test public void testFindUserById() throws Exception{ //创建Dao对象 UserDao userDao=new UserDaoImpl(sqlSessionFactory); //调用UserDao的方法 User user=userDao.findUserById(1); System.out.println(user.getUsername()); } }
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter. DEBUG [main] - PooledDataSource forcefully closed/removed all connections. DEBUG [main] - PooledDataSource forcefully closed/removed all connections. DEBUG [main] - PooledDataSource forcefully closed/removed all connections. DEBUG [main] - PooledDataSource forcefully closed/removed all connections. DEBUG [main] - Opening JDBC Connection DEBUG [main] - Created connection 28970806. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@1ba0f36] DEBUG [main] - ==> Preparing: SELECT * FROM USER WHERE id=? DEBUG [main] - ==> Parameters: 1(Integer) DEBUG [main] - <== Total: 1 DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.Connection@1ba0f36] DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.Connection@1ba0f36] DEBUG [main] - Returned connection 28970806 to pool.
下一篇总结我们来使用mapper代理方法来写Dao,来解决上面我们发现的问题。
转载请注明出处:http://blog.csdn.net/acmman/article/details/46455507