mybatis 入门优化
在上一节《mybatis快速入门》的基础上对IWebUserDao的实现类进行了优化,暂时命名为WebUserDaoMybatis2.java
package com.mozi.dao.impl; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import com.mozi.dao.IWebUserDao; import com.mozi.entity.WebUser; public class WebUserDaoMybatis2 implements IWebUserDao { private SqlSessionFactory sqlSessionFactory; // 需要向dao实现类中注入SqlSessionFactory // 通过构造方法注入 public WebUserDaoMybatis2(SqlSessionFactory sqlSessionFactory) { this.sqlSessionFactory = sqlSessionFactory; } @Override public List<WebUser> findUserByName(String username) { SqlSession sqlSession = sqlSessionFactory.openSession(); List<WebUser> list = sqlSession .selectList("src.main.resource.userMapper.findUserByName", username); // 提交事务 sqlSession.commit(); // 释放资源 sqlSession.close(); return list; } @Override public void insertUser(WebUser user) { SqlSession sqlSession = sqlSessionFactory.openSession(); // 执行插入操作 sqlSession.insert("src.main.resource.userMapper.insertUser", user); // 提交事务 sqlSession.commit(); // 释放资源 sqlSession.close(); } @Override public void deleteUser(String id) { SqlSession sqlSession = sqlSessionFactory.openSession(); // 执行插入操作 sqlSession.delete("src.main.resource.userMapper.deleteUser", id); // 提交事务 sqlSession.commit(); // 释放资源 sqlSession.close(); } @Override public void updateUser(WebUser user) { SqlSession sqlSession = sqlSessionFactory.openSession(); // 执行插入操作 sqlSession.update("src.main.resource.userMapper.updateUser", user); // 提交事务 sqlSession.commit(); // 释放资源 sqlSession.close(); } }
添加测试类 WebUserDaoImplTest.java
package com.mozi.test.java; import static org.junit.Assert.*; import java.io.IOException; import java.io.InputStream; import java.util.List; 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 com.mozi.dao.IWebUserDao; import com.mozi.dao.impl.WebUserDaoJdbc;import com.mozi.dao.impl.WebUserDaoMybatis2;import com.mozi.entity.WebUser; public class WebUserDaoImplTest { private static SqlSessionFactory sqlSessionFactory = null; private static IWebUserDao dao = null; static{ String resource = "SqlMapConfig.xml"; InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream(resource); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); dao = new WebUserDaoMybatis2(sqlSessionFactory); } @Test public void findUserByName() { List<WebUser> userList = dao.findUserByName("aa"); System.out.println(userList); assertNotNull(userList); } @Test public void insertUser() { WebUser user = new WebUser(); user.setName("5"); user.setTrueName("lisi"); user.setPass("5"); dao.insertUser(user); } @Test public void deleteUser() { dao.deleteUser("5"); } @Test public void updateUser() { WebUser user = new WebUser(); user.setName("5"); user.setTrueName("lisi"); user.setPass("666"); dao.updateUser(user); } }