Mybatis_java项目

来源:互联网 发布:java.util.zip 加密 编辑:程序博客网 时间:2024/06/04 19:57

在项目遇到了一个问题,需要对一批zip格式的数据进行预处理然后入库操作,选用mybatis进行数据库的处理。

网上查阅相关资料MyBatis学习总结(一)

Mybatis大致介绍

MyBatis是一个支持普通SQL查询存储过程高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装。MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录。

1.项目中需要相应的jar包,我的是mybatis-3.2.2.jar和ojdbc14.jar两个jar包

 

2.按下图创建配置文件



Configuration.xml中内容

<?xml version="1.0" encoding="UTF-8" ?><!--       Copyright 2009-2012 the original author or authors.       Licensed under the Apache License, Version 2.0 (the "License");       you may not use this file except in compliance with the License.       You may obtain a copy of the License at          http://www.apache.org/licenses/LICENSE-2.0       Unless required by applicable law or agreed to in writing, software       distributed under the License is distributed on an "AS IS" BASIS,       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.       See the License for the specific language governing permissions and       limitations under the License.--><!DOCTYPE configuration    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"    "http://mybatis.org/dtd/mybatis-3-config.dtd">  <configuration><!--  <settings>    <setting name="useGeneratedKeys" value="false"/>    <setting name="useColumnLabel" value="true"/>  </settings>  <typeAliases>    <typeAlias alias="UserAlias" type="org.apache.ibatis.submitted.complex_property.User"/>  </typeAliases>-->  <environments default="development">    <environment id="development">      <transactionManager type="JDBC">        <property name="" value=""/>      </transactionManager>
<pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; line-height: 20.8px; font-family: 'Courier New' !important;"><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">     <!--</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 配置数据库连接信息 </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">--></span>
<dataSource type="UNPOOLED"> <property name="driver" value="oracle.jdbc.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@xx.xx.xx.xx:1521:orcl"/> <property name="username" value="xxx"/> <property name="password" value="xxx"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/run/wz/rgis/imeiDataIntoOracle/config/sqlxml/ImeiDataMapper.xml"/> </mappers></configuration>

3.编写测试代码

package com.run.wz.rgis.imeiDataIntoOracle.controller;import java.io.File;import java.io.InputStream;import java.sql.SQLException;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import com.run.wz.rgis.imeiDataIntoOracle.dataAnalyse.ImeiDataAnalyse;import com.run.wz.rgis.imeiDataIntoOracle.entity.ImeiInDBTime;import com.run.wz.rgis.imeiDataIntoOracle.entity.ImeiLastPoint;import com.run.wz.rgis.imeiDataIntoOracle.entity.ImeiPoint;import com.run.wz.rgis.imeiDataIntoOracle.parseImeiData.ReadBcp;import com.run.wz.rgis.imeiDataIntoOracle.parseImeiData.ZipFileScaner;public class TestSql {    public static void main(String[] args){    SqlSession sqlSession = null;    sqlSession = sqlSession();    sqlSession.update("ImeiDataManage.detailInfoQueryMapper.insertImeiLastData", object);    sqlSession.commit();    sqlSession.clearCache();    sqlSession.close();               }    public static SqlSession sqlSession(){    //mybatis配置文件    String resource = "com/run/wz/rgis/imeiDataIntoOracle/config/Configuration.xml";    //类的加载器加载配置文件    InputStream is = TestSql.class.getClassLoader().getResourceAsStream(resource);    //通过配置信息构建一个SqlSessionFactory    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);    //通过SqlSessionFactory打开一个数据库会话    SqlSession sqlSession = sqlSessionFactory.openSession();    return sqlSession;    }}



0 0