ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

MyBatis实现数据库增删改查

2021-08-19 17:57:43  阅读:115  来源: 互联网

标签:改查 Studenting studenting 增删 static MyBatis studenting1 public


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);
    }
       

标签:改查,Studenting,studenting,增删,static,MyBatis,studenting1,public
来源: https://blog.csdn.net/lijianyu51314/article/details/119806332

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有