Mybaits课堂练习


DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

    
    

    
    
    
    
    
    
    



DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

    
    
        
        
        
        
    
    
    
        
        
    
    
    
        
        <package name="cn.kgc.pojo">package>
    
    
    default="dev">
        
            
            
                
                
                
                
            
        
        
            
            
                
                
                
                
            
        
        
            
            
                
                
                
                
            
        
    
    
        
    
package cn.kgc.dao;

import cn.kgc.pojo.Provider;
import cn.kgc.pojo.User;

import java.util.List;

public interface UserMapper {
    public List getUserListByNullParam();
    public List getProviderList();
}
package cn.kgc.utils;

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;

public class MybatisUtils {
    private static SqlSessionFactory sqlSessionFactory;

    static {
        String path = "mybatis-config.xml";
        try {
            InputStream is = Resources.getResourceAsStream(path);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static SqlSession getSqlSession() {
        return sqlSessionFactory.openSession();
    }
}
package cn.kgc.test;

import cn.kgc.dao.UserMapper;
import cn.kgc.pojo.Provider;
import cn.kgc.pojo.User;
import cn.kgc.utils.MybatisUtils;
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 TestUserMapper {
    @Test
    public void test01(){
        //1.加载核心配置文件
        String path = "mybatis-config.xml";
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream(path);
            // 2.mybatis中的核心类
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            int result = sqlSession.selectOne("cn.kgc.dao.UserMapper.count");
            System.out.println(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //使用工具類
    @Test
    public void test02(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        List userList = sqlSession.selectList("cn.kgc.dao.UserMapper.userlist");
        for (User user : userList) {
            System.out.println("****************用戶名"+user.getUserName());
        }
    }
    //使用接口方式[以后就采用此种方式]
    @Test
    public void test03(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        List userList = sqlSession.getMapper(UserMapper.class).getUserListByNullParam();
        for (User user : userList) {
            System.out.println("****************用戶名"+user.getUserName());
        }
    }
    @Test
    public void test04(){
        String path="mybatis-config.xml";
        InputStream is=null;
        try {
            is= Resources.getResourceAsStream(path);
            SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is);
            SqlSession sqlSession=sqlSessionFactory.openSession();
            int result = sqlSession.selectOne("cn.kgc.dao.UserMapper.result");
            System.out.println("一共"+result+"条供应商记录。");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test05(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        List providerList = sqlSession.getMapper(UserMapper.class).getProviderList();
        for (Provider provider : providerList) {
            System.out.println("**供应商**"+provider.getProName()+"--"+provider.getProContact()+"--"+provider.getProPhone()+"--"+provider.getProDesc());
        }
    }
    @Test
    public void test06(){
        SqlSession sqlSession= MybatisUtils.getSqlSession();
        List providerList=sqlSession.selectList("cn.kgc.dao.UserMapper.providerlist");
        for (Provider provider:providerList){
            System.out.println("**供应商**"+provider.getProName()+"--"+provider.getProContact()+"--"+provider.getProPhone()+"--"+provider.getProDesc());
        }
    }



}

 

你可能感兴趣的:(Mybaits课堂练习)