1.原始方法开发Dao
Dao接口
package cn.sm1234.dao; import java.util.List; import cn.sm1234.domain.Customer; public interface CustomerDao { public void saveCustomer(Customer customer); public void updateCustomer(Customer customer); public void deleteCustomer(Integer id); public List<Customer> queryAllCustomer(); public Customer queryCustomerById(Integer id); public List<Customer> queryCustomerByName(String name);
}
Dao实现:
package cn.sm1234.dao.impl; import java.util.List; import org.apache.ibatis.session.SqlSession; import cn.sm1234.dao.CustomerDao;
import cn.sm1234.domain.Customer;
import cn.sm1234.utils.SessionUtils; public class CustomerDaoImpl implements CustomerDao { @Override
public void saveCustomer(Customer customer) {
SqlSession sqlSession = null;
try {
sqlSession = SessionUtils.getSesion();
sqlSession.insert("insertCustomer", customer);
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
} finally {
sqlSession.close();
}
} @Override
public void updateCustomer(Customer customer) {
SqlSession sqlSession = null;
try {
sqlSession = SessionUtils.getSesion();
sqlSession.update("updateCustomer", customer);
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
} finally {
sqlSession.close();
} } @Override
public void deleteCustomer(Integer id) {
SqlSession sqlSession = null;
try {
sqlSession = SessionUtils.getSesion();
sqlSession.delete("deleteCustomer", id);
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
} finally {
sqlSession.close();
} } @Override
public List<Customer> queryAllCustomer() {
SqlSession sqlSession = null;
try {
sqlSession = SessionUtils.getSesion();
return sqlSession.selectList("queryAllCustomer");
} catch (Exception e) {
e.printStackTrace();
} finally {
sqlSession.close();
}
return null;
} @Override
public Customer queryCustomerById(Integer id) {
SqlSession sqlSession = null;
try {
sqlSession = SessionUtils.getSesion();
return sqlSession.selectOne("queryCustomerById", id);
} catch (Exception e) {
e.printStackTrace();
} finally {
sqlSession.close();
}
return null;
} @Override
public List<Customer> queryCustomerByName(String name) {
SqlSession sqlSession = null;
try {
sqlSession = SessionUtils.getSesion();
return sqlSession.selectList("queryCustomerById", name);
} catch (Exception e) {
e.printStackTrace();
} finally {
sqlSession.close();
}
return null;
} }
测试类:
package cn.sm1234.test; import java.util.List; import org.junit.Test; import cn.sm1234.dao.CustomerDao;
import cn.sm1234.dao.impl.CustomerDaoImpl;
import cn.sm1234.domain.Customer; public class Demo1 { @Test
public void test1(){
Customer c = new Customer();
c.setName("陈六222");
c.setGender("男");
c.setTelephone("13244445555"); CustomerDao dao = new CustomerDaoImpl();
dao.saveCustomer(c);
} @Test
public void test2(){
Customer c = new Customer();
c.setId(1);
c.setName("李四"); CustomerDao dao = new CustomerDaoImpl();
dao.updateCustomer(c);
} @Test
public void test3(){
CustomerDao dao = new CustomerDaoImpl();
dao.deleteCustomer(14);
} @Test
public void test4(){
CustomerDao dao = new CustomerDaoImpl();
List<Customer> list = dao.queryAllCustomer();
for (Customer customer : list) {
System.out.println(customer);
}
} @Test
public void test5(){
CustomerDao dao = new CustomerDaoImpl();
Customer customer = dao.queryCustomerById(1);
System.out.println(customer);
} @Test
public void test6(){
CustomerDao dao = new CustomerDaoImpl();
List<Customer> list = dao.queryCustomerByName("%陈%");
for (Customer customer : list) {
System.out.println(customer);
}
}
}
Customer.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">
<!-- 该文件存放CRUD的sql语句 -->
<mapper namespace="test">
<!-- 添加 -->
<insert id="insertCustomer" parameterType="customer">
INSERT INTO t_customer(NAME,gender,telephone) VALUES(#{name},#{gender},#{telephone})
</insert> <!-- 修改 -->
<!-- parameterType传入对象,包含需要使用的值 -->
<update id="updateCustomer" parameterType="customer">
UPDATE t_customer SET NAME = #{name} WHERE id = #{id}
</update> <!-- 查询所有数据 -->
<!-- 输出映射 resultType -->
<!-- parameterType可以省略,resultType不可以省略 -->
<select id="queryAllCustomer" resultType="customer">
SELECT * FROM t_customer
</select> <!-- 根据id查询 -->
<select id="queryCustomerById" parameterType="_int" resultType="customer">
SELECT * FROM t_customer WHERE id=#{value}
</select> <!-- 根据name模糊查询 -->
<select id="queryCustomerByName" parameterType="string" resultType="customer">
<!-- 方法一 -->
SELECT * FROM t_customer WHERE NAME LIKE #{value}
<!-- 方法二 -->
<!-- SELECT * FROM t_customer WHERE NAME LIKE '%${value}%' -->
</select> <!-- 删除 -->
<delete id="deleteCustomer" parameterType="int">
DELETE FROM t_customer WHERE id=#{value}
</delete> </mapper>
缺点:代码繁琐。
解决方法:利用动态代理方式Dao接口开发。Dao层只需要接口,不需要重复的Dao层实现。
2.动态代理方式开发Dao层(推荐使用)
好处:无需再去编写Dao层的实现类。
Dao接口:
package cn.sm1234.dao; import java.util.List; import cn.sm1234.domain.Customer; public interface CustomerDao { public void saveCustomer(Customer customer); public void updateCustomer(Customer customer); public void deleteCustomer(Integer id); public List<Customer> queryAllCustomer(); public Customer queryCustomerById(Integer id); public List<Customer> queryCustomerByName(String name);
}
Customer.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">
<!-- 该文件存放CRUD的sql语句 -->
<!--
如果是动态代理方式
1)namespace必须和Dao接口的路径保持一致
2)Dao接口里面的方法和sql语句的id保持一致
3)Dao接口的方法的参数和返回值类型 和 映射文件的parameterType和resultType要对应
-->
<mapper namespace="cn.sm1234.dao.CustomerDao">
<!-- 添加 -->
<insert id="saveCustomer" parameterType="customer">
INSERT INTO t_customer(NAME,gender,telephone) VALUES(#{name},#{gender},#{telephone})
</insert> <!-- 修改 -->
<!-- parameterType传入对象,包含需要使用的值 -->
<update id="updateCustomer" parameterType="customer">
UPDATE t_customer SET NAME = #{name} WHERE id = #{id}
</update> <!-- 查询所有数据 -->
<!-- 输出映射 resultType -->
<!-- parameterType可以省略,resultType不可以省略 -->
<select id="queryAllCustomer" resultType="customer">
SELECT * FROM t_customer
</select> <!-- 根据id查询 -->
<select id="queryCustomerById" parameterType="_int" resultType="customer">
SELECT * FROM t_customer WHERE id=#{value}
</select> <!-- 根据name模糊查询 -->
<select id="queryCustomerByName" parameterType="string" resultType="customer">
<!-- 方法一 -->
SELECT * FROM t_customer WHERE NAME LIKE #{value}
<!-- 方法二 -->
<!-- SELECT * FROM t_customer WHERE NAME LIKE '%${value}%' -->
</select> <!-- 删除 -->
<delete id="deleteCustomer" parameterType="int">
DELETE FROM t_customer WHERE id=#{value}
</delete> </mapper>
测试代码:
package cn.sm1234.test; import java.util.List; import org.apache.ibatis.session.SqlSession;
import org.junit.Test; import cn.sm1234.dao.CustomerDao;
import cn.sm1234.domain.Customer;
import cn.sm1234.utils.SessionUtils; public class Demo2 { @Test
public void test1(){
Customer c = new Customer();
c.setName("陈六333");
c.setGender("男");
c.setTelephone("13244445555"); SqlSession sqlSession = SessionUtils.getSession();
//getMapper(): 返回指定接口的动态代理的实现类对象
CustomerDao dao = sqlSession.getMapper(CustomerDao.class);
dao.saveCustomer(c);
sqlSession.commit();
sqlSession.close();
} @Test
public void test2(){
Customer c = new Customer();
c.setId(1);
c.setName("李四222"); SqlSession sqlSession = SessionUtils.getSession();
//getMapper(): 返回指定接口的动态代理的实现类对象
CustomerDao dao = sqlSession.getMapper(CustomerDao.class);
dao.updateCustomer(c);
sqlSession.commit();
sqlSession.close();
} @Test
public void test3(){
SqlSession sqlSession = SessionUtils.getSession();
CustomerDao dao = sqlSession.getMapper(CustomerDao.class);
dao.deleteCustomer(19);
sqlSession.commit();
sqlSession.close();
} @Test
public void test4(){
SqlSession sqlSession = SessionUtils.getSession();
CustomerDao dao = sqlSession.getMapper(CustomerDao.class);
List<Customer> list = dao.queryAllCustomer();
for (Customer customer : list) {
System.out.println(customer);
}
} @Test
public void test5(){
SqlSession sqlSession = SessionUtils.getSession();
CustomerDao dao = sqlSession.getMapper(CustomerDao.class);
Customer customer = dao.queryCustomerById(1);
System.out.println(customer);
} @Test
public void test6(){
SqlSession sqlSession = SessionUtils.getSession();
CustomerDao dao = sqlSession.getMapper(CustomerDao.class);
List<Customer> list = dao.queryCustomerByName("%陈%");
for (Customer customer : list) {
System.out.println(customer);
}
}
}
要点总结:
如果是动态代理方式
1)namespace必须和Dao接口的路径保持一致
2)Dao接口里面的方法和sql语句的id保持一致
3) Dao接口的方法的参数和返回值类型 和 映射文件的parameterType和resultType要对应