mybatis operates the database to add, delete, modify and check

  1. Configure MyBatis: In the project configuration file (usually an XML file, for example mybatis-config.xml), configure MyBatis-related information, including database connection information, mapping file path, etc.

  2. Create a mapping file: The mapping file (usually an XML file) defines the mapping relationship between database tables and Java objects, as well as the addition, deletion, modification, and query operations on the database. In the mapping file, you need to define the SQL statement and corresponding parameters.

  3. Create a Java object: Create a Java object corresponding to the database table. The properties of the object correspond to the fields of the database table one-to-one.

  4. Write Java code: Use the API of MyBatis to write Java code to perform add, delete, modify and query operations on the database.

There is a Userdatabase table named idand contains nametwo fields.

  1. Configure MyBatis

mybatis-config.xmlConfigure the database connection information and mapping file path in the file, for example :

<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/mappers/UserMapper.xml"/>
  </mappers>
</configuration>
  1. Create mapping file

UserMapper.xmlDefine the SQL statement and corresponding parameters in the file, for example :

<mapper namespace="com.example.mappers.UserMapper">
  <insert id="insertUser" parameterType="com.example.User">
    INSERT INTO user (id, name) VALUES (#{id}, #{name})
  </insert>
  
  <delete id="deleteUser" parameterType="int">
    DELETE FROM user WHERE id = #{id}
  </delete>
  
  <update id="updateUser" parameterType="com.example.User">
    UPDATE user SET name = #{name} WHERE id = #{id}
  </update>
  
  <select id="getUserById" parameterType="int" resultType="com.example.User">
    SELECT * FROM user WHERE id = #{id}
  </select>
</mapper>
  1. Create Java objects

Create a UserJava object named , containing two properties idand name, as well as corresponding getter and setter methods.

package com.example;

public class User {
    
    
  private int id;
  private String name;

  // Getter and setter methods
  // ...
}
  1. Write Java code

Use MyBatis API to perform database addition, deletion, modification and query operations, for example:

package com.example;

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.Reader;

public class Main {
    
    
  public static void main(String[] args) {
    
    
    try {
    
    
      // 加载MyBatis配置文件
      Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
      // 创建SqlSessionFactory
      SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
      // 创建SqlSession
      SqlSession sqlSession = sqlSessionFactory.openSession();

      // 插入数据
      User user = new User();
      user.setId(1);
      user.setName("John");
      sqlSession.insert("com.example.mappers.UserMapper.insertUser", user);

      // 提交事务
      sqlSession.commit();

      // 查询数据
      User retrievedUser = sqlSession.selectOne("com.example.mappers.UserMapper.getUserById", 1);
      System.out.println(retrievedUser.getName());

      // 更新数据
      retrievedUser.setName("Jane");
      sqlSession.update("com.example.mappers.UserMapper.updateUser", retrievedUser);

      // 提交事务
      sqlSession.commit();

      // 删除数据
      sqlSession.delete("com.example.mappers.UserMapper.deleteUser", 1);

      // 提交事务
      sqlSession.commit();

      // 关闭SqlSession
      sqlSession.close();
    } catch (IOException e) {
    
    
      e.printStackTrace();
    }
  }
}

The above example demonstrates the use of MyBatis to perform database addition, deletion, modification and query operations. The mapping file and Java code can be modified according to actual needs.

Guess you like

Origin blog.csdn.net/kkwyting/article/details/133378537