【MyBatis03】设置配置文件模板

文章目录

    • 1.设置mybatis-config.xml配置文件模版
    • 2.创建映射文件的模板

1.设置mybatis-config.xml配置文件模版

新建一个xml文件,设置模板的内容
【MyBatis03】设置配置文件模板_第1张图片

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:13306/mybatis
jdbc.username=root
jdbc.password=root

DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    
    
    <properties resource="jdbc.properties">properties>
    
    <typeAliases >
        
        
        <package name="com.sdjzu.mybatis.pojo"/>
    typeAliases>
    
    
    <environments default="development">
        
        <environment id="development">
            
            <transactionManager type="JDBC"/>
            
            <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>

    environments>

    
    <mappers>
        
        
        <package name="com.sdjzu.mybatis.mapper"/>
    mappers>
configuration>

【MyBatis03】设置配置文件模板_第2张图片
设置模板的内容
【MyBatis03】设置配置文件模板_第3张图片
【MyBatis03】设置配置文件模板_第4张图片

2.创建映射文件的模板


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

mapper>

【MyBatis03】设置配置文件模板_第5张图片
【MyBatis03】设置配置文件模板_第6张图片
封装SqlSessionUtils工具类

package com.sdjzu.mybatis.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 SqlSessionUtils {
    public static SqlSession getSqlSession(){
        SqlSession sqlSession=null;
        try {
            InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            sqlSession = sqlSessionFactory.openSession(true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sqlSession;
    }


}

测试

package com.sdjzu.mybatis.test;

import com.sdjzu.mybatis.mapper.ParameterMapper;
import com.sdjzu.mybatis.pojo.User;
import com.sdjzu.mybatis.utils.SqlSessionUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class ParameterMapperTest {
    @Test
    public void testGetAllUser(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
        List<User> allUser = mapper.getAllUser();
        /*用强for循环*/
        for (User user:allUser){
            System.out.println(user);
        }


    }
}

测试结果

DEBUG 04-03 23:03:17,282 ==>  Preparing: select * from t_user (BaseJdbcLogger.java:137) 
DEBUG 04-03 23:03:17,313 ==> Parameters:  (BaseJdbcLogger.java:137) 
DEBUG 04-03 23:03:17,313 <==      Total: 8 (BaseJdbcLogger.java:137) 
User{id=1, username='jay', password='123456', age=23, sex='?', email='[email protected]'}
User{id=3, username='admin', password='123456', age=23, sex='?', email='[email protected]'}
User{id=4, username='admin', password='123456', age=23, sex='?', email='[email protected]'}
User{id=5, username='admin', password='123456', age=23, sex='?', email='[email protected]'}
User{id=6, username='admin', password='123456', age=23, sex='?', email='[email protected]'}
User{id=7, username='admin', password='123', age=23, sex='?', email='[email protected]'}
User{id=8, username='admin', password='123456', age=23, sex='?', email='[email protected]'}
User{id=9, username='admin', password='123456', age=23, sex='?', email='[email protected]'}

你可能感兴趣的:(Mybatis,java)