MyBatis实现数据库增删改查

2021/8/19 19:06:36

本文主要是介绍MyBatis实现数据库增删改查,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

MyBatis查询数据
1、配置好maven项目
配置步骤点击 MyBatis使用
<--------------------------------->
2、接口实现类写查询数据代码

    <!-- 查询全部数据-->
    <select id="allStudenting" resultType="com.dh.entity.Studenting">
        select * from studenting
    </select>
    <!-- 以id为条件查询单个数据-->
    <select id="checkStudenting" resultType="com.dh.entity.Studenting">
        select * from studenting where id = #{id}
    </select>

3、新建service接口层和接口实现类
在这里插入图片描述
4、在service层的接口实现类实现dao层业务
在这里插入图片描述
5、封装MyBatis工具类(增删改查都要用的MyBatis工具类,封装在一起减少代码重复)
在这里插入图片描述

package com.dh.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionManager;

import java.io.IOException;
import java.io.InputStream;

public class MyBatis {
    static SqlSessionManager sqlSessionManager;

    static {
        String mybatis = "mybatis.xml";
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = Resources.getResourceAsStream(mybatis);
            sqlSessionManager = SqlSessionManager.newInstance(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
            sqlSessionManager = null;
        }
    }

    public static <T> T getMapper(Class<T> var1) {
        return sqlSessionManager.getMapper(var1);
    }

    // 获取sqlSession对象
    public static SqlSessionManager getSqlSessionManager() {
        if (sqlSessionManager != null) {
            return sqlSessionManager;
        }
        return null;
    }
}

6、全部查询和单个查询代码:

   @Override
    public List<Studenting> allStudenting() {
     // 调用MyBatis工具类
        StudentingDao studentingDao = MyBatis.getMapper(StudentingDao.class);
        List<Studenting> studentings = studentingDao.allStudenting();
        return studentings;
    }

    @Override
    public Studenting checkStudenting(Studenting studenting) {
    // 调用MyBatis工具类
        StudentingDao studentingDao = MyBatis.getMapper(StudentingDao.class);
        Studenting studenting1 = studentingDao.checkStudenting(studenting);
        return studenting1;
    }

7、测试
测试类调用接口实现类时注意设为静态(在类中,使用 static 修饰符修饰的属性(成员变量)称为静态变量,也可以称为类变量,常量称为静态常量,方法称为静态方法或类方法,它们统称为静态成员,归整个类所有。

静态成员不依赖于类的特定实例,被类的所有实例共享,就是说 static 修饰的方法或者变量不需要依赖于对象来进行访问,只要这个类被加载,Java 虚拟机就可以根据类名找到它们。)

static StudentServiceDao studentServiceDao = new StudentingServiceDaoImpl();
public static void main(String[] args) {
        // 单个查询
        Studenting studenting4 = new Studenting();
        studenting4.setId(10);
        Studenting studenting = studentServiceDao.checkStudenting(studenting4);
        // 全部查询
        List<Studenting> studentings = studentServiceDao.allStudenting();
    }

MyBatis增加数据
1、接口实现类写增加数据代码

    <insert id="addStudenting">
        <!-- 注意传入的id,name值要以#{}表示-->
        insert into studenting (id,name) values (#{id},#{name})
    </insert>

2、在service层的接口实现类写人增加数据代码

    @Override
    public int addStudenting(Studenting studenting) {
    // 调用MyBatis工具类
        StudentingDao studentingDao = MyBatis.getMapper(StudentingDao.class);
        int studenting1 = studentingDao.addStudenting(studenting);
        return studenting1;
    }

3、测试

static StudentServiceDao studentServiceDao = new StudentingServiceDaoImpl();
public static void main(String[] args) {
        // 增加数据库数据
        Studenting studenting1 = new Studenting();
        studenting1.setName("小李");
        int i = studentServiceDao.addStudenting(studenting1);
    }

MyBatis删除数据
1、接口实现类写删除数据代码

    <delete id="deleteStudenting">
        delete from studenting where id = #{id}
    </delete>

2、在service层的接口实现类写人删除数据代码

    @Override
    public int deleteStudenting(Studenting studenting) {
    // 调用MyBatis工具类
        StudentingDao studentingDao = MyBatis.getMapper(StudentingDao.class);
        int studenting1 = studentingDao.deleteStudenting(studenting);
        return studenting1;
    }

3、测试

static StudentServiceDao studentServiceDao = new StudentingServiceDaoImpl();
public static void main(String[] args) {
        // 删除数据库数据
        int i1 = studentServiceDao.changeStudenting(studenting2);
        Studenting studenting3 = new Studenting();
        studenting3.setId(9);
        int i3 = studentServiceDao.deleteStudenting(studenting3);
    }

MyBatis更改数据
1、接口实现类写更改数据代码

    <update id="changeStudenting">
        update studenting set name = #{name} where id = #{id}
    </update>

2、在service层的接口实现类写人更改数据代码

    @Override
    public int changeStudenting(Studenting studenting) {
    // 调用MyBatis工具类
        StudentingDao studentingDao = MyBatis.getMapper(StudentingDao.class);
        int studenting1 = studentingDao.changeStudenting(studenting);
        return studenting1;
    }

3、测试

static StudentServiceDao studentServiceDao = new StudentingServiceDaoImpl();
public static void main(String[] args) {
        // 更改数据库数据
        Studenting studenting2 = new Studenting();
        studenting2.setId(8);
        studenting2.setName("小丽");
        int i1 = studentServiceDao.changeStudenting(studenting2);
    }
       


这篇关于MyBatis实现数据库增删改查的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程