IDEA创建MyBatis项目--实现简单的查操作


IDEA创建MyBatis项目--实现简单的查操作

1.创建一个maven工程,不使用模板


2.通过maven加载Mybatis依赖包


在pom文件中导入maven坐标


        
            org.mybatis
            mybatis
            3.3.0
        
        
            mysql
            mysql-connector-java
            5.1.29
        
        
            junit
            junit
            4.11
            test
        
        
            log4j
            log4j
            1.2.17
        
        
            org.slf4j
            slf4j-api
            1.7.12
        
    

3.新建config.properties配置文件


填写内容

4.新建Mybatis配置文件

在以下位置创建

命名为mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>


    
    
 
        
    
    
        
            
            
                
                
                
                
            
        
    
    
        
    
 


5.编写SqlSessionFactoryUtils工具类

在以下位置创建

没有的包右击创建package

package com.Utils;
 
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.io.Resources;
 
import java.io.IOException;
import java.io.InputStream;
 
public class SqlSessionFactoryUtils {
 
    private final static Class lock=SqlSessionFactoryUtils.class;
 
    private static SqlSessionFactory sqlSessionFactory=null;
 
    private SqlSessionFactoryUtils(){};
 
    public static SqlSessionFactory getSqlSessionFactory(){
        synchronized (lock){
            if (sqlSessionFactory!=null){
                return sqlSessionFactory;
            }
 
            String resource="mybatis-config.xml";
            InputStream inputStream;
            try {
                inputStream=Resources.getResourceAsStream(resource);
                sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream);
            }catch (IOException e){
                e.printStackTrace();
                return null;
            }
 
            return sqlSessionFactory;
        }
    }
 
    public static SqlSession openSqlSession(){
        if (sqlSessionFactory==null){
            getSqlSessionFactory();
        }
        return  sqlSessionFactory.openSession();
    }
 
 
}

6.编写Student实体类


package com.entity;
 
public class Student {
 
    private int id;
    private String name;
    private int age;
 
    public int getId() {
        return id;
    }
 
    public void setId(int id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
 
    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

7.编写StudentMapper接口

package com.mapper;
 
import com.entity.Student;
 
public interface StudentMapper {
    public Student getStudent(long id);
}

8.编写StudentMapper.xml映射器

<?xml version="1.0" encoding="utf-8" ?>


 
    
 

9.与数据库表结构对应

10.编写Main类(或测试类test)

package com;
 
import com.entity.Student;
import com.mapper.StudentMapper;
import com.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
 
public class Main {
 
    public static void main(String[] args){
        SqlSession sqlSession=SqlSessionFactoryUtils.openSqlSession();
        StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
        System.out.println(studentMapper.getStudent(1).toString());
    }
}

注意点

1.Mapper.xml的namespace与Mapper.java类的包名要一致
2.Mapper.xml中select元素的id属性值必须跟Mapper.java中方法名要一致
3.把xml文件放在resources目录下