查询数据库的操作是放在Dao层的,在Dao层我们有以下3种方式使用MyBatis进行数据库的操作。

定义Dao接口并实现其接口

对《SQL必知必会》里面的客户表进行操作。

JavaBean

  1. package com.lff;
  2. public class Customers {
  3. private Integer custId;
  4. private String custName;
  5. private String custAddress;
  6. private String custCity;
  7. private String custState;
  8. private String custZip;
  9. private String custCountry;
  10. private String custContact;
  11. private String custEmail;
  12. //setter and getter....
  13. }

mapper文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="customers">
  6. <insert id="add" parameterType="com.lff.Customers">
  7. INSERT INTO Customers(cust_id,cust_name,cust_address,cust_city,cust_state,cust_zip) values (#{custId},#{custName},#{custAddress},#{custCity},#{custState},#{custZip})
  8. </insert>
  9. <delete id="delete" parameterType="int">
  10. DELETE FROM Customers WHERE cust_id = #{id}
  11. </delete>
  12. <update id="save" parameterType="com.lff.Customers">
  13. UPDATE Customers SET cust_name=#{custName},cust_address = #{custAddress},cust_city=#{custCity},cust_state=#{custState},cust_zip=#{custZip} where cust_id = #{custId}
  14. </update>
  15. <select id="selectOneCustomer" parameterType="int" resultType="com.lff.Customers">
  16. SELECT * FROM Customers WHERE cust_id = #{id}
  17. </select>
  18. </mapper>

DAO接口

  1. package com.lff.dao;
  2. import com.lff.Customers;
  3. public interface CustomerDaoInterface {
  4. public Boolean add(Customers customers); //增
  5. public boolean delete(Integer id); //删
  6. public boolean save(Customers customers); //改
  7. public Customers selectOneCustomer(Integer id); //查
  8. }

Dao实现类

在实现类里面获取到SqlSession对象,进行数据库操作

  1. package com.lff.dao;
  2. import com.lff.Customers;
  3. import com.lff.beans.Person;
  4. import org.apache.ibatis.session.SqlSession;
  5. import utils.MyBatisUtils;
  6. import java.util.List;
  7. public class CustomerDaoImpl implements CustomerDaoInterface{
  8. @Override
  9. public Boolean add(Customers customers) {
  10. Integer effectRows;
  11. try(SqlSession session = MyBatisUtils.openSession()){
  12. effectRows = session.insert("customers.add",customers);
  13. session.commit();
  14. }
  15. return effectRows > 0;
  16. }
  17. @Override
  18. public boolean delete(Integer id) {
  19. Integer effectRows;
  20. try(SqlSession session = MyBatisUtils.openSession()){
  21. effectRows = session.delete("customers.delete",id);
  22. session.commit();
  23. }
  24. return effectRows > 0;
  25. }
  26. @Override
  27. public boolean save(Customers customers) {
  28. Integer effectRows;
  29. try(SqlSession session = MyBatisUtils.openSession()){
  30. effectRows = session.update("customers.save",customers);
  31. session.commit();
  32. }
  33. return effectRows > 0;
  34. }
  35. @Override
  36. public Customers selectOneCustomer(Integer id) {
  37. Customers Customers;
  38. try(SqlSession session = MyBatisUtils.openSession()){
  39. Customers = session.selectOne("customers.selectOneCustomer",id);
  40. }
  41. return Customers;
  42. }
  43. }

外界使用

  1. //增
  2. Customers customers1 = new Customers();
  3. customers1.setCustId(160);
  4. customers1.setCustName("lff");
  5. customers1.setCustState("ss");
  6. customers1.setCustCity("BJ");
  7. customers1.setCustState("CN");
  8. customers1.setCustZip("10001");
  9. CustomerDaoInterface dao = new CustomerDaoImpl();
  10. dao.add(customers1);
  1. //删
  2. CustomerDaoInterface dao = new CustomerDaoImpl();
  3. dao.delete(160);
  1. //改
  2. Customers customers1 = new Customers();
  3. customers1.setCustId(1011);
  4. customers1.setCustName("lifufa");
  5. customers1.setCustState("ss");
  6. customers1.setCustCity("BJ");
  7. customers1.setCustState("CN");
  8. customers1.setCustZip("10001");
  9. dao.save(customers1);
  1. //查
  2. Customers customers = dao.selectOneCustomer(0);

只定义Dao接口,利用SqlSession的getMapper方法生成Dao的代理对象

第一步:先写Dao接口

  1. package com.lff.dao;
  2. import com.lff.Customers;
  3. public interface CustomerDaoInterface {
  4. public Boolean add(Customers customers); //增
  5. public boolean delete(Integer id); //删
  6. public boolean save(Customers customers); //改
  7. public Customers selectOneCustomer(Integer id); //查
  8. }

�第二步:在mapper文件里面要求有两点

  • namespace的名称要为Dao接口类的全类名
  • select、insert、update、delete的id值要与Dao接口中的方法名保持一致 ```xml <?xml version=”1.0” encoding=”UTF-8” ?> <!DOCTYPE mapper

    1. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    2. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    INSERT INTO Customers(cust_id,cust_name,cust_address,cust_city,cust_state,cust_zip) values (#{custId},#{custName},#{custAddress},#{custCity},#{custState},#{custZip}) DELETE FROM Customers WHERE cust_id = #{id} UPDATE Customers SET cust_name=#{custName},cust_address = #{custAddress},cust_city=#{custCity},cust_state=#{custState},cust_zip=#{custZip} where cust_id = #{custId}

  1. �外界使用
  2. ```java
  3. //增
  4. Customers customers1 = new Customers();
  5. customers1.setCustId(160);
  6. customers1.setCustName("lff");
  7. customers1.setCustState("ss");
  8. customers1.setCustCity("BJ");
  9. customers1.setCustState("CN");
  10. customers1.setCustZip("10001");
  11. try(SqlSession session = MyBatisUtils.openSession()){
  12. //使用该方式生成Dao接口的代理对象
  13. CustomerDaoInterface customerDao = session.getMapper(CustomerDaoInterface.class);
  14. customerDao.add(customers1);
  15. session.commit();
  16. }

使用上面的方式生成Dao的代理对象后,就可以使用该对象里面的方法,进行增、删、改、查啦。

  1. //删除
  2. try(SqlSession session = MyBatisUtils.openSession()){
  3. //使用该方式生成Dao接口的代理对象
  4. CustomerDaoInterface customerDao = session.getMapper(CustomerDaoInterface.class);
  5. Boolean isSuccess = customerDao.delete(160); //应该返回影响的行数,但MyBatis内部会判断如果Dao方法名中返回的是Boolean值就做y影响行数>0处理。
  6. session.commit();
  7. }