maven安装 mybatis遇到的问题

最近公司要我讲解下数据库优化,于是顺便搭建下mybatis框架。下面记录一下安装的过程。以及遇到的一些问题。

上面为maven的一些配置和安装方式

1、引入mybatis包 和mysql数据库的包

    <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.7</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.30</version>
        </dependency>

2、配置mybatis文件

mybatis-config.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <!-- 导入指定路径配置文件或类路径下配置文件 -->
    <properties resource="jdbc.properties" />


    <typeAliases>
        <!--你的bean文件,在mapper文件中有引用 -->
        <typeAlias type="com.zt.mysql.entity.Student" alias="Student" />
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <!-- 指定事务管理类型JDBC(使用JDBC的事务管理机制)或MANAGED(使用MANAGED的事务管理机制, 这种机制MyBatis自身不会去实现事务管理,而是让程序的容器如(JBOSS,Weblogic)来实现对事务的管理) -->
            <transactionManager type="JDBC" />
            <!-- 指定数据源连接配置 数据源类型POOLED、UNPOOLED、JNDI -->
            <!-- POOLED :Mybatis实现的简单的数据库连接池类型,它使得数据库连接可被复用,不必在每次请求时都去创建一个物理的连接。 -->
            <!-- UNPOOLED :这个数据源的实现是每次被请求时简单打开和关闭连接 -->
            <!-- JNDI :通过JNDI从web容器里获取数据源 -->
            <dataSource type="POOLED">
                <property name="username" value="${username}" />
                <property name="password" value="${password}" />
                <property name="driver" value="${driver}" />
                <property name="url" value="${url}" />
            </dataSource>
        </environment>
    </environments>

<!--  
    <mappers>
        引用你的empmapper.xml文件 
        <mapper resource="mybatis.xml" />
    </mappers>
    -->
</configuration>

jdbc.properties文件

##
username=root
password=123456
driver=jdbc:mysql://localhost:3306/test
url=com.mysql.jdbc.Driver

3、进行测试,是否能获取SqlSessionFactory对象。

  读取配置文件

1、方式一

InputStream in = this.getClass().getClassLoader().getResourceAsStream("mybatis-config.xml");

SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);

SqlSession session = sqlSessionFactory.openSession();

2、方式二

/**
 * 
 */
package com.zt.mysql.service;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

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 com.zt.mysql.entity.Student;

/**
 * @author yanlei
 * @date 2018-7-17
 * @Description :
 */

public class GetSqlSessionFactory {

	private static SqlSessionFactory sqlSessionFactory = null;

	private static GetSqlSessionFactory getSqlSessionFactory = null;

	private GetSqlSessionFactory() {
		Reader reader = null;
		try {
			reader = Resources.getResourceAsReader("mybatis-config.xml");
		} catch (IOException e) {
			e.printStackTrace();
		}
		/** 先获得SqlSessionFactory 会话工厂对象 */
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
//		InputStream in = this.getClass().getClassLoader().getResourceAsStream("mybatis-config.xml");
//		sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
		// 注解方式查询时需要注册mapper
		SqlSession session = sqlSessionFactory.openSession();

		System.out.println(session);
		sqlSessionFactory.getConfiguration().addMapper(Student.class);
	}

	public static GetSqlSessionFactory getInstance() {
		if (getSqlSessionFactory == null)
			getSqlSessionFactory = new GetSqlSessionFactory();
		return getSqlSessionFactory;
	}

	public static SqlSessionFactory getSqlSessionFactory() {
		return sqlSessionFactory;
	}

	public static void main(String[] args) {
		GetSqlSessionFactory sqlSessionFactory = getInstance();
		System.out.println(sqlSessionFactory);
	}

}

下面着重说一下遇到的一些问题

1、最开始我配置文件直接从网上拷贝下来,所以多了一个mapper映射文件。另外我还有一个Student mapper.xml文件放在xml文件路径下。所以我进行读取配置文件时,总是报错。 报下面的错误。

 Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource mybatis.xml

看到这个报错,我一直纠结是我的配置文件写路径写错了吗,百思不得其解。

于是我觉得可能时配置文件读取不到,导致的错误。我研究了一下maven项目怎么读取配置文件,发现网上都是这样读取文件的,于是断定文件读取没有问题。

记录下二种读取文件的方式。

1、Resource.getResourceAsReader("")

2、this.getCLass().getClassLoader().getResourceAsStream("");

最后经过仔细思考,发现肯可能原因出在mapper。mapper出现了问题,不能放配置文件里面。

猜你喜欢

转载自blog.csdn.net/qq_38816854/article/details/81076171