mybatis總體流程

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 
 
 
<configuration>
    
    <!-- <properties>
        <property name="jdbc.driver" value="com.mysql.jdbc.Driver"/>
    </properties> -->
    
    <!-- 
        <properties>:設置或引入資源文件
        resource:在類路徑下訪問資源文件
        url:在網絡路徑或磁盤路徑下訪問資源文件
     -->
    <properties resource="jdbc.properties"></properties>
    
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    
    <typeAliases>
        <!-- 
            爲類型設置類型別名
            type:Java 類型,若只設置type,默認的別名就是類型,且不區分大小寫
         -->
        <!-- <typeAlias type="com.atguigu.bean.User" alias="u"/> 不建議-->
        <!-- 這種寫法在XxxMapper.xml的resultType只須要寫個Xxx就能夠 -->
        <package name="com.atguigu.bean"/>
    </typeAliases>
    
    
    <!-- 
        <environments>:設置鏈接數據庫的環境
        default:設置默認使用的數據庫環境
     -->
    <environments default="mysql">
        <!-- 
            <environment>:設置某個具體的數據庫的環境
            id:數據庫環境的惟一標示
         -->
        <environment id="mysql">
            <!-- type="JDBC|MANAGED" -->
            <transactionManager type="JDBC" />
            <!-- type="POOLED|UNPOOLED|JNDI" -->
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}" />
                <property name="url" value="${jdbc.url}" />
                <property name="username" value="${jdbc.username}" />
                <property name="password" value="${jdbc.password}" />
            </dataSource>
        </environment>
        
        <environment id="oracle">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/ssm" />
                <property name="username" value="root" />
                <property name="password" value="123456" />
            </dataSource>
        </environment>
    </environments>
    <!-- 引入映射文件 -->
    <mappers>
    <!-- 引入具體的映射文件 -->
        <mapper resource="UserMapper.xml" />
    </mappers>
</configuration>

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 
 <!-- 這個用來指定對應的dao接口 -->
<mapper namespace="com.atguigu.mapper.UserMapper">
    
    <!-- 
        <select>:定義查詢語句
        id:設置SQL語句的惟一標示
        resultType:結果類型,即實體類的全限定名
        resultType若是在sqlMapConfig.xml中配置過typeAlisas,則按起的別名來,不建議
     -->
    <select id="getUserByUid" resultType="com.atguigu.bean.User">
        select uid,username,password,age,sex from user where uid = ${value}
    </select>
    
</mapper>

 

package com.atguigu.mapper;

import com.atguigu.bean.User;

public interface UserMapper {
    //和UserMapper.xml中的sql的id對應的
    /**
     * <!-- 
        <select>:定義查詢語句
        id:設置SQL語句的惟一標示
        resultType:結果類型,即實體類的全限定名
     -->
    <select id="getUserByUid" resultType="com.atguigu.bean.User">
        select uid,username,password,age,sex from user where uid = ${value} 或者#{id}
    </select>
    
     * @param uid
     * @return
     */
    User getUserByUid(String uid);
    
}

 

package com.atguigu.mapper;

import static org.junit.Assert.*;

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

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 com.atguigu.bean.User;

public class TestMybatis {

    @Test
    public void test() throws IOException {
        
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");//org.apache.ibatis.io.Resources
        //InputStream is = TestMybatis.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = 
                new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //getMapper():會經過動態代理動態生成UserMapper的代理實現類
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        System.out.println(mapper.getClass().getName());//com.sun.proxy.$Proxy4這個爲生成的代理對象
        User user = mapper.getUserByUid("1");
        System.out.println(user);
        //user_name ---> userName
    }
    
}

相關文章
相關標籤/搜索