Mybatis-Spring

整合Mybatis

步骤

1.导入相关jar包

  • junit
  • mybatis
  • mysql数据库
  • spring相关的
  • aop织入
  • mybatis-spring【new】


    
        Spring-study
        org.example
        1.0-SNAPSHOT
    
    4.0.0

    Spring-10-mybatis


    
        
        
            junit
            junit
            4.12
            test
        
        
        
            mysql
            mysql-connector-java
            5.1.47
        
        
        
            org.mybatis
            mybatis
            3.4.6
        

        
        
            org.springframework
            spring-webmvc
            5.1.9.RELEASE
        

        
        
            org.springframework
            spring-jdbc
            5.1.9.RELEASE
        
        
            org.aspectj
            aspectjweaver
            1.8.13
        
        
        
            org.aspectj
            aspectjweaver
            1.8.13
        
        
        
        
            org.mybatis
            mybatis-spring
            2.0.6
        

        
        
        
            org.projectlombok
            lombok
            1.18.12
            provided
        

        
        
            log4j
            log4j
            1.2.17
        
    
    
    
        
            
                src/main/java
                
                    **/*.properties
                    **/*.xml
                
                false
            
            
                src/main/resources
                
                    **/*.properties
                    **/*.xml
                
                false
            
        
    

2.编写配置文件

3.测试

回忆mybatis

  1. 编写实体类     
  2. 编写核心配置文件
  3. 编写接口
  4. 编写Mapper.xml
  5. 测试

JDBC常用编写Mapper.xml的代码




    
        
        
    

    
    
    
    

Mybatis-Spring

准备工作:①创建接口UserMapper

import com.pojo.User;

import java.util.List;

public interface UserMapper {
    public List selectUser();
}

②UserMapper.xml【该文件必须和UserMapper接口在同一个包下】




    

③mybatis-config.xml(注意点:该文件会被绑定到spring-dao.xml)





    
        
        
        
        
    

    
    
        
        
        
        
        
    
    
    
    
        
        
        
    




同时可以创建一个applicationContext.xml用来将spring-dao.xml中的内容进行整合。




    
 
  



    
    
    
    
    

其次创建一个com.dao在其包中创建UserMapperImpl类【方式一】

(该类需继承UserMapper)

package com.dao;

import com.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

public class UserMapperImpl implements UserMapper{
    //我们的所有操作,都使用SqlSession来执行,在原来,现在都使用SqlSessionTemplate;
    private SqlSessionTemplate sqlSession;

    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }

    public List selectUser() {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        return mapper.selectUser();
    }
}

将UserMapperImpl中的内容导入applicationContext.xml


    

测试类:

public class MyTest {
    @Test
    public void TestMy() throws IOException {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapper mapper = context.getBean("userMapper", UserMapper.class);
        for (User user : mapper.selectUser()) {
            System.out.println(user);
        }
    }
}

其次创建一个com.dao在其包中创建UserMapperImpl2类【方式二】

public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {
    public List selectUser() {
        /*建议精简一行*/
        /*SqlSession sqlSession = getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        return mapper.selectUser();*/
        return getSqlSession().getMapper(UserMapper.class).selectUser();
        /*
        相当于
            private SqlSessionTemplate sqlSession;
            public void setSqlSession(SqlSessionTemplate sqlSession) { this.sqlSession = sqlSession;}
            注入操作
            */
    }
}

将UserMapperImpl中的内容导入applicationContext.xml


    

spring-dao.xml改动




    
    
        
        
        
        
    

    
    
        
        
        
        
        
    
    




测试类:

package com.dao;

import com.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 org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

public class MyTest {
    @Test
    public void TestMy() throws IOException {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapper mapper = context.getBean("userMapper2", UserMapper.class);
        for (User user : mapper.selectUser()) {
            System.out.println(user);
        }
    }
}

你可能感兴趣的:(spring,mysql,maven)