MyBatis入门(idea环境)

以查询员工信息为例入门mybatis!

一、使用idea创建maven项目:

创建好后,项目结构如下


image.png

二、配置pom.xml

主要配置resources目录、mysql-connector、mybatis



    4.0.0

    com.mybatis.example
    Mybatis
    1.0-SNAPSHOT
    
        
            
            
                src/main/java
                
                    **/**
                
            
        
    
    
        
        
            mysql
            mysql-connector-java
            5.1.47
        

        
        
            org.mybatis
            mybatis
            3.4.6
        

    

注意:在eclipse中直接new Source Folder就行,但是在idea中需要配置resource的目录


image.png

三、新建包名,以及员工实体类

image.png

public class Employee {
    private Integer id;
    private String lastName;
    private Integer gender;
    private String email;

    public Employee() {
    }

    // getter and setter ...

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", lastName='" + lastName + '\'' +
                ", gender=" + gender +
                ", email='" + email + '\'' +
                '}';
    }
}

四、在resource目录下新建:

mybatsis-config.xml 和 EmployeeMapper配置文件

image.png

mybatis-config.xml:




    
        
            
            
                
                
                
                
            
        
    
    
        

    

EmployeeMapper.xml:




    

五、编写main方法测试

package com.mybatis.example;

import com.mybatis.example.bean.Employee;
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 Main {

    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        Employee employee = session.selectOne("com.mybatis.example.EmployeeMapper.selectEmployee", 1);
        System.out.println(employee);
        session.close();
    }
}

输出结果:

Employee{id=1, lastName='null', gender=1, email='[email protected]'}

lastName='null' 这个问题等下再说。

六、以上就是以Mapper.xml配置文件的方式,访问数据库。

若不想使用EmployeeMapper.xml文件,则可以使用下面的方式:

1:更改mybatis-config.xml文件中的mappers节点为:
 

        
    
2.新建EmployeeMapper接口
image.png
package com.mybatis.example;

import com.mybatis.example.bean.Employee;
import org.apache.ibatis.annotations.Select;

public interface EmployeeMappger {

    @Select("select * from employee where id = #{id}")
    Employee selectEmployee(int id);

}

3.修改main方法
    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
//        Employee employee = session.selectOne("com.mybatis.example.EmployeeMapper.selectEmployee", 1);
        //先获取mapper对象,调用mapper对象的selectEmployee方法
        EmployeeMappger mapper = session.getMapper(EmployeeMappger.class);
        Employee employee = mapper.selectEmployee(1);
        System.out.println(employee);
        session.close();
    }

打印结果:

Employee{id=1, lastName='null', gender=1, email='[email protected]'}

七、下面解决刚才遇到的问题,lastName=null

由于数据库中的字段为last_name而实体类中的字段为lastName,所以未能匹配上。
解决方法:修改EmployeeMapper或EmployeeMapper.xml中的sql语句为:

"select id, last_name lastName, gender, email from employee where id = #{id}"
或
"select id, last_name as lastName, gender, email from employee where id = #{id}"
as可以省略

即可。
即给last_name起个别名为lastName。

注意!!!!之前没有在pom.xml中配置
 
        
            
            
                src/main/java
                
                    **/**
                
            
        
    

导致找不到mybatis-config.xml,等很多问题,耽误了不少时间。

你可能感兴趣的:(MyBatis入门(idea环境))