Mybatis基础 -- 相关概念及快速入门

2021-01-24  本文已影响0人  Travis_Wu

一、Mybatis相关概念

二、Mybatis基本应用

三、注意事项

执行方法SqlSession sqlSession = sqlSessionFactory.openSession();的时候,会默认开启一个事务,但是不会自动提交该事务,在执行增删改操作的时候,需要手动提交事务sqlSession.commit();
如果不想手动提交事务,则可以使用SqlSession sqlSession = sqlSessionFactory.openSession(true);方法,该方法会自动提交事务。

四、Mybatis的Dao层传统开发方式

  1. 编写Dao层接口
package com.wujun.dao;

import com.wujun.pojo.User;

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

public interface IUserDao {

    List<User> findAll() throws IOException;
}
  1. 编写Dao层实现类
package com.wujun.dao;

import com.wujun.pojo.User;
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;
import java.util.List;

public class UserDaoImpl implements IUserDao {

    @Override
    public List<User> findAll() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> users = sqlSession.selectList("user.findAll");
        sqlSession.close();
        return users;
    }
}

  1. 编写测试类
package com.wujun.test;

import com.wujun.dao.IUserDao;
import com.wujun.dao.UserDaoImpl;
import com.wujun.pojo.User;
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 org.junit.Test;

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

public class MybatisTest {

    /*@Test
    public void test1() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> users = sqlSession.selectList("user.findAll");
        for (User user : users) {
            System.out.println(user);
        }
        sqlSession.close();
    }*/

    @Test
    public void test2() throws IOException {
        IUserDao userDao = new UserDaoImpl();
        List<User> users = userDao.findAll();
        for (User user : users) {
            System.out.println(user);
        }
    }
}

  1. 测试结果
User(id=1, username=吴俊1)
User(id=2, username=吴俊2)
User(id=3, username=吴俊3)

五、Mybatis的Dao层代理开发方式

public interface UserMapper {

    List<User> findAll();

    User findById(Long id);
}
<?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">

<!--namespace:名称空间,与id组成sql的唯一标识-->
<mapper namespace="com.wujun.mapper.UserMapper">
    <select id="findAll" resultType="com.wujun.pojo.User">
        select * from User
    </select>

    <select id="findById" parameterType="java.lang.Long" resultType="com.wujun.pojo.User">
        select * from User where id = #{id}
    </select>
</mapper>
package com.wujun.test;

import com.wujun.dao.IUserDao;
import com.wujun.dao.UserDaoImpl;
import com.wujun.mapper.UserMapper;
import com.wujun.pojo.User;
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 org.junit.Test;

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

public class MybatisTest {

    /*@Test
    public void test1() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> users = sqlSession.selectList("user.findAll");
        for (User user : users) {
            System.out.println(user);
        }
        sqlSession.close();
    }*/

    /*@Test
    public void test2() throws IOException {
        IUserDao userDao = new UserDaoImpl();
        List<User> users = userDao.findAll();
        for (User user : users) {
            System.out.println(user);
        }
    }*/

    @Test
    public void test3() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> all = mapper.findAll();
        for (User user : all) {
            System.out.println(user);
        }
        System.out.println("===============================");
        User byId = mapper.findById(1L);
        System.out.println(byId);
    }
}

User(id=1, username=吴俊1)
User(id=2, username=吴俊2)
User(id=3, username=吴俊3)
===============================
User(id=1, username=吴俊1)
上一篇 下一篇

猜你喜欢

热点阅读