Mybatis传统方式开发
Dao 层传统实现方式
-
分层思想:控制层(controller)、业务层(service)、持久层(dao)。
-
调用流程
-
在日常开发过程中,排查问题时难免需要输出 MyBatis 真正执行的 SQL 语句、参数、结果等信息,我们就可以借助 LOG4J 的功能来实现执行信息的输出。
-
使用步骤:
<!--配置LOG4J--> <settings> <setting name="logImpl" value="log4j"/> </settings>
持久层(Dao):
接口(StudentMapper):
package com.itheima.mapper; import com.itheima.bean.Student; import java.util.List; /* 持久层接口 */ public interface StudentMapper { //查询全部 public abstract List<Student> selectAll(); //根据id查询 public abstract Student selectById(Integer id); //新增数据 public abstract Integer insert(Student stu); //修改数据 public abstract Integer update(Student stu); //删除数据 public abstract Integer delete(Integer id); }
实现类(StudentMapperImpl):
package com.itheima.mapper.impl; import com.itheima.bean.Student; import com.itheima.mapper.StudentMapper; 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 java.io.IOException; import java.io.InputStream; import java.util.List; /* 持久层实现类 */ public class StudentMapperImpl implements StudentMapper { /* 查询全部 */ @Override public List<Student> selectAll() { List<Student> list = null; SqlSession sqlSession = null; InputStream is = null; try{ //1.加载核心配置文件 is = Resources.getResourceAsStream("MyBatisConfig.xml"); //2.获取SqlSession工厂对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); //3.通过工厂对象获取SqlSession对象 sqlSession = sqlSessionFactory.openSession(true); //4.执行映射配置文件中的sql语句,并接收结果 list = sqlSession.selectList("StudentMapper.selectAll"); } catch (Exception e) { e.printStackTrace(); } finally { //5.释放资源 if(sqlSession != null) { sqlSession.close(); } if(is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } //6.返回结果 return list; } /* 根据id查询 */ @Override public Student selectById(Integer id) { Student stu = null; SqlSession sqlSession = null; InputStream is = null; try{ //1.加载核心配置文件 is = Resources.getResourceAsStream("MyBatisConfig.xml"); //2.获取SqlSession工厂对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); //3.通过工厂对象获取SqlSession对象 sqlSession = sqlSessionFactory.openSession(true); //4.执行映射配置文件中的sql语句,并接收结果 stu = sqlSession.selectOne("StudentMapper.selectById",id); } catch (Exception e) { e.printStackTrace(); } finally { //5.释放资源 if(sqlSession != null) { sqlSession.close(); } if(is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } //6.返回结果 return stu; } /* 新增功能 */ @Override public Integer insert(Student stu) { Integer result = null; SqlSession sqlSession = null; InputStream is = null; try{ //1.加载核心配置文件 is = Resources.getResourceAsStream("MyBatisConfig.xml"); //2.获取SqlSession工厂对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); //3.通过工厂对象获取SqlSession对象 sqlSession = sqlSessionFactory.openSession(true); //4.执行映射配置文件中的sql语句,并接收结果 result = sqlSession.insert("StudentMapper.insert",stu); } catch (Exception e) { e.printStackTrace(); } finally { //5.释放资源 if(sqlSession != null) { sqlSession.close(); } if(is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } //6.返回结果 return result; } /* 修改功能 */ @Override public Integer update(Student stu) { Integer result = null; SqlSession sqlSession = null; InputStream is = null; try{ //1.加载核心配置文件 is = Resources.getResourceAsStream("MyBatisConfig.xml"); //2.获取SqlSession工厂对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); //3.通过工厂对象获取SqlSession对象 sqlSession = sqlSessionFactory.openSession(true); //4.执行映射配置文件中的sql语句,并接收结果 result = sqlSession.update("StudentMapper.update",stu); } catch (Exception e) { e.printStackTrace(); } finally { //5.释放资源 if(sqlSession != null) { sqlSession.close(); } if(is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } //6.返回结果 return result; } /* 删除功能 */ @Override public Integer delete(Integer id) { Integer result = null; SqlSession sqlSession = null; InputStream is = null; try{ //1.加载核心配置文件 is = Resources.getResourceAsStream("MyBatisConfig.xml"); //2.获取SqlSession工厂对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); //3.通过工厂对象获取SqlSession对象 sqlSession = sqlSessionFactory.openSession(true); //4.执行映射配置文件中的sql语句,并接收结果 result = sqlSession.delete("StudentMapper.delete",id); } catch (Exception e) { e.printStackTrace(); } finally { //5.释放资源 if(sqlSession != null) { sqlSession.close(); } if(is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } //6.返回结果 return result; } }
业务层(Service层):
接口(StudentService):
package com.itheima.service; import com.itheima.bean.Student; import java.util.List; /* 业务层接口 */ public interface StudentService { //查询全部 public abstract List<Student> selectAll(); //根据id查询 public abstract Student selectById(Integer id); //新增数据 public abstract Integer insert(Student stu); //修改数据 public abstract Integer update(Student stu); //删除数据 public abstract Integer delete(Integer id); }
接口实现类(StudentServiceImpl):
package com.itheima.service.impl; import com.itheima.bean.Student; import com.itheima.mapper.StudentMapper; import com.itheima.mapper.impl.StudentMapperImpl; import com.itheima.service.StudentService; import java.util.List; /* 业务层实现类 */ public class StudentServiceImpl implements StudentService { //创建持久层对象 private StudentMapper mapper = new StudentMapperImpl(); @Override public List<Student> selectAll() { return mapper.selectAll(); } @Override public Student selectById(Integer id) { return mapper.selectById(id); } @Override public Integer insert(Student stu) { return mapper.insert(stu); } @Override public Integer update(Student stu) { return mapper.update(stu); } @Override public Integer delete(Integer id) { return mapper.delete(id); } }
表现层(controller):
package com.itheima.controller; import com.itheima.bean.Student; import com.itheima.service.StudentService; import com.itheima.service.impl.StudentServiceImpl; import org.junit.Test; import java.util.List; /* 控制层测试类 */ public class StudentController { //创建业务层对象 private StudentService service = new StudentServiceImpl(); //查询全部功能测试 @Test public void selectAll() { List<Student> students = service.selectAll(); for (Student stu : students) { System.out.println(stu); } } //根据id查询功能测试 @Test public void selectById() { Student stu = service.selectById(3); System.out.println(stu); } //新增功能测试 @Test public void insert() { Student stu = new Student(4,"赵六",26); Integer result = service.insert(stu); System.out.println(result); } //修改功能测试 @Test public void update() { Student stu = new Student(4,"赵六",16); Integer result = service.update(stu); System.out.println(result); } //删除功能测试 @Test public void delete() { Integer result = service.delete(4); System.out.println(result); } }