Start with mybatis-config.xml under Resources


      
<! DOCTYPEconfiguration
        PUBLIC "- / / mybatis.org//DTD Config / 3.0 / EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<! -- Core Configuration file -->
<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://localhost:3306/ssms? useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="cn/zmxy/dao/UsersMapper.xml"/>
    </mappers>
</configuration>
Copy the code

Pojos implement Java classes (corresponding to database tables) in the class layer, such as the Users table

public class Users {
  private int id;
  private String name;
  private String pwd;

  public Users(a){}}Copy the code

Mapping XML and Interface interfaces for the DAO layer

UsersMapper.xml


      
<! DOCTYPEmapper
  PUBLIC "- / / mybatis.org//DTD Mapper / 3.0 / EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<! -- The namespace maintains a corresponding DAO /mapper interface.
<mapper namespace="cn.zmxy.dao.UsersMapper">
 <select id="getUserList" resultType="cn.zmxy.pojo.Users">
  SELECT * FROM ssms.users
    </select>
 <select id="getUserById" resultType="cn.zmxy.pojo.Users" parameterType="int">
  SELECT * FROM ssms.users WHERE id = #{id}
  </select>
 <insert id="addUser" parameterType="cn.zmxy.pojo.Users">
  INSERT INTO ssms.users (id, name, pwd) VALUES (#{id}, #{name}, #{pwd})
    </insert>
 <update id="updateUser" parameterType="cn.zmxy.pojo.Users">
  UPDATE ssms.users SET name = #{name}, pwd = #{pwd} WHERE id = #{id}
  </update>
 <delete id="deleteUser" parameterType="int">
  DELETE FROM ssms.users WHERE id = #{id}
  </delete>
</mapper>
Copy the code

UsersMapper.java

package cn.zmxy.dao;
import cn.zmxy.pojo.Users;
import java.util.List;

public interface UsersMapper {

  // Query all users
  public List<Users> getUserList(a);

  // Query users by ID
  public Users getUserById(int id);

  // Add a user
  public int addUser(Users user);

  // Modify the user
  public int updateUser(Users user);

  // Delete the user
  public int deleteUser(int id);
}
Copy the code

Write an utils class to get SqlSession

MyBatisUtils.java

package cn.zmxy.utils;

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

public class MyBatisUtils {

  private static SqlSessionFactory sqlSessionFactory;

  static {
  try {
  String resource = "mybatis-config.xml";
  InputStream is = Resources.getResourceAsStream(resource);
  sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
 } catch(IOException e) { e.printStackTrace(); }}public static SqlSession getSqlSession(a)
 {  return sqlSessionFactory.openSession();
 }}
Copy the code

And then finally you can call it, write a test class

UserMapperTest.java

package cn.zmxy.dao;
import cn.zmxy.pojo.Users;
import cn.zmxy.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;

public class UserDaoTest {
  @Test
  public void test(a)
 {  // The first step is to get the SqlSession object
  SqlSession sqlSession = MyBatisUtils.getSqlSession();
  // Step 2, execute SQL
  UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class);
  List<Users> userList = usersMapper.getUserList();

  / / print the users
  for (Users users : userList) {
  System.out.println(users);
 }  / / close the SqlSession
  sqlSession.close();
 }
  @Test
  public void getUserById(a)
 {  // The first step is to get the SqlSession object
  SqlSession sqlSession = MyBatisUtils.getSqlSession();
  UsersMapper mapper = sqlSession.getMapper(UsersMapper.class);

  Users user = mapper.getUserById(1);
  System.out.println(user);
  / / close the SqlSession
  sqlSession.close();
 }
  @Test
  public void addUser(a)
 {  // The first step is to get the SqlSession object
  SqlSession sqlSession = MyBatisUtils.getSqlSession();
  UsersMapper mapper = sqlSession.getMapper(UsersMapper.class);

  int num = mapper.addUser(new Users(0."Lulu"."1234"));
  System.out.println("Number of affected rows:" + num);
  sqlSession.commit();
  / / close the SqlSession
  sqlSession.close();
 }
  @Test
  public void updateUser(a)
 {  // The first step is to get the SqlSession object
  SqlSession sqlSession = MyBatisUtils.getSqlSession();
  UsersMapper mapper = sqlSession.getMapper(UsersMapper.class);

  int num = mapper.updateUser(new Users(3."Lulu"."1234"));
  System.out.println("Number of affected rows:" + num);
  sqlSession.commit();
  / / close the SqlSession
  sqlSession.close();
 }
  @Test
  public void deleteUser(a)
 {  // The first step is to get the SqlSession object
  SqlSession sqlSession = MyBatisUtils.getSqlSession();
  UsersMapper mapper = sqlSession.getMapper(UsersMapper.class);

  int num = mapper.deleteUser(11);
  System.out.println("Number of affected rows:" + num);
  sqlSession.commit();
  / / close the SqlSessionsqlSession.close(); }}Copy the code