传统开发模式DAO
目录
- 定义接口 CustomerDao.java
- 实现接口 CustomerDaoImpl.java
- 测试类
在传统开发模式DAO下,我们自己先定义好接口,然后再去定义实现类,在实现类中实现接口的操作。到时候只需要创建一个 dao 对象,即可调用其中的方法。
定义接口 CustomerDao.java
在 dao 包下,创建一个接口文件 CustomerDao.java,在其中写上数据库的各种操作的抽象方法。
public interface CustomerDao {public Customer getCustomerWithId(Integer id); // 根据id查询public List<Customer> getAllCustomer(); // 查询所有public void addCustomer(Customer customer); // 添加用户public void updateCustomer(Customer customer); // 更新用户信息public void delete(Customer customer); // 删除用户
}
实现接口 CustomerDaoImpl.java
在 dao 包下定义接口的实现类,CustomerDaoImpl.java,在这里实现覆盖接口中的抽象方法。
小技巧:将鼠标指针放在接口名称处,按 alt + enter ,选择 Implement interface 可以快速创建接口的实现类。
实现类中将抽象方法覆盖为具体的操作:利用我们之前封装好的 MyBatis 工具类,可以很方便的写出代码。
public class CustomerDaoImpl implements CustomerDao {@Overridepublic Customer getCustomerWithId(Integer id) {SqlSession sqlSession = MybatisUtils.openSession();Customer customer = sqlSession.selectOne("queryCustomerById", id);sqlSession.close();return customer;}@Overridepublic List<Customer> getAllCustomer() {SqlSession sqlSession = MybatisUtils.openSession();List<Customer> allCustomer = sqlSession.selectList("queryAllCustomer");sqlSession.close();return allCustomer;}@Overridepublic void addCustomer(Customer customer) {SqlSession sqlSession = MybatisUtils.openSession();sqlSession.insert("insertCustomer", customer);sqlSession.close();}@Overridepublic void updateCustomer(Customer customer) {SqlSession sqlSession = MybatisUtils.openSession();sqlSession.update("updateCustomer", customer);sqlSession.close();}@Overridepublic void delete(Customer customer) {SqlSession sqlSession = MybatisUtils.openSession();sqlSession.delete("deleteCustomer", customer);sqlSession.close();}
}
测试类
在 Test 包中新建一个测试类 MyTest2.java,利用单元测试,测试一下以上的DAO是否成功。
public class MyTest2 {@Testpublic void test(){CustomerDao customerDao = new CustomerDaoImpl();Customer customerWithId = customerDao.getCustomerWithId(1); // 查询id为1的用户System.out.println(customerWithId);System.out.println("-----------------------------------------------");List<Customer> allCustomer = customerDao.getAllCustomer(); // 查询所有用户for (Customer customer : allCustomer) {System.out.println(customer);}}}
运行效果:成功查询出id为1的对象,以及所有对象。
Opening JDBC Connection
Created connection 1227074340.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@4923ab24]
==> Preparing: SELECT * FROM `customer` WHERE cust_id = ?
==> Parameters: 1(Integer)
<== Columns: cust_id, cust_name, cust_profession, cust_phone, email
<== Row: 1, 鲁班, 射手, 13499887733, 12341241@qq.com
<== Total: 1
Customer{cust_id=1, cust_name='鲁班', cust_profession='射手', cust_phone='13499887733', email='12341241@qq.com'}
-----------------------------------------------
Opening JDBC Connection
Created connection 1122805102.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@42eca56e]
==> Preparing: SELECT * FROM `customer`
==> Parameters:
<== Columns: cust_id, cust_name, cust_profession, cust_phone, email
<== Row: 1, 鲁班, 射手, 13499887733, 12341241@qq.com
<== Row: 2, 李白, 刺客, 18977665521, libai@163.com
<== Row: 3, 阿轲, 刺客, 18977665997, aike@qq.com
<== Row: 4, 德玛西亚, 肉盾, 13700997665, demaxiya.126.com6
<== Row: 5, 亚索, 战士, 13586878987, yasuo@qq.com
<== Row: 6, 奶妈, 辅助, 13398909089, nama@qq.com
<== Row: 7, 剑圣, 刺客, 13398909088, jiansheng@163.com
<== Row: 8, 盖伦, 肉盾, 15923242231, gailun@126.com
<== Row: 9, 锤石, 辅助, 13398908900, 8888@163.com
<== Row: 11, 孙悟空, 战士, 13728964922, lixin@qq.com
<== Row: 20, 后裔6, null, 18937485936, null
<== Row: 21, 后裔6, null, 18937485936, null
<== Total: 12
Customer{cust_id=1, cust_name='鲁班', cust_profession='射手', cust_phone='13499887733', email='12341241@qq.com'}
Customer{cust_id=2, cust_name='李白', cust_profession='刺客', cust_phone='18977665521', email='libai@163.com'}
Customer{cust_id=3, cust_name='阿轲', cust_profession='刺客', cust_phone='18977665997', email='aike@qq.com'}
Customer{cust_id=4, cust_name='德玛西亚', cust_profession='肉盾', cust_phone='13700997665', email='demaxiya.126.com6'}
Customer{cust_id=5, cust_name='亚索', cust_profession='战士', cust_phone='13586878987', email='yasuo@qq.com'}
Customer{cust_id=6, cust_name='奶妈', cust_profession='辅助', cust_phone='13398909089', email='nama@qq.com'}
Customer{cust_id=7, cust_name='剑圣', cust_profession='刺客', cust_phone='13398909088', email='jiansheng@163.com'}
Customer{cust_id=8, cust_name='盖伦', cust_profession='肉盾', cust_phone='15923242231', email='gailun@126.com'}
Customer{cust_id=9, cust_name='锤石', cust_profession='辅助', cust_phone='13398908900', email='8888@163.com'}
Customer{cust_id=11, cust_name='孙悟空', cust_profession='战士', cust_phone='13728964922', email='lixin@qq.com'}
Customer{cust_id=20, cust_name='后裔6', cust_profession='null', cust_phone='18937485936', email='null'}
Customer{cust_id=21, cust_name='后裔6', cust_profession='null', cust_phone='18937485936', email='null'}