MyBatis接口实现方式实现Dao层

博客围绕MyBatis接口代理方式的实现规则展开,涉及Java和MySQL相关知识,介绍了该方式在信息技术领域的具体实现规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

接口代理方式-实现规则

在这里插入图片描述
在这里插入图片描述

<?xml version="1.0" encoding="UTF-8" ?>
<!--MyBatis的DTD约束-->
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!--
    mapper 核心的根标签
    namespces属性:名称空间
-->
<mapper namespace="com.itheima.mapper.StudentMapper">

    <!--
   select 查询功能的标签
   id属性:唯一标识
   resultType属性:指定结果映射对象类型
   paramenterType属性:指定才是映射对象类型
-->
    <select id="selectAll" resultType="student">
        select * from student
    </select>
    <select id="selectById" resultType="student" parameterType="int">
        select * from student where id = #{id}
    </select>
    <insert id="insert" parameterType="student">
        insert into student values(#{id},#{name},#{age})
    </insert>
    <update id="update" parameterType="student">
        update student set name =#{name},age=#{age} where id =#{id}
    </update>
    <delete id="delete" parameterType="int">
        delete from student where id =#{id}
    </delete>
</mapper>
package com.itheima.service.impl;

import com.itheima.bean.Student;
import com.itheima.mapper.StudentMapper;
import com.itheima.service.StudentService;
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 StudentServiceImpl implements StudentService {



    @Override
    public List<Student> selectAll() {
       /// 1 加载核心配置文件
        List<Student> students=null;
        SqlSession sqlSession=null;
        InputStream inputStream =null;
        try {
            inputStream = Resources.getResourceAsStream("MyBatisConfig.xml");

            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            sqlSession = sqlSessionFactory.openSession(true);

            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

            students= mapper.selectAll();

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

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

        }

        return students;
        /// 2 获取SqlSession工厂对象

        /// 通过工厂对象获取SqlSession对象

        // 获取StudentMapper接口实现类对象

        // 通过实现类对象调用方法,接收返回结果

        // 释放资源

        ///返回结果
    }

    @Override
    public Student selectById(Integer id) {
       Student students=null;
        SqlSession sqlSession=null;
        InputStream inputStream =null;
        try {
            inputStream = Resources.getResourceAsStream("MyBatisConfig.xml");

            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            sqlSession = sqlSessionFactory.openSession(true);

            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

            students= mapper.selectById(id);

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

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

        }

        return students;
    }

    @Override
    public Integer insert(Student stu) {

        Integer result=null;
        SqlSession sqlSession=null;
        InputStream inputStream =null;
        try {
            inputStream = Resources.getResourceAsStream("MyBatisConfig.xml");

            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            sqlSession = sqlSessionFactory.openSession(true);

            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

            result = mapper.insert(stu);

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

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

        }

        return result;

    }

    @Override
    public Integer update(Student stu) {
        Integer result=null;
        SqlSession sqlSession=null;
        InputStream inputStream =null;
        try {
            inputStream = Resources.getResourceAsStream("MyBatisConfig.xml");

            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            sqlSession = sqlSessionFactory.openSession(true);

            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

            result = mapper.update(stu);

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

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

        }

        return result;
    }

    @Override
    public Integer delete(Integer id) {
        Integer result=null;
        SqlSession sqlSession=null;
        InputStream inputStream =null;
        try {
            inputStream = Resources.getResourceAsStream("MyBatisConfig.xml");

            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            sqlSession = sqlSessionFactory.openSession(true);

            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

            result = mapper.delete(id);

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

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

        }

        return result;
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值