码迷,mamicode.com
首页 > 其他好文 > 详细

传统方式实现Dao层

时间:2021-07-19 16:53:38      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:java   nal   public   imp   delete   ref   import   控制   nfa   

传统方式实现Dao层

1.结构图

技术图片

2.controller

package com.ding.controller;

import com.ding.bean.Student;
import com.ding.service.StudentService;
import com.ding.service.impl.StudentServiceImpl;
import org.junit.Test;

import java.util.List;

/**
 * @Description 控制层测试类
 * @Author 丁帅帅
 * @Date 21/07/18 17:02
 * @Version 1.0
 */
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);
    }

}

3.mapper

package com.ding.mapper;

import com.ding.bean.Student;


import java.util.List;

/**
 * @Description 持久层接口
 * @Author 丁帅帅
 * @Date 21/07/18 15:47
 * @Version 1.0
 */
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);


}

package com.ding.mapper.impl;

import com.ding.bean.Student;
import com.ding.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;

/**
 * @Description 持久层实现类
 * @Author 丁帅帅
 * @Date 21/07/18 15:54
 * @Version 1.0
 */
public class StudentMapperImpl implements StudentMapper {


    @Override
    public List<Student> selectAll() {
        InputStream is =null;
        SqlSession sqlSession =null;
        List<Student> list=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 (IOException e) {
            e.printStackTrace();
        }finally {

            if(sqlSession!=null){
                sqlSession.close();
            }
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return list;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Student selectById(Integer id) {
        InputStream is =null;
        SqlSession sqlSession =null;
        Student stu=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 (IOException e) {
            e.printStackTrace();
        }finally {
            //5.释放资源
            if(sqlSession!=null){
                sqlSession.close();
            }
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return stu;
    }

    /**
     * 新增
     * @param stu
     * @return
     */
    @Override
    public Integer insert(Student stu) {
        InputStream is=null;
        SqlSession sqlSession=null;
        Integer result=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();
                }
            }
        }

        return result;
    }

    /**
     * 修改功能
     * @param stu
     * @return
     */
    @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;
    }
}

4.service

package com.ding.service;

import com.ding.bean.Student;

import java.util.List;

/**
 * @Description 业务层接口
 * @Author 丁帅帅
 * @Date 21/07/18 16:53
 * @Version 1.0
 */
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);
}

package com.ding.service.impl;

import com.ding.bean.Student;
import com.ding.mapper.StudentMapper;
import com.ding.mapper.impl.StudentMapperImpl;
import com.ding.service.StudentService;

import java.util.List;

/**
 * @Description 业务层实现类
 * @Author 丁帅帅
 * @Date 21/07/18 16:56
 * @Version 1.0
 */
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);
    }
}

传统方式实现Dao层

标签:java   nal   public   imp   delete   ref   import   控制   nfa   

原文地址:https://www.cnblogs.com/dss-99/p/15028971.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有
迷上了代码!