记录MyBatis与Spring整合过程

将MyBatis的SqlSessionFactory、数据源、Mapper的创建过程交给Spring去进行管理。

PART_ONE:环境配置
1.MyBatis环境jar包
2.Spring环境jar包
3.MyBatis和Spring整合的jar包


image.png

4.项目文件结构


image.png

5.编写Spring的核心配置文件ApplicationContext.xml

        
    
    

    
    
        
        
        
        
    
    
    
    
        
        
        
        
         
    
    
        
    
    
    
    
    
    
    
        
        
        
        
             

6.Mybatis的核心配置文件SqlMapConfig.xml




                
    
    
        
        
        
        
        
        
        
        
        
        
    
    

PART_TWO:dao方式的实现

image.png

1.将dao实现类中的sqlSessionFactory交给Spring创建并且配置dao实现类的bean,使用时通过spring进行实例化。

2.UserDao.java

package dao;

import entiy.User;

public interface UserDao {
    
    public User selectUserById(int id);

}

3.UserDaoImpl.java

package dao;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import entiy.User;

public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
    
    @Override
    public User selectUserById(int id) {
        SqlSession sqlsession = this.getSqlSession();
        User user = sqlsession.selectOne("daomap.findUserById", id);
        return user;
    }
}

4.测试类

package test;

import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import dao.UserDao;
import entiy.User;

public class daoTest {
    
    @Test
    public void testDao() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
        UserDao userdao = (UserDao) applicationContext.getBean("userDao");
        User test = userdao.selectUserById(2);
        System.out.println(test);
    }

}

PART_THREE:Mapper方式的实现

image.png

1.Spring根据mapper接口生成代理

2.UserMapper.xml








    
        
        
    
    
    
    
    
    
    
        
        SELECT LAST_INSERT_ID()
        
        
        
        insert into t_user (name,address) values (#{name},#{address})
    
    
    
    
        delete from t_user where id =#{id}
    
    
    
    
        update t_user set name=#{name},address=#{address} where id=#{id}
    
    
    
    
    
    
    
        
        
    
    
    
    

3.UserMapper.java

package Mapper;

import java.util.List;

import entiy.User;
import entiy.UserCustom;
import entiy.UserQueryVo;

public interface UserMapper {
    
    public User findUserById(int id);
    
    public List findUserByName(String name);
    
    public void addUser(User user);
    
    public void deleteUserById(int id);
    
    public List findUserList(UserQueryVo userQueryVo);
    
    public List findUserResultMap(int id);

}

4.测试类

package test;

import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import Mapper.UserMapper;
import entiy.User;

public class mapperTest {
    
    @Test
    public void testMap() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
        UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");
        User test = userMapper.findUserById(3);
        System.out.println(test);
    }
}

注意:如果在applicationContext.xml中使用mapper的批量处理,遵循规范:将mapper.java和mapper.xml映射文件名称保持一致,且在一个目录中自动扫描出来的mapper的bean的id为mapper类名(首字母小写)。而且在SqlMapConfig.xml中不用配置UserMapper.xml如
image.png

你可能感兴趣的:(记录MyBatis与Spring整合过程)