Use annotations at the DAO layer

If annotations are used in the DAO, the SQLMapConfig.xml configuration should specify class, not Resources

<mappers>
    <mapper class="com.jserm.dao.UserDao"></mapper>
</mappers>// Instead of resources<mappers>
    <mapper resource="com/jserm/dao/UserDao.xml"></mapper>
</mappers>
Copy the code

In addition, if annotations are used, they can be deleted without creating the corresponding XML configuration in the Resources directory

You can also write your own implementation classes

If you don’t want to use mybatis encapsulated functionality, you can also write your own implementation class, but it is not necessary

  • Write your own implementation class
package com.jserm.dao.impl;

import com.jserm.dao.UserDao;
import com.jserm.domain.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class UserDaoImpl implements UserDao {
    private SqlSessionFactory factory;

    public UserDaoImpl(SqlSessionFactory factory) {
        this.factory = factory;
    }

    @Override
    public List<User> findAll(a) {
        // 1. Use the factory to create the SQLSession object
        SqlSession session = factory.openSession();
        // 2. Use session to execute the query and find the findAll method under com.jserm.dao.userdao through the namespace of Userdao.xml
        List<User> users = session.selectList("com.jserm.dao.UserDao.findAll");
        session.close();
        // 3. Return the query result
        returnusers; }}// Execute in the test class
package com.jserm.test;

import com.jserm.dao.UserDao;
import com.jserm.dao.impl.UserDaoImpl;
import com.jserm.domain.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.InputStream;
import java.util.List;

/ * * *@author zhouwen
 * @createDate2021/5/20 13:17 * Introduction to Mybatis */
public class MybatisTest {
    public static void main(String[] args) throws Exception {
        // 1. Read the configuration file
        InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
        // create SqlSessionFactory
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(in);
        // 3. Use proxy objects to execute methods
        UserDaoImpl userDao = new UserDaoImpl(factory);
        // 4. Execute methods using proxy objects
        List<User> users = userDao.findAll();
        for(User user : users) { System.out.println(user); } in.close(); }}Copy the code
  • Use mybatis functions directly
package com.jserm.dao.impl;

import com.jserm.dao.UserDao;
import com.jserm.domain.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public List<User> findAll(a) {}}// Directly used in the test class
package com.jserm.test;

import com.jserm.dao.UserDao;
import com.jserm.domain.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.InputStream;
import java.util.List;

/ * * *@author zhouwen
 * @createDate2021/5/20 13:17 * Introduction to Mybatis */
public class MybatisTest {
    public static void main(String[] args) throws Exception {
        // 1. Read the configuration file
        InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
        // create SqlSessionFactory
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(in);
        // 3. Use the factory to produce sqlSession objects
        SqlSession session = factory.openSession();
        // 4. Use SQLSession to create a proxy object for the DAO interface
        UserDao userDao = session.getMapper(UserDao.class);
        // 5. Use proxy objects to execute methods
        List<User> users = userDao.findAll();
        for (User user : users) {
            System.out.println(user);
        }
        // 6. Release resourcessession.close(); in.close(); }}Copy the code