原文链接:http://www.yiidian.com/mybatis/mybatis-dao.html
在MyBatis中,我们有两种Dao的写法,一种叫传统Dao写法,一种叫Mapper代理接口。下面看看如何实现。
1 传统Dao写法
1.1 编写CustomerDao接口
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);
}
1.2 编写CustomerDao实现类
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语句。
1.3 编写CustomerDao.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">
<!--
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>
1.4 编写测试类
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();
}
}
2 Mapper代理接口
2.1 编写CustomerDao接口
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);
}
2.2 编写CustomerDao.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">
<!--
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>
2.3 编写测试类
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-aFQ
欢迎关注我的公众号::一点教程。获得独家整理的学习资源和日常干货推送。
如果您对我的系列教程感兴趣,也可以关注我的网站:yiidian.com