mybatis简单示例

mybatis简单示例

创建mybatis配置文件:mybatis.xml

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE configuration PUBLIC "mybatis-3-mapper.dtd" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"/>
<!--数据库环境,支持配置多个environment  default参数为任意environment的id-->
<environments default="oracleEnvironment">
    <environment id="oracleEnvironment">
        <!--配置事务管理器  type可选值:JDBC,MANAGED-->
        <transactionManager type="JDBC"></transactionManager>
        <!--配置数据源对象,type类型可选值:JNDI,POOLED,UNPOOLED-->
        <dataSource type="POOLED">
            <property name="driver" value="${driver}"/>
            <property name="url" value="${url}"/>
            <property name="username" value="${username}"/>
            <property name="password" value="${pass}"/>
        </dataSource>
    </environment>
</environments>
<!--配置映射文件-->
<mappers>
    <mapper resource="switchcfg.xml"/>
</mappers>
</configuration>

创建数据库信息文件db.properties

driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
username=system
pass=123456

创建数据库操作接口类SwictchCfgDAO

public interface SwictchCfgDAO {
/**
 * 根据参数查询表数据
 * @param switchId
 * @return
 * @throws Exception
 */
public SwitchCfg getSwitchById(String switchId)throws Exception;
}

创建接口类映射文件switch.xml

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

<mapper namespace="com.qgni.SwictchCfgDAO">
<select id="getSwitchById" parameterType="java.lang.String" resultType="com.qgni.SwitchCfg">
    select * from comm.switch_cfg where switch_id = #{switchId}
</select>
</mapper>

创建实体类对象SwitchCfg

public class SwitchCfg {
String switch_ID;
String switch_Name;
String switch_Flag;
String switch_Content;
String switch_Class;
String ordinal;
String switch_Class_Code;
String class_Ordinal;

public String getSwitch_ID() {
    return switch_ID;
}

public void setSwitch_ID(String switch_D) {
    this.switch_ID = switch_D;
}

public String getSwitch_Name() {
    return switch_Name;
}

public void setSwitch_Name(String switch_Name) {
    this.switch_Name = switch_Name;
}

public String getSwitch_Flag() {
    return switch_Flag;
}

public void setSwitch_Flag(String switch_Flag) {
    this.switch_Flag = switch_Flag;
}

public String getSwitch_Content() {
    return switch_Content;
}

public void setSwitch_Content(String switch_Content) {
    this.switch_Content = switch_Content;
}

public String getSwitch_Class() {
    return switch_Class;
}

public void setSwitch_Class(String switch_Class) {
    this.switch_Class = switch_Class;
}

public String getOrdinal() {
    return ordinal;
}

public void setOrdinal(String ordinal) {
    this.ordinal = ordinal;
}

public String getSwitch_Class_Code() {
    return switch_Class_Code;
}

public void setSwitch_Class_Code(String switch_Class_Code) {
    this.switch_Class_Code = switch_Class_Code;
}

public String getClass_Ordinal() {
    return class_Ordinal;
}

public void setClass_Ordinal(String class_Ordinal) {
    this.class_Ordinal = class_Ordinal;
}
/**
 * 重写toString方法为了测试显示
 * @return
 */
@Override
public String toString() {
    return switch_ID+switch_Name;
}
}

编写测试类MyBatisTest

public class MyBatisTest {
    public static void main(String[] args) {
        String resource = "mybatis.xml";
        Reader reader = null;
        SqlSession session = null;
        try {
            reader = Resources.getResourceAsReader(resource);
            //初始化工厂
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
            //获取数据库操作对象
            session= sessionFactory.openSession();
            SwictchCfgDAO swictchCfgDAO  = session.getMapper(SwictchCfgDAO.class);
            SwitchCfg switchCfg = swictchCfgDAO.getSwitchById("inp_sel_023");
            System.out.println(switchCfg.toString());
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(session!=null){
                session.close();
            }
        }
    }
}

测试结果

1418753-20181110141701041-56583140.png

转载于:https://www.cnblogs.com/qgni/p/9939309.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值