zoukankan      html  css  js  c++  java
  • ibatis简单运行

    我所有的代码都在ibatis包下,包括类和配置文件 

    代码
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"
    >
    <sqlMapConfig>
    <settings cacheModelsEnabled="false" enhancementEnabled="false"
    lazyLoadingEnabled
    ="false" maxRequests="3000" maxSessions="3000"
    maxTransactions
    ="3000" useStatementNamespaces="false" />
    <transactionManager type="JDBC">
    <dataSource type="simple">
    <property name="JDBC.Driver"
    value
    ="com.mysql.jdbc.Driver" />
    <property name="JDBC.ConnectionURL"
    value
    ="jdbc:mysql://localhost:3306/test" />
    <property name="JDBC.Username" value="root" />
    <property name="JDBC.Password" value="******" />
    </dataSource>
    </transactionManager>
    <sqlMap resource="ibatis/user.xml" />
    </sqlMapConfig>

    接下去是user.xml

    代码
    <?xml version="1.0" encoding="UTF-8"?>

    <!DOCTYPE sqlMap
    PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
    "http://ibatis.apache.org/dtd/sql-map-2.dtd"
    >

    <sqlMap namespace="User">

    <typeAlias alias="user" type="ibatis.User" />
    <select id="getUser" resultClass="user" parameterClass="java.lang.String">
    select * from user where name like '%$name$%';
    </select>

    <select id="getAllUser" resultClass="user">
    select * from user;
    </select>
    </sqlMap>
    代码
    package ibatis;

    import java.io.IOException;
    import java.io.Reader;
    import java.sql.SQLException;
    import java.util.List;

    import org.apache.log4j.BasicConfigurator;

    import com.ibatis.common.resources.Resources;
    import com.ibatis.sqlmap.client.SqlMapClient;
    import com.ibatis.sqlmap.client.SqlMapClientBuilder;

    public class IbatisDemo {
    public static void main(String[] args) throws IOException, SQLException {
    BasicConfigurator.configure();
    String resource
    = "ibatis/sqlmap.xml";
    Reader reader
    = Resources.getResourceAsReader(resource);

    SqlMapClient sqlMap
    = SqlMapClientBuilder.buildSqlMapClient(reader);
    List list
    = sqlMap.queryForList("getUser", "sa");
    System.out.println(list.size());

    list
    = sqlMap.queryForList("getAllUser");
    System.out.println(list.size());
    }
    }

     User.java类我就不贴了,一个只有两个private属性的bean,很简单的run就可以得出结果,这也是最简单的ibatis应用了

  • 相关阅读:
    通过Java获取图片的信息
    防止浏览器缓冲
    InitializingBean,DisposableBean
    php 直接跳出嵌套循环
    个推IGt.BaseTemplate.php,不仅有bug,还有bom头,好恶心!
    【洛谷】2473:[SCOI2008]奖励关【期望DP(倒推)】
    【BZOJ】4720: [Noip2016]换教室
    【BZOJ】4767: 两双手【组合数学】【容斥】【DP】
    【BZOJ】4318: OSU!【期望DP】
    【BZOJ】4985: 评分【DP】
  • 原文地址:https://www.cnblogs.com/xiziyin/p/1628554.html
Copyright © 2011-2022 走看看