以下是一个简单的学生管理系统的MyBatis代码示例:

  1. 创建数据库表

首先,我们需要创建一个名为"student"的数据库表,包含以下字段:

  • id: 学生ID,主键,自增
  • name: 学生姓名
  • age: 学生年龄
  • gender: 学生性别
  1. 创建实体类

创建一个名为"Student"的Java实体类,定义与数据库表对应的字段和对应的getter和setter方法。

public class Student {
    private int id;
    private String name;
    private int age;
    private String gender;

    // getter and setter methods
}
  1. 创建Mapper接口

创建一个名为"StudentMapper"的Mapper接口,定义与学生管理相关的数据库操作方法。

public interface StudentMapper {
    void insert(Student student);
    void update(Student student);
    void delete(int id);
    Student getById(int id);
    List<Student> getAll();
}
  1. 创建Mapper XML文件

在resources目录下创建一个名为"StudentMapper.xml"的Mapper XML文件,定义与Mapper接口对应的SQL语句。

<?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.example.mapper.StudentMapper">
    <insert id="insert" parameterType="com.example.entity.Student">
        INSERT INTO student (name, age, gender)
        VALUES (#{name}, #{age}, #{gender})
    </insert>

    <update id="update" parameterType="com.example.entity.Student">
        UPDATE student
        SET name = #{name}, age = #{age}, gender = #{gender}
        WHERE id = #{id}
    </update>

    <delete id="delete" parameterType="int">
        DELETE FROM student
        WHERE id = #{id}
    </delete>

    <select id="getById" resultType="com.example.entity.Student" parameterType="int">
        SELECT * FROM student
        WHERE id = #{id}
    </select>

    <select id="getAll" resultType="com.example.entity.Student">
        SELECT * FROM student
    </select>
</mapper>
  1. 创建MyBatis配置文件

在resources目录下创建一个名为"mybatis-config.xml"的MyBatis配置文件,配置数据库连接信息和Mapper 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.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mydatabase"/>
                <property name="username" value="root"/>
                <property name="password" value="password"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/example/mapper/StudentMapper.xml"/>
    </mappers>
</configuration>
  1. 创建MyBatis工具类

创建一个名为"MyBatisUtil"的工具类,用于初始化MyBatis配置并获取SqlSessionFactory和SqlSession对象。

public class MyBatisUtil {
    private static SqlSessionFactory sqlSessionFactory;

    static {
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSessionFactory getSqlSessionFactory() {
        return sqlSessionFactory;
    }

    public static SqlSession getSqlSession() {
        return sqlSessionFactory.openSession();
    }
}
  1. 测试

使用以上代码,可以进行学生管理的数据库操作。

public class Main {
    public static void main(String[] args) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);

        // 插入学生信息
        Student student1 = new Student();
        student1.setName("张三");
        student1.setAge(18);
        student1.setGender("男");
        studentMapper.insert(student1);
        sqlSession.commit();

        // 更新学生信息
        Student student2 = studentMapper.getById(1);
        student2.setName("李四");
        studentMapper.update(student2);
        sqlSession.commit();

        // 删除学生信息
        studentMapper.delete(1);
        sqlSession.commit();

        // 查询所有学生信息
        List<Student> students = studentMapper.getAll();
        for (Student student : students) {
            System.out.println(student.getName());
        }

        sqlSession.close();
    }
}

以上代码示例了如何使用MyBatis进行学生信息的插入、更新、删除和查询操作

学生管理系统mybatis代码

原文地址: https://www.cveoy.top/t/topic/ijBe 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录