原文連接:http://www.yiidian.com/mybatis/mybatis-dao.htmlhtml
在MyBatis中,咱們有兩種Dao的寫法,一種叫傳統Dao寫法,一種叫Mapper代理接口。下面看看如何實現。java
package com.yiidian.dao; import com.yiidian.domain.Customer; import java.util.List; /** * Dao接口 *一點教程網 - www.yiidian.com */ 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); }
package com.yiidian.dao.impl; import com.yiidian.dao.CustomerDao; import com.yiidian.domain.Customer; import com.yiidian.utils.MyBatisUtils; import org.apache.ibatis.session.SqlSession; import java.util.List; /** * Dao實現類 */ 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語句。sql
<?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"> <!-- namespace: 用於指定該映射文件須要映射的Dao接口 --> <mapper namespace="com.yiidian.dao.CustomerDao"> <!--查詢全部--> <select id="findAll" resultType="com.yiidian.domain.Customer"> select * from t_customer </select> <!--1.添加方法--> <insert id="save" parameterType="com.yiidian.domain.Customer"> INSERT INTO t_customer(NAME,gender,telephone) VALUES(#{name},#{gender},#{telephone}) </insert> <!--2.修改方法--> <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>
package com.yiidian.mybatis; import com.yiidian.dao.CustomerDao; import com.yiidian.dao.impl.CustomerDaoImpl; import com.yiidian.domain.Customer; import com.yiidian.utils.MyBatisUtils; 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.Test; import java.io.IOException; import java.io.InputStream; import java.util.List; /** * MyBatis測試類 - 傳統Dao寫法 * 一點教程網 - www.yiidian.com */ public class TestCustomerDao { /** * 添加 */ @Test public void testSave(){ //1.獲取SqlSession對象 SqlSession session = MyBatisUtils.getSession(); //2.建立傳統Dao實現類對象 CustomerDao customerDao = new CustomerDaoImpl(); //3.調用save方法 Customer customer = new Customer(); customer.setName("小蒼"); customer.setGender("女"); customer.setTelephone("15755556666"); customerDao.save(customer); //4.關閉鏈接 session.close(); } /** * 修改 */ @Test public void testUpdate(){ //1.獲取SqlSession對象 SqlSession session = MyBatisUtils.getSession(); //2.建立傳統Dao實現類對象 CustomerDao customerDao = new CustomerDaoImpl(); //3.調用update方法 Customer customer = new Customer(); customer.setId(5); customer.setName("小澤"); customer.setGender("女"); customer.setTelephone("15755556666"); customerDao.update(customer); session.commit(); //4.關閉鏈接 session.close(); } /** * 查詢全部 */ @Test public void testFindAll(){ //1.獲取SqlSession對象 SqlSession session = MyBatisUtils.getSession(); //2.建立傳統Dao實現類對象 CustomerDao customerDao = new CustomerDaoImpl(); //3.調用findAll方法 List<Customer> list = customerDao.findAll(); for(Customer cust:list){ System.out.println(cust); } //4.關閉鏈接 session.close(); } /** * 查詢一個 */ @Test public void testFindById(){ //1.獲取SqlSession對象 SqlSession session = MyBatisUtils.getSession(); //2.建立傳統Dao實現類對象 CustomerDao customerDao = new CustomerDaoImpl(); //3.調用findById方法 Customer customer = customerDao.findById(3); System.out.println(customer); //4.關閉鏈接 session.close(); } /** * 條件查詢 */ @Test public void testFindByName(){ //1.獲取SqlSession對象 SqlSession session = MyBatisUtils.getSession(); //2.建立傳統Dao實現類對象 CustomerDao customerDao = new CustomerDaoImpl(); //3.調用findByName方法 List<Customer> list = customerDao.findByName("%小%"); for(Customer cust:list){ System.out.println(cust); } //4.關閉鏈接 session.close(); } /** * 刪除 */ @Test public void testDelete(){ //1.獲取SqlSession對象 SqlSession session = MyBatisUtils.getSession(); //2.建立傳統Dao實現類對象 CustomerDao customerDao = new CustomerDaoImpl(); //3.調用findByName方法 customerDao.delete(5); // 提交事務 session.commit(); //4.關閉鏈接 session.close(); } }
package com.yiidian.dao; import com.yiidian.domain.Customer; import java.util.List; /** * Dao接口 *一點教程網 - www.yiidian.com */ 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); }
<?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"> <!-- namespace: 用於指定該映射文件須要映射的Dao接口 --> <mapper namespace="com.yiidian.dao.CustomerDao"> <!--查詢全部--> <select id="findAll" resultType="com.yiidian.domain.Customer"> select * from t_customer </select> <!--1.添加方法--> <insert id="save" parameterType="com.yiidian.domain.Customer"> INSERT INTO t_customer(NAME,gender,telephone) VALUES(#{name},#{gender},#{telephone}) </insert> <!--2.修改方法--> <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>
package com.yiidian.mybatis; import com.yiidian.dao.CustomerDao; import com.yiidian.dao.impl.CustomerDaoImpl; import com.yiidian.domain.Customer; import com.yiidian.utils.MyBatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; /** * MyBatis測試類 - Mapper代理接口 * 一點教程網 - www.yiidian.com */ public class TestCustomerDao2 { /** * 添加 */ @Test public void testSave(){ //1.獲取SqlSession對象 SqlSession session = MyBatisUtils.getSession(); //2.生成Dao代理對象 CustomerDao customerDao = session.getMapper(CustomerDao.class); //3.調用save方法 Customer customer = new Customer(); customer.setName("小蒼"); customer.setGender("女"); customer.setTelephone("15755556666"); customerDao.save(customer); //4.關閉鏈接 session.close(); } /** * 修改 */ @Test public void testUpdate(){ //1.獲取SqlSession對象 SqlSession session = MyBatisUtils.getSession(); //2.生成Dao代理對象 CustomerDao customerDao = session.getMapper(CustomerDao.class); //3.調用update方法 Customer customer = new Customer(); customer.setId(5); customer.setName("小澤"); customer.setGender("女"); customer.setTelephone("15755556666"); customerDao.update(customer); session.commit(); //4.關閉鏈接 session.close(); } /** * 查詢全部 */ @Test public void testFindAll(){ //1.獲取SqlSession對象 SqlSession session = MyBatisUtils.getSession(); //2.生成Dao代理對象 CustomerDao customerDao = session.getMapper(CustomerDao.class); //3.調用findAll方法 List<Customer> list = customerDao.findAll(); for(Customer cust:list){ System.out.println(cust); } //4.關閉鏈接 session.close(); } /** * 查詢一個 */ @Test public void testFindById(){ //1.獲取SqlSession對象 SqlSession session = MyBatisUtils.getSession(); //2.生成Dao代理對象 CustomerDao customerDao = session.getMapper(CustomerDao.class); //3.調用findById方法 Customer customer = customerDao.findById(5); System.out.println(customer); //4.關閉鏈接 session.close(); } /** * 條件查詢 */ @Test public void testFindByName(){ //1.獲取SqlSession對象 SqlSession session = MyBatisUtils.getSession(); //2.生成Dao代理對象 CustomerDao customerDao = session.getMapper(CustomerDao.class); //3.調用findByName方法 List<Customer> list = customerDao.findByName("%小%"); for(Customer cust:list){ System.out.println(cust); } //4.關閉鏈接 session.close(); } /** * 刪除 */ @Test public void testDelete(){ //1.獲取SqlSession對象 SqlSession session = MyBatisUtils.getSession(); //2.生成Dao代理對象 CustomerDao customerDao = session.getMapper(CustomerDao.class); //3.調用findByName方法 customerDao.delete(5); // 提交事務 session.commit(); //4.關閉鏈接 session.close(); } }
源碼下載:https://pan.baidu.com/s/1ZsM5CAu026zfF3wZEA-aFQapache
歡迎關注個人公衆號::一點教程。得到獨家整理的學習資源和平常乾貨推送。 若是您對個人系列教程感興趣,也能夠關注個人網站:yiidian.comsession