Mybatis --02.mybatis核心配置文件mybatis-config.xml

说明:mybatis环境搭建步骤:

1、创建Maven工程 
2、导入坐标、pom.xml
3、编写必要代码、实体类和持久层接口(domain/Dao)
4、编写核心配置文件mybatis-config.xml
5、编写映射配置文件UserMapper.xml
6、编写测试类test/java/TestMybatis.java

一、Mybatis核心配置文件

1、environments

Mybatis-config.xml(名子随意取)




    
    
        
            
            
                 
                
                
                
                
                
                
                
            
        
    

    
        
    

说明:Mybatis可以配置成适应多种环境、开发、测试、和生产环境、尽管可以配置多个环境、

但每个SqlSessionFactory实列只能选择其一、可以有如下二种方式指定使用哪个环境

方式一:default

添加一个测试环境test、并在default参数中指向test环境

Mybatis-test-config.xml




    
    
        
            
            
                 
                
                
                
                
                
                
                
            
        
        
            
            
                
                
                
                
            
        
    
    
        
    

方式二:build方法

通过build的另一个重载方法来构建sqlSessionFactory

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

/**
 * @ Author     :ssw.
 * @ Date       :Created in 20:47 2018/11/4
 */
public class TestMyBatis {
    
    @Test
    public void test(){
        SqlSession sqlSession = null;
        try {
            //加载核心配置文件
            InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
            //创建工厂
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in,"test");
            //获取SqlSession
            sqlSession = sqlSessionFactory.openSession();
            //执行查询语句
            /*
            执行查询语句,selectOne参数解释:
            参数1:语句的唯一标识,由namespace+id组成
            参数2:语句需要的参数
             */
            User user = sqlSession.selectOne("UserMapper.findUserById", 1L);
            System.out.println("user = " + user);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (sqlSession != null){
                sqlSession.close();
            }
        }
    }
}

演示:

A、创建Maven工程--file-new--project--maven--ok

B、导入坐标pom.xml


        
        
            mysql
            mysql-connector-java
        
        
        
            junit
            junit
        
        
        
            org.mybatis
            mybatis
        
        
        
            org.slf4j
            slf4j-log4j12
        
    

C、编写实体类User.java

package com.sswblog.domain;

import java.io.Serializable;
import java.util.Date;

/**
 * @ Author     :ssw.
 * @ Date       :Created in 19:44 2018/11/4
 */
public class User implements Serializable {
    private static final long serialVersionUid = 1L;
    private Long id;
    private String userName;
    private String password;
    private String name;
    private Integer age;
    private Integer sex;
    private Date birthday;
    private Date created;
    private Date updated;

    public static long getSerialVersionUid() {
        return serialVersionUid;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public Date getUpdated() {
        return updated;
    }

    public void setUpdated(Date updated) {
        this.updated = updated;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                ", birthday=" + birthday +
                ", created=" + created +
                ", updated=" + updated +
                '}';
    }
}

数据库:

Mybatis --02.mybatis核心配置文件mybatis-config.xml_第1张图片

D、核心配置文件mybatis-config.xml




    
        
            
            
                
                
                
                
                
                
                
                
            
        

    
        
        
            
            
            
            
        
    


    
    
        
    

E、映射配置文件UserMappers.xml





    
    

F、编写测试类

package com.sswblog;

import com.sswblog.domain.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 java.io.InputStream;

/**
 * @ Author     :ssw.
 * @ Date       :Created in 20:47 2018/11/4
 */
public class TestMyBatis {

    @Test
    public void test(){
        SqlSession sqlSession = null;
        try {
            //加载核心配置文件
            InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
            //创建工厂
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in,"test");
            //获取SqlSession
            sqlSession = sqlSessionFactory.openSession();
            //执行查询语句
            /*
            执行查询语句,selectOne参数解释:
            参数1:语句的唯一标识,由namespace+id组成
            参数2:语句需要的参数
             */
            User user = sqlSession.selectOne("UserMapper.findUserById", 1L);
            System.out.println("user = " + user);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (sqlSession != null){
                sqlSession.close();
            }
        }
    }
}

2、读取外部属性资源文件

在src/main/resources下创建jdbc.properties

Mybatis --02.mybatis核心配置文件mybatis-config.xml_第2张图片

jdbc.properties

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=sswqzx

在mybatis-config.xml中引入jdbc.properties




     
   
    
    
        
            
            
                
                
                
                
                
                
                
                
            
        
    
    
        
    

 测试类

package com.sswblog;

import com.sswblog.domain.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 java.io.InputStream;

/**
 * @ Author     :ssw.
 * @ Date       :Created in 20:47 2018/11/4
 */
public class TestMyBatis {

    @Test
    public void test(){
        SqlSession sqlSession = null;
        try {
            //加载核心配置文件
            InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
            //创建工厂
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
            //获取SqlSession
            sqlSession = sqlSessionFactory.openSession();
            //执行查询语句
            /*
            执行查询语句,selectOne参数解释:
            参数1:语句的唯一标识,由namespace+id组成
            参数2:语句需要的参数
             */
            User user = sqlSession.selectOne("UserMapper.findUserById", 1L);
            System.out.println("user = " + user);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (sqlSession != null){
                sqlSession.close();
            }
        }
    }
}

 

3、settings设置

mybatis核心配置文件中settings中可以设置参数很多,今天咱们先学习驼峰匹配

经典数据库命名规则:如果多个单词之间,通常使用下划线进行连接。

Java中属性命名规则:驼峰式命名。

前面事例中、我们发现user_name列的值没有查询出来,就是因为默认情况下,不能完成user_name列到userName属性的映射。

方式一:需要在mybatis-config.xml中开启驼峰匹配:

mybatis-config.xml




    
   
    
        
        
    
    
    
        
            
            
                
                
                
                
                
                
                
                
            
        
    
    
        
    

UserMappers.xml





    
    

测试类:

package com.sswblog;

import com.sswblog.domain.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 java.io.InputStream;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 20:47 2018/11/4
 */
public class TestMyBatis {
    @Test
    public void test(){
        SqlSession sqlSession = null;
        try {
            //加载核心配置文件
            InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
            //创建工厂
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
            //获取SqlSession
            sqlSession = sqlSessionFactory.openSession();
            //执行查询语句
            /*
            执行查询语句,selectOne参数解释:
            参数1:语句的唯一标识,由namespace+id组成
            参数2:语句需要的参数
             */
            User user = sqlSession.selectOne("UserMapper.findUserById", 1L);
            System.out.println("user = " + user);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (sqlSession != null){
                sqlSession.close();
            }
        }
    }
}

方式二:在UserMapper.xml中给列加别名





    
    

4、typeAliases

typeAliases表示类型别名,是为 Java 类型取的一个短的名字,存在的意义仅在于用来减少类完全限定名的冗余。

方式一:typeAliase (在mybatis-config.xml中)




    
   
    
        
        
    
    
        
    
    
    
        
            
            
                
                
                
                
                
                
                
                
            
        
    
    
        
    

UserMappers.xml中使用别名:





    
    
    

缺点:每个实体类都要配置

方式二:package

扫描该包下所有类,别名就是类名,大小写不敏感,建议使用的时候和类名一致。

mybatis-config.xml




    
   
    
        
        
    
    
        
        
    
    
    
        
            
            
                
                
                
                
                
                
                
                
            
        
    
    
        
    

UserMappers.xml





    
    
    

内置的类型别名:

别名 映射的类型
_byte byte
_long long
_short short
_int int
_integer int
_double double
_float float
_boolean boolean
string String
byte Byte
long Long
short Short
int Integer
integer Integer
double Double
float Float
boolean Boolean
date Date
decimal BigDecimal
bigdecimal BigDecimal
object Object
map Map
hashmap HashMap
list List
arraylist ArrayList
collection Collection
iterator Iterator

在UserMappers.xml中增加一条根据id查询用户名的sql语句:

UserMappers.xml





    
    
    
    

在TestMyBatis中编写单元测试方法:

package com.sswblog;

import com.sswblog.domain.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 java.io.InputStream;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 20:47 2018/11/4
 */
public class TestMyBatis {
    @Test
    public void test(){
        SqlSession sqlSession = null;
        try {
            //加载核心配置文件
            InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
            //创建工厂
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
            //获取SqlSession
            sqlSession = sqlSessionFactory.openSession();
            //执行查询语句
            /*
            执行查询语句,selectOne参数解释:
            参数1:语句的唯一标识,由namespace+id组成
            参数2:语句需要的参数
             */
            User user = sqlSession.selectOne("UserMapper.findUserById", 1L);
            System.out.println("user = " + user);

            String userName = sqlSession.selectOne("UserMapper.findUserNameById", 1L);
            System.out.println(userName);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (sqlSession != null){
                sqlSession.close();
            }
        }
    }
}

Mybatis --02.mybatis核心配置文件mybatis-config.xml_第3张图片

5、mappers

mybatis核心配置文件Mappers标签是引入映射文件

Mybatis --02.mybatis核心配置文件mybatis-config.xml_第4张图片

 

你可能感兴趣的:(Mybatis,mybatis核心配置文件)