查询数据库的操作是放在Dao层的,在Dao层我们有以下3种方式使用MyBatis进行数据库的操作。
定义Dao接口并实现其接口
JavaBean
package com.lff;
public class Customers {
private Integer custId;
private String custName;
private String custAddress;
private String custCity;
private String custState;
private String custZip;
private String custCountry;
private String custContact;
private String custEmail;
//setter and getter....
}
mapper文件
<?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="customers">
<insert id="add" parameterType="com.lff.Customers">
INSERT INTO Customers(cust_id,cust_name,cust_address,cust_city,cust_state,cust_zip) values (#{custId},#{custName},#{custAddress},#{custCity},#{custState},#{custZip})
</insert>
<delete id="delete" parameterType="int">
DELETE FROM Customers WHERE cust_id = #{id}
</delete>
<update id="save" parameterType="com.lff.Customers">
UPDATE Customers SET cust_name=#{custName},cust_address = #{custAddress},cust_city=#{custCity},cust_state=#{custState},cust_zip=#{custZip} where cust_id = #{custId}
</update>
<select id="selectOneCustomer" parameterType="int" resultType="com.lff.Customers">
SELECT * FROM Customers WHERE cust_id = #{id}
</select>
</mapper>
DAO接口
package com.lff.dao;
import com.lff.Customers;
public interface CustomerDaoInterface {
public Boolean add(Customers customers); //增
public boolean delete(Integer id); //删
public boolean save(Customers customers); //改
public Customers selectOneCustomer(Integer id); //查
}
Dao实现类
在实现类里面获取到SqlSession对象,进行数据库操作
package com.lff.dao;
import com.lff.Customers;
import com.lff.beans.Person;
import org.apache.ibatis.session.SqlSession;
import utils.MyBatisUtils;
import java.util.List;
public class CustomerDaoImpl implements CustomerDaoInterface{
@Override
public Boolean add(Customers customers) {
Integer effectRows;
try(SqlSession session = MyBatisUtils.openSession()){
effectRows = session.insert("customers.add",customers);
session.commit();
}
return effectRows > 0;
}
@Override
public boolean delete(Integer id) {
Integer effectRows;
try(SqlSession session = MyBatisUtils.openSession()){
effectRows = session.delete("customers.delete",id);
session.commit();
}
return effectRows > 0;
}
@Override
public boolean save(Customers customers) {
Integer effectRows;
try(SqlSession session = MyBatisUtils.openSession()){
effectRows = session.update("customers.save",customers);
session.commit();
}
return effectRows > 0;
}
@Override
public Customers selectOneCustomer(Integer id) {
Customers Customers;
try(SqlSession session = MyBatisUtils.openSession()){
Customers = session.selectOne("customers.selectOneCustomer",id);
}
return Customers;
}
}
外界使用
//增
Customers customers1 = new Customers();
customers1.setCustId(160);
customers1.setCustName("lff");
customers1.setCustState("ss");
customers1.setCustCity("BJ");
customers1.setCustState("CN");
customers1.setCustZip("10001");
CustomerDaoInterface dao = new CustomerDaoImpl();
dao.add(customers1);
//删
CustomerDaoInterface dao = new CustomerDaoImpl();
dao.delete(160);
//改
Customers customers1 = new Customers();
customers1.setCustId(1011);
customers1.setCustName("lifufa");
customers1.setCustState("ss");
customers1.setCustCity("BJ");
customers1.setCustState("CN");
customers1.setCustZip("10001");
dao.save(customers1);
//查
Customers customers = dao.selectOneCustomer(0);
只定义Dao接口,利用SqlSession的getMapper方法生成Dao的代理对象
第一步:先写Dao接口
package com.lff.dao;
import com.lff.Customers;
public interface CustomerDaoInterface {
public Boolean add(Customers customers); //增
public boolean delete(Integer id); //删
public boolean save(Customers customers); //改
public Customers selectOneCustomer(Integer id); //查
}
�第二步:在mapper文件里面要求有两点
- namespace的名称要为Dao接口类的全类名
select、insert、update、delete的id值要与Dao接口中的方法名保持一致 ```xml <?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">
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}
�外界使用
```java
//增
Customers customers1 = new Customers();
customers1.setCustId(160);
customers1.setCustName("lff");
customers1.setCustState("ss");
customers1.setCustCity("BJ");
customers1.setCustState("CN");
customers1.setCustZip("10001");
try(SqlSession session = MyBatisUtils.openSession()){
//使用该方式生成Dao接口的代理对象
CustomerDaoInterface customerDao = session.getMapper(CustomerDaoInterface.class);
customerDao.add(customers1);
session.commit();
}
使用上面的方式生成Dao的代理对象后,就可以使用该对象里面的方法,进行增、删、改、查啦。
//删除
try(SqlSession session = MyBatisUtils.openSession()){
//使用该方式生成Dao接口的代理对象
CustomerDaoInterface customerDao = session.getMapper(CustomerDaoInterface.class);
Boolean isSuccess = customerDao.delete(160); //应该返回影响的行数,但MyBatis内部会判断如果Dao方法名中返回的是Boolean值就做y影响行数>0处理。
session.commit();
}