当前位置: 首页 > news >正文

org.apache.ibatis Test

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/zwf?allowMultiQueries=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="zwf"/>
                <property name="password" value="13805029595"/>
            </dataSource>
        </environment>
    </environments>


    <mappers>
        <mapper resource="StudentMapper.xml" />  <!-- Or use annotation-based mapper -->
    </mappers>

</configuration>

StudentMapper.xml

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


<mapper namespace="com.hz.nft.trade.admin.zwf.StudentMapper">

    <!-- Example SQL statements (adapt as needed) -->
    <insert id="insert" parameterType="com.hz.nft.trade.admin.zwf.Student">
        INSERT INTO student (code, name) VALUES (#{code}, #{name})
    </insert>
    <delete id="deleteById" parameterType="String">
        DELETE FROM student WHERE code = #{code}
    </delete>
    <update id="updateById" parameterType="com.hz.nft.trade.admin.zwf.Student">
        UPDATE student SET name = #{param1.name} WHERE code = #{param1.code}
    </update>
    <select id="selectById" parameterType="String" resultType="com.hz.nft.trade.admin.zwf.Student">
        SELECT code, name FROM student WHERE code = #{code}
    </select>
    <select id="selectAll" resultType="com.hz.nft.trade.admin.zwf.Student">
        SELECT code, name FROM student
    </select>

    <select id="selectList" resultType="com.hz.nft.trade.admin.zwf.Student">
        SELECT code, name FROM student
    </select>

</mapper>
Student.java
package com.hz.nft.trade.admin.zwf;

public class Student
{

    private String code;
    private String name;

    public String getCode()
    {
        return code;
    }

    public void setCode(String code)
    {
        this.code = code;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }
}
StudentMapper.java
package com.hz.nft.trade.admin.zwf;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface StudentMapper extends BaseMapper<Student>
{
    //You can add custom methods here if needed, beyond the BaseMapper's built-in methods.
    List<Student> selectAll();
}

IbatisTest.java
package com.hz.nft.trade.admin.zwf;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class IbatisTest
{

    public static void main(String[] args) throws IOException
    {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession(true);// autoCommit=true

        try
        {
            StudentMapper studentMapper = session.getMapper(StudentMapper.class);

            // 删除
            studentMapper.deleteById("005129");

            // 新增
            Student student = new Student();
            student.setCode("005129");
            student.setName("ZZZZ");
            studentMapper.insert(student);


            // 根据ID查询
            Student selectedStudent = studentMapper.selectById("005129");
            System.out.println("Selected Student: " + selectedStudent);


            // 查询
//            List<Student> allStudents = studentMapper.selectAll(); //Custom method, or use BaseMapper's selectList(null)
            List<Student> allStudents = studentMapper.selectList(null); //Custom method, or use BaseMapper's selectList(null)
            System.out.println("All Students: " + allStudents);


            // 修改
            student.setName("ZWf");
            studentMapper.updateById(student);


            // 删除
            studentMapper.deleteById("005129");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (session != null)
            {
                session.close();
            }
        }
    }
}

相关文章:

  • Pytorch深度学习框架60天进阶学习计划 - 第40天:工业缺陷检测(二)
  • Dubbo(45)如何排查Dubbo的序列化问题?
  • django相关面试题
  • 设计模式:代理模式 - 控制访问与增强功能的艺术
  • AutoGen深度解析:从核心架构到多智能体协作的完整指南
  • 【图片识别改名工具】如何识别图片中文字内容,并根据文字对图片批量重命名批量改名,基于WPF和腾讯OCR的完整实现
  • 【SQL Server 2017】封闭网络下,数据调研所有数据表实战(提效400%)
  • Python 实现的运筹优化系统数学建模详解(0-1规划指派问题)
  • 【人工智能】引爆智能时代的大模型伦理挑战:DeepSeek 如何应对偏见与隐私问题
  • 量子代理签名:量子时代的数字授权革命
  • ubuntu22.04 进入不了系统设置
  • 基于FreeRTOS和LVGL的多功能低功耗智能手表(APP篇)
  • 鸿蒙案例---生肖抽卡
  • 24.0.2 双系统ubuntu 安装显卡驱动黑屏,系统启动界面键盘失灵
  • 跨站点请求伪造(CSRF)原理与Spring Security防护机制详解
  • 数据结构|排序算法(二)插入排序 希尔排序 冒泡排序
  • gerrit上面可以git fetch
  • P8697 [蓝桥杯 2019 国 C] 最长子序列
  • conda-pack打包环境到超算上。解决无法打包可编辑包
  • GIS开发笔记(3)win11环境中osgearth加载大体积全球高程数据(dem)
  • wordpress图片上传大小/长春关键词优化报价
  • 免费做四年级题的网站/百度关键词挖掘查询工具
  • 中国城镇化建设工作委员会网站/seo优化排名经验
  • 中国工程招标网/深圳网站seo
  • 门户网站建设需要注意什么/seo推广经验
  • 武汉免费做网站/百度地图客服人工电话