mybatis——配置文件中mapper的namespace绑定接口

在mybatis中,映射文件中的namespace是用于绑定Dao接口的,即面向接口编程。

当你的namespace绑定接口后,你可以不用写接口实现类,mybatis会通过该绑定自动帮你找到对应要执行的SQL语句

以下为项目图和数据库表

mybatis——配置文件中mapper的namespace绑定接口_第1张图片    mybatis——配置文件中mapper的namespace绑定接口_第2张图片

mybatis-config




    
        
        
    
    
        
        
    
    
    
        
            
            
            
            
                
                
                
                
            
        
    
    
        
    


mapper中namespace绑定接口




    
        insert into
        users(name,password)
        values(#{name},#{password})
    

    
        insert into
        users(name,password)
        values(#{name},#{password})
        
            SELECT  LAST_INSERT_ID()
        
    

    

    
        delete from
        users where id=#{id}
    

    

    
        update users
        set
        name=#{name},password=#{password}
        where id=#{id}
    


log4j.properties

#全局配置
log4j.rootLogger=ERROR,stdout

#MyBatis日志配置
log4j.logger.com.etc.dao=TRACE

#控制台输出配置
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

UserDao

package com.etc.dao;

import com.etc.model.User;

import java.util.List;

public interface UserDao {

    List queryAll();

    int insert(User user);
  
}

BaseMapper

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.Before;

import java.io.IOException;
import java.io.InputStream;

public class BaseMapper {

    private  SqlSessionFactory sqlSessionFactory;

    @Before
    public  void init() throws IOException {
        InputStream inputStream = null;
        try {
            String resource = "mybatis-config.xml";
            inputStream = Resources.getResourceAsStream(resource);
            this.sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (inputStream!=null){
                inputStream.close();
            }
        }
    }

    public SqlSession getSqlSession(){
        return this.sqlSessionFactory.openSession();
    }
}

UserTest

import com.etc.model.User;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class UserTest extends BaseMapper {


    @Test
    public void testSelectAll(){
        try {
            SqlSession sqlSession = this.getSqlSession();
            List list = sqlSession.selectList("queryAll");
            sqlSession.close();
            for (User user : list){
                System.out.println(user.getName()+"__"+user.getPassword());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Test
    public void testInsert(){
        try {
            User user = new User();
            user.setName("龙宫");
            user.setPassword("456");
            SqlSession sqlSession  = this.getSqlSession();
            sqlSession.insert("insert",user);
            sqlSession.commit();
            sqlSession.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

效果图

mybatis——配置文件中mapper的namespace绑定接口_第3张图片

你可能感兴趣的:(mybatis,IDEA)