MyBatis连接Oracle数据库的例子

天大地大妈咪最大 提交于 2020-04-10 16:46:15

之前我曾写过一篇博客,提及了如何建立一个最简单的MyBatis连接Oracle数据库的Demo。

博客地址如下:http://my.oschina.net/Tsybius2014/blog/626206

在这篇博客中,我使用了XML配置MyBatis连接数据库相关的内容,并通过MyBatis提供的函数,直接通过XML文件配置的内容,通过SqlSessionFactory建立SqlSession。

本文将说明另一种建立SqlSession,还是使用 SqlSessionFactory ,但不通过XML配置文件建立,而是通过代码直接建立。

我的电脑操作系统版本为Win7旗舰版(ServicePack1),Oracle版本为Oracle11g

程序使用的jar包有:mybatis-3.2.2.jar、ojdbc14-10.2.0.2.0.jar

之前有配置文件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>
  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
        <property name="username" value="用户名"/>
        <property name="password" value="密码"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="BaseMapper.xml"/>
  </mappers>
</configuration>

接口BaseMapper.java定义如下:

import org.apache.ibatis.annotations.Select;

public interface BaseMapper {
    @Select("SELECT 1 FROM DUAL")
    int getTestData();
}

BaseMapper.xml定义如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="BaseMapper" /> 

最后将main函数写为:

import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**
 * MyBatis使用测试
 * @author Tsybius2014
 * @date 2016年5月22日
 * @time 下午10:39:11
 * @remark
 */
public class MyBatisTest {
    public static void main(String[] args) {
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session = sqlSessionFactory.openSession();
            try {
                BaseMapper mapper = session.getMapper(BaseMapper.class);
                int result = mapper.getTestData();
                System.out.println(result);
            } finally {
                session.close();
            }
        } catch (Exception ex) {
            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(stringWriter);
            ex.printStackTrace(printWriter);
            System.out.println(stringWriter.toString());
        }
    }
}

此时代码输出的结果为“1”,下面的代码与之等价,我们可以在代码中手动建立Configuration类(org.apache.ibatis.session.Configuration),而不是直接从 mybatis-config.xml 中读取:

import java.io.PrintWriter;
import java.io.StringWriter;

import oracle.jdbc.pool.OracleDataSource;

import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

/**
 * MyBatis使用测试
 * @author Tsybius2014
 * @date 2016年5月22日
 * @time 下午10:44:06
 * @remark
 */
public class MyBatisTest {
    public static void main(String[] args) {
        try {
            Configuration config = new Configuration();
            OracleDataSource dataSource = new OracleDataSource();
            dataSource.setURL("jdbc:oracle:thin:用户名/密码@127.0.0.1:1521:orcl");
            TransactionFactory transactionFactory = new JdbcTransactionFactory();
            Environment environment = new Environment("development", transactionFactory, dataSource);
            config.setEnvironment(environment);
            config.addMapper(BaseMapper.class);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
            SqlSession session = sqlSessionFactory.openSession();
            try {
                BaseMapper mapper = session.getMapper(BaseMapper.class);
                int result = mapper.getTestData();
                System.out.println(result);
            } finally {
                session.close();
            }
        } catch (Exception ex) {
            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(stringWriter);
            ex.printStackTrace(printWriter);
            System.out.println(stringWriter.toString());
        }
    }
}

END

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!