在MyBatis中,我们有两种Dao的写法,一种叫传统Dao写法,一种叫Mapper代理接口。下面看看如何实现。
1 传统Dao写法
1.1 编写CustomerDao接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 public interface CustomerDao { public List<Customer> findAll () ; public void save (Customer customer) ; public void update (Customer customer) ; public Customer findById (Integer id) ; public List<Customer> findByName (String name) ; public void delete (Integer id) ; }
1.2 编写CustomerDao实现类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 public class CustomerDaoImpl implements CustomerDao { @Override public List<Customer> findAll () { SqlSession sqlSession = null ; try { sqlSession = MyBatisUtils.getSession(); return sqlSession.selectList("com.yiidian.dao.CustomerDao.findAll" ); } catch (Exception e) { e.printStackTrace(); } finally { sqlSession.close(); } return null ; } @Override public void save (Customer customer) { SqlSession sqlSession = null ; try { sqlSession = MyBatisUtils.getSession(); sqlSession.insert("com.yiidian.dao.CustomerDao.save" , customer); sqlSession.commit(); } catch (Exception e) { e.printStackTrace(); sqlSession.rollback(); } finally { sqlSession.close(); } } @Override public void update (Customer customer) { SqlSession sqlSession = null ; try { sqlSession = MyBatisUtils.getSession(); sqlSession.update("com.yiidian.dao.CustomerDao.update" , customer); sqlSession.commit(); } catch (Exception e) { e.printStackTrace(); sqlSession.rollback(); } finally { sqlSession.close(); } } @Override public Customer findById (Integer id) { SqlSession sqlSession = null ; try { sqlSession = MyBatisUtils.getSession(); return sqlSession.selectOne("com.yiidian.dao.CustomerDao.findById" ,id); } catch (Exception e) { e.printStackTrace(); } finally { sqlSession.close(); } return null ; } @Override public List<Customer> findByName (String name) { SqlSession sqlSession = null ; try { sqlSession = MyBatisUtils.getSession(); return sqlSession.selectList("com.yiidian.dao.CustomerDao.findByName" ,name); } catch (Exception e) { e.printStackTrace(); } finally { sqlSession.close(); } return null ; } @Override public void delete (Integer id) { SqlSession sqlSession = null ; try { sqlSession = MyBatisUtils.getSession(); sqlSession.delete("com.yiidian.dao.CustomerDao.delete" , id); sqlSession.commit(); } catch (Exception e) { e.printStackTrace(); sqlSession.rollback(); } finally { sqlSession.close(); } } }
传统方式的重点在于Dao实现类,在Dao实现类中,手动调用SqlSession提供的方法直接执行映射文件的SQL语句。
1.3 编写CustomerDao.xml映射
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace ="com.yiidian.dao.CustomerDao" > <select id ="findAll" resultType ="com.yiidian.domain.Customer" > select * from t_customer </select > <insert id ="save" parameterType ="com.yiidian.domain.Customer" > INSERT INTO t_customer(NAME,gender,telephone) VALUES(#{name},#{gender},#{telephone}) </insert > <update id ="update" parameterType ="com.yiidian.domain.Customer" > UPDATE t_customer SET NAME = #{name}, gender = #{gender}, telephone = #{telephone} WHERE id = #{id} </update > <select id ="findById" parameterType ="integer" resultType ="com.yiidian.domain.Customer" > select * from t_customer where id = #{id} </select > <select id ="findByName" parameterType ="string" resultType ="com.yiidian.domain.Customer" > select * from t_customer where name like #{name} </select > <delete id ="delete" parameterType ="integer" > delete from t_customer where id = #{id} </delete > </mapper >
1.4 编写测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 public class TestCustomerDao { @Test public void testSave () { SqlSession session = MyBatisUtils.getSession(); CustomerDao customerDao = new CustomerDaoImpl(); Customer customer = new Customer(); customer.setName("小苍" ); customer.setGender("女" ); customer.setTelephone("15755556666" ); customerDao.save(customer); session.close(); } @Test public void testUpdate () { SqlSession session = MyBatisUtils.getSession(); CustomerDao customerDao = new CustomerDaoImpl(); Customer customer = new Customer(); customer.setId(5 ); customer.setName("小泽" ); customer.setGender("女" ); customer.setTelephone("15755556666" ); customerDao.update(customer); session.commit(); session.close(); } @Test public void testFindAll () { SqlSession session = MyBatisUtils.getSession(); CustomerDao customerDao = new CustomerDaoImpl(); List<Customer> list = customerDao.findAll(); for (Customer cust:list){ System.out.println(cust); } session.close(); } @Test public void testFindById () { SqlSession session = MyBatisUtils.getSession(); CustomerDao customerDao = new CustomerDaoImpl(); Customer customer = customerDao.findById(3 ); System.out.println(customer); session.close(); } @Test public void testFindByName () { SqlSession session = MyBatisUtils.getSession(); CustomerDao customerDao = new CustomerDaoImpl(); List<Customer> list = customerDao.findByName("%小%" ); for (Customer cust:list){ System.out.println(cust); } session.close(); } @Test public void testDelete () { SqlSession session = MyBatisUtils.getSession(); CustomerDao customerDao = new CustomerDaoImpl(); customerDao.delete(5 ); session.commit(); session.close(); } }
2 Mapper代理接口
2.1 编写CustomerDao接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 public interface CustomerDao { public List<Customer> findAll () ; public void save (Customer customer) ; public void update (Customer customer) ; public Customer findById (Integer id) ; public List<Customer> findByName (String name) ; public void delete (Integer id) ; }
2.2 编写CustomerDao.xml映射
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace ="com.yiidian.dao.CustomerDao" > <select id ="findAll" resultType ="com.yiidian.domain.Customer" > select * from t_customer </select > <insert id ="save" parameterType ="com.yiidian.domain.Customer" > INSERT INTO t_customer(NAME,gender,telephone) VALUES(#{name},#{gender},#{telephone}) </insert > <update id ="update" parameterType ="com.yiidian.domain.Customer" > UPDATE t_customer SET NAME = #{name}, gender = #{gender}, telephone = #{telephone} WHERE id = #{id} </update > <select id ="findById" parameterType ="integer" resultType ="com.yiidian.domain.Customer" > select * from t_customer where id = #{id} </select > <select id ="findByName" parameterType ="string" resultType ="com.yiidian.domain.Customer" > select * from t_customer where name like #{name} </select > <delete id ="delete" parameterType ="integer" > delete from t_customer where id = #{id} </delete > </mapper >
2.3 编写测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 public class TestCustomerDao2 { @Test public void testSave () { SqlSession session = MyBatisUtils.getSession(); CustomerDao customerDao = session.getMapper(CustomerDao.class); Customer customer = new Customer(); customer.setName("小苍" ); customer.setGender("女" ); customer.setTelephone("15755556666" ); customerDao.save(customer); session.close(); } @Test public void testUpdate () { SqlSession session = MyBatisUtils.getSession(); CustomerDao customerDao = session.getMapper(CustomerDao.class); Customer customer = new Customer(); customer.setId(5 ); customer.setName("小泽" ); customer.setGender("女" ); customer.setTelephone("15755556666" ); customerDao.update(customer); session.commit(); session.close(); } @Test public void testFindAll () { SqlSession session = MyBatisUtils.getSession(); CustomerDao customerDao = session.getMapper(CustomerDao.class); List<Customer> list = customerDao.findAll(); for (Customer cust:list){ System.out.println(cust); } session.close(); } @Test public void testFindById () { SqlSession session = MyBatisUtils.getSession(); CustomerDao customerDao = session.getMapper(CustomerDao.class); Customer customer = customerDao.findById(5 ); System.out.println(customer); session.close(); } @Test public void testFindByName () { SqlSession session = MyBatisUtils.getSession(); CustomerDao customerDao = session.getMapper(CustomerDao.class); List<Customer> list = customerDao.findByName("%小%" ); for (Customer cust:list){ System.out.println(cust); } session.close(); } @Test public void testDelete () { SqlSession session = MyBatisUtils.getSession(); CustomerDao customerDao = session.getMapper(CustomerDao.class); customerDao.delete(5 ); session.commit(); session.close(); } }