关于mybatis连接oracle数据库实例

首先是mybatis 和数据库交互文件,mybatis-config.xml文件,昨天写过了。今天写sql语句的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="InasTestMapper">

    <select id="selectInas" resultType="com.boco.active.inas.database.Inas_test" parameterType="com.boco.active.inas.database.Inas_test">
        select * from inas_test where id = #{id}
    </select>

    <insert id="insertInas" parameterType="com.boco.active.inas.database.Inas_test">
        insert into inas_test(id,userName,requestInfo,requestTime) values(#{id}, #{userName},#{requestInfo},#{requestTime})
    </insert>

    <update id="updateInas" parameterType="com.boco.active.inas.database.Inas_test">
        update inas_test set responseInfo = #{responseInfo},responseTime = #{responseTime} where id=#{id}
    </update>

    <delete id="deleteInas" parameterType="String">
        delete from inas_test where id=#{id}
    </delete>

</mapper>

 

查询,插入,更新,和删除语句。都是单条的。

然后就是inas_test表对应的java文件。

package com.boco.active.inas.database;

/**
 * Created by lihuan on 2016/6/1.
 */
public class Inas_test {

    private String id;
    private String userName;
    private String requestInfo;
    private String requestTime;
    private String responseInfo;
    private String responseTime;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getRequestInfo() {
        return requestInfo;
    }

    public void setRequestInfo(String requestInfo) {
        this.requestInfo = requestInfo;
    }

    public String getRequestTime() {
        return requestTime;
    }

    public void setRequestTime(String requestTime) {
        this.requestTime = requestTime;
    }

    public String getResponseInfo() {
        return responseInfo;
    }

    public void setResponseInfo(String responseInfo) {
        this.responseInfo = responseInfo;
    }

    public String getResponseTime() {
        return responseTime;
    }

    public void setResponseTime(String responseTime) {
        this.responseTime = responseTime;
    }
}

 

最后是测试的call文件

package com.boco.active.inas.database;

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

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

public class Call {

    public static SqlSessionFactory sqlSessionFactory = null;

    public static void main(String[] args) throws IOException {

        kickStartMybatis();
        selectInas("1");
//        String keyId = insertInas("9","15006","reqiest information","20160601 101010");
//        updateInas(keyId,"response information","20160601 111111");

//        deleteInas("5");

    }
    public static void kickStartMybatis() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }
    public static void selectInas (String id) {
        SqlSession session = sqlSessionFactory.openSession();
        try {

            Inas_test inas_test =new Inas_test();
            inas_test.setId(id);
            Inas_test result = session.selectOne("selectInas", inas_test);
            session.commit();
            System.out.println(result.getRequestInfo());
        } catch (Exception e) {
            session.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
    }
    public static String insertInas(String id,String userName,String requestInfo,String requestTime) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            Inas_test inas_test = new Inas_test();
            inas_test.setId(id);
            inas_test.setUserName(userName);
            inas_test.setRequestInfo(requestInfo);
            inas_test.setRequestTime(requestTime);
            int result  = session.insert("insertInas",inas_test);
            session.commit();
            System.out.println(result);
            return inas_test.getId();
        } catch (Exception e) {
            session.rollback();
            e.printStackTrace();
            return "FAlSE";
        } finally {
            session.close();
        }

    }
    public static void updateInas(String id,String responseInfo,String responseTime){
        SqlSession session = sqlSessionFactory.openSession();
        try {
            Inas_test inas_test = new Inas_test();
            inas_test.setId(id);
            inas_test.setResponseInfo(responseInfo);
            inas_test.setResponseTime(responseTime);

            int result  = session.update("updateInas", inas_test);
            session.commit();
            System.out.println(result);
        } catch (Exception e) {
            session.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
    }
    public static void deleteInas(String id){
        SqlSession session = sqlSessionFactory.openSession();
        try {

            Inas_test inas_test =new Inas_test();
            inas_test.setId(id);
            int result = session.delete("deleteInas", inas_test);
            session.commit();
            System.out.println(result);
        } catch (Exception e) {
            session.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
    }
}

注意建表时关键字的约束条件。 还有就是session后面的操作类型,最后要加session.commit(),要不然不写入数据库。

 

 

转载于:https://my.oschina.net/hxflar1314520/blog/685360

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值