Java配置IBatis

原文链接:http://www.cnblogs.com/ChenLeo/archive/2011/05/30/2062976.html

前提:把数据库、commons-logging.jar,IBatis.jar和jdbc.jar等放入ClassPath中

1.创建sqlMapConfig.xml和map.xml等文件。

对于这些文件的格式必须参照(sql-map-config.dtd和sql-map.dtd--他们最好要对应相对版本的JAR)

2.sqlMapConfig.xml的写法

例子:

注:下文中的“model.user”为在项目的model包中创建的user类,包含account和password两个属性。

<!--encoding是指这个xml存储时的编码方式-->

<?xml version="1.0" encoding="windows-1252" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<!-- Always ensure to use the correct XML header as above! -->
<sqlMapConfig>
<!-- The properties (name=value) in the file specified here can be used placeholders in this
config file (e.g. “${driver}”. The file is relative to the classpath and is completely optional. -->
 <!--    <properties resource="examples/sqlmap/maps/SqlMapConfigExample.properties" /> -->

<!-- Type aliases allow you to use a shorter name for long fully qualified class names. -->
    <typeAlias alias="User" type="model.user"/>

<!-- Configure a datasource to use with this SQL Map using SimpleDataSource.
Notice the use of the properties from the above resource -->
    <transactionManager type="JDBC" >
        <dataSource type="SIMPLE">

   <!--设置IBatis要使用的jdbc-->
            <property name="JDBC.Driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>

    <!--设置数据库的连接串。其中是服务器名,端口号,数据库名字-->
            <property name="JDBC.ConnectionURL" value="jdbc:sqlserver://localhost:1433; DatabaseName=MyShop"/>

    <!--设置登录数据库用的用户名和密码-->
            <property name="JDBC.Username" value="sa"/>
            <property name="JDBC.Password" value="123"/>
            <property name="JDBC.DefaultAutoCommit" value="true" />
            <property name="Pool.MaximumActiveConnections" value="10"/>
            <property name="Pool.MaximumIdleConnections" value="5"/>
            <property name="Pool.MaximumCheckoutTime" value="120000"/>
            <property name="Pool.TimeToWait" value="500"/>
            <property name="Pool.PingQuery" value="select 1 from ACCOUNT"/>
            <property name="Pool.PingEnabled" value="false"/>
            <property name="Pool.PingConnectionsOlderThan" value="1"/>
            <property name="Pool.PingConnectionsNotUsedFor" value="1"/>
        </dataSource>
    </transactionManager>

<!-- Identify all SQL Map XML files to be loaded by this SQL map. Notice the paths
are relative to the classpath. For now, we only have one… -->

 <!--这个map.xml在项目一个jar的map包下-->   
    <sqlMap resource="map/userMap.xml" />
</sqlMapConfig>

3.map.xml的写法

例子:

<!--encoding是指这个xml存储时的编码方式-->

<?xml version="1.0" encoding="windows-1252"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap  namespace="User">
   
    <resultMap id="userResult" class="model.user">
        <result property="account" column="Account"/>
        <result property="password" column="Password"/>
    </resultMap>
   
    <select id="getUserByAccount" resultMap="userResult">
        select * from Users where Account = #Value#
    </select>

    <select id="getUsers" resultMap="userResult">
        select * from Users
    </select>
</sqlMap>
3.把相应的commons-logging.jdk,IBatis.jdk,jdbc.jdk等添加到项目的库中

4.在main函数中创建利用Ibatis来访问数据库显示数据的过程

       -创建相应的SqlMapClient对象

       -用SqlMapClient调用getUserByAccount的SQL语句

       -显示查询的结果

例子:

public static void main(String[] args) {

   //创建相应的SqlMapClient对象

    //IBatis在项目中的配置方案,即前面的SqlMapConfig.xml 

            String resource = "config/SqlMapConfig.xml";
            Reader reader = null;

            SqlMapClient sqlMap = null;
            try {

    //读取配置文件
                reader = Resources.getResourceAsReader(resource);

    //创建这个SqlMapClient对象。即按照配置方案去整理数据库连接方式和相应的map.xml文件
                XmlSqlMapClientBuilder xmlBuilder = new XmlSqlMapClientBuilder();
                sqlMap = xmlBuilder.buildSqlMap(reader);
            } catch (Exception e) {
                e.printStackTrace();
            }
       

   //用sqlMap去读取user信息 

        user user = (user)sqlMap.queryForObject("getUserByAccount", "ChenLeo");
        System.out.println(user);

//用sqlMap去读取大量用户的信息  
        List<user> list = sqlMap.queryForList("getUsers", null);
        for (user item : list) {
            System.out.println(item);
        }

转载于:https://www.cnblogs.com/ChenLeo/archive/2011/05/30/2062976.html

上一篇:Lucene.net(4.8.0) 学习问题记录一:分词器Analyzer的构造和内部成员ReuseStategy


下一篇:java – 查询是否只能在Ibatis上的resultMap上返回部分结果?