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

本篇文章只是为了快速创建一个Mybatis实现查的项目,并没有对知识点进行讲。我个人认为学习框架先是动手实现一下,先不用管为什么要这么做,等运行成功后再去琢磨比较容易懂和记得牢。

IDEA创建MyBatis项目--实现简单的查操作_第1张图片

IDEA创建MyBatis项目--实现简单的查操作_第2张图片

通过maven加载Mybatis依赖包

IDEA创建MyBatis项目--实现简单的查操作_第3张图片

 
        
            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
        
    

新建config.properties配置文件

IDEA创建MyBatis项目--实现简单的查操作_第4张图片

IDEA创建MyBatis项目--实现简单的查操作_第5张图片

新建Mybatis配置文件




    
    

        
    
    
        
            
            
                
                
                
                
            
        
    
    
        
    


IDEA创建MyBatis项目--实现简单的查操作_第6张图片

编写SqlSessionFactoryUtils工具类

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();
    }


}

IDEA创建MyBatis项目--实现简单的查操作_第7张图片

编写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 +
                '}';
    }
}

IDEA创建MyBatis项目--实现简单的查操作_第8张图片

编写StudentMapper接口

package com.mapper;

import com.entity.Student;

public interface StudentMapper {
    public Student getStudent(long id);
}

IDEA创建MyBatis项目--实现简单的查操作_第9张图片

 

编写StudentMapper.xml映射器





    

IDEA创建MyBatis项目--实现简单的查操作_第10张图片

数据库表结构

IDEA创建MyBatis项目--实现简单的查操作_第11张图片

编写Main类

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());
    }
}

IDEA创建MyBatis项目--实现简单的查操作_第12张图片

IDEA创建MyBatis项目--实现简单的查操作_第13张图片

注意的点

  1. Mapper.xml的namespace与Mapper.java类的包名要一致
  2. Mapper.xml中select元素的id属性值必须跟Mapper.java中方法名要一致

报错

出现这个错误,可以看一下报错的内容:Could not find resource com/xml/StudentMapper.xml。一个很不好的习惯,把xml放在了java/com/xml包下,java包目录下就应该放java代码,放xml实在不应该。把xml文件放在resources目录下,便解决这个问题。实际就是查找resources/mapper目录的文件。

IDEA创建MyBatis项目--实现简单的查操作_第14张图片

"C:\Program Files\Java\jdk1.8.0_131\bin\java.exe" "-javaagent:E:\IntelliJ IDEA 2018.1.6\lib\idea_rt.jar=59220:E:\IntelliJ IDEA 2018.1.6\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_131\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar;C:\Users\林颖\Desktop\conmybatis\target\classes;C:\Users\林颖\.m2\repository\org\mybatis\mybatis\3.3.0\mybatis-3.3.0.jar;C:\Users\林颖\.m2\repository\mysql\mysql-connector-java\5.1.29\mysql-connector-java-5.1.29.jar;C:\Users\林颖\.m2\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;C:\Users\林颖\.m2\repository\org\slf4j\slf4j-api\1.7.12\slf4j-api-1.7.12.jar;C:\Users\林颖\.m2\repository\org\slf4j\slf4j-log4j12\1.7.12\slf4j-log4j12-1.7.12.jar" com.mian
log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in com/xml/StudentMapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/xml/StudentMapper.xml
	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
	at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:82)
	at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:66)
	at com.Utils.SqlSessionFactoryUtils.getSqlSessionFactory(SqlSessionFactoryUtils.java:29)
	at com.Utils.SqlSessionFactoryUtils.openSqlSession(SqlSessionFactoryUtils.java:41)
	at com.mian.main(mian.java:11)
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/xml/StudentMapper.xml
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:115)
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:95)
	at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
	... 4 more
Caused by: java.io.IOException: Could not find resource com/xml/StudentMapper.xml
	at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:114)
	at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:100)
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:338)
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:113)
	... 6 more

进程完成,退出码 1

修改之前:

IDEA创建MyBatis项目--实现简单的查操作_第15张图片

 

 

 

 


Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: Cannot find class: ${database.driver}

遇到这种问题肯定是config.properties跟 value="${database.xx}"两个文件其中一个写错了。

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