Mybatis基本配置以及CRUD

Mybatis框架学习

1.0、Mybatis常用结构

MybatisUtils 帮助类 可以直接使用此类

package com.chris.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;

    private static final String sqlTypes = null;

    // 静态代码块 固定语法
    static {
        try {
            // 使用Mybatis第一步  获取SqlSessionFactory
            String resource = "mybatis-config-mysql.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /***
     * 既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。
     * 你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句。例如:
     */
    public static SqlSession getSqlSession() {
        return sqlSessionFactory.openSession();
    }

}

1.00、UserMapper 接口类

package com.chris.dao;

import com.chris.pojo.User;

import java.util.List;

public interface UserMapper {

    List<User> getListUser();

    User getUserById(int Id);

    int addUser(User user);

    int updateUser(User user);

    int delUser(int id);
}

1.01、UserMapper.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.chris.dao.UserMapper">
    <select id="getListUser" resultType="com.chris.pojo.User">
     select * from chrisdb.zd_user
  </select>

    <select id="getUserById" parameterType="int" resultType="com.chris.pojo.User">
        select * from chrisdb.zd_user where id = #{id}
    </select>

    <insert id="addUser" parameterType="com.chris.pojo.User">
        insert into chrisdb.zd_user(id,name)  values(#{id},#{name})
    </insert>

    <update id="updateUser" parameterType="com.chris.pojo.User">
        update chrisdb.zd_user set name = #{name} where id = #{id}
    </update>

    <delete id="delUser" parameterType="int" >
        DELETE from chrisdb.zd_user where id = #{id}
    </delete>
</mapper>

1.02、连接mysql的配置文件

存放目录 \src\main\resources

<?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/chrisdb?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value=""/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/chris/dao/UserMapper.xml"/>
    </mappers>
</configuration>

1.03、测试单元

package com.chris.dao;

import com.chris.pojo.User;
import com.chris.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class UserDaoTest {
    @Test
    public void test() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> listUser = mapper.getListUser();
        for (User user : listUser) {
            System.out.println(user);
        }
    }

    /***
     * 根据ID查询一个
     */
    @Test
    public void test022() {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtils.getSqlSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            User userById = mapper.getUserById(1);
            System.out.println(userById);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }

    @Test
    public void test023() {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtils.getSqlSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            User users = new User(5, "赵六");
            int i = mapper.addUser(users);
            sqlSession.commit();
            System.out.println(i);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }

    @Test
    public void test03() {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtils.getSqlSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            User users = new User(5, "赵六");
            int i = mapper.updateUser(users);
            sqlSession.commit();
            System.out.println(i);
        } finally {
            sqlSession.close();
        }
    }

    @Test
    public void test04() {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtils.getSqlSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            int i = mapper.delUser(5);
            sqlSession.commit();
            System.out.println(i);
        } catch (Exception exception) {
            System.out.println(exception.getMessage());

        } finally {
            sqlSession.close();

        }
    }


}
posted @ 2022-03-29 21:10  Chris丶Woo  阅读(19)  评论(0编辑  收藏  举报