春天的熊

ibatis快速通用简单配置

SqlMapConfig.xml

 

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

 

<!DOCTYPE sqlMapConfig     

    PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"     

    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

 

<sqlMapConfig>

 

  <!-- 配置事物管理器 -->

  <transactionManager type="JDBC" commitRequired="false">

    <dataSource type="SIMPLE">

      <property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>

      <property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/TestIbatis"/>

      <property name="JDBC.Username" value="root"/>

      <property name="JDBC.Password" value="admin"/>

    </dataSource>

  </transactionManager>

 

  <!-- 映射文件的配置  -->

  <sqlMap resource="com/shunwang/userinfo.xml"/>

  <!-- List more here...

  <sqlMap resource="com/mydomain/data/Order.xml"/>

  <sqlMap resource="com/mydomain/data/Documents.xml"/>

  -->

 

</sqlMapConfig>

 

 

普通映射文件.xml

(本例:userinfo.xml)

 

 

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

 

<!DOCTYPE sqlMap  PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"     

    "http://ibatis.apache.org/dtd/sql-map-2.dtd">

 

<sqlMap>

 

  <!-- 使用类的别名,以避免每次都输入完整的类名   -->

  <typeAlias alias="Userinfo" type="com.shunwang.pojo.Userinfo"/>

 

  <!-- 用于存放结果集和表之间的对应关系  -->

  <resultMap id="userinfo" class="Userinfo">

    <result property="id" column="id"/>

    <result property="name" column="name"/>

    <result property="password" column="password"/>

  </resultMap>

  <!-- 具体的执行SQL语句代码  -->

  <!-- 查询   -->

  <select id="getAllUsers" resultMap="userinfo">

     select * from userinfo;

  </select>

  <select id="getUserById" parameterClass="int"  resultMap="userinfo">

     select * from userinfo where id=#id#;

  </select>

   <select id="getUserByName" parameterClass="String"  resultMap="userinfo">

     select * from userinfo where name like '%$name$%'

  </select>

  <select id="getAllUsersWithCondition" parameterClass="int"  resultMap="userinfo">

     select * from userinfo;

  </select>

  <!-- 插入操作   -->

  <insert id="addUser" parameterClass="userinfo">

    insert into userinfo (name,password)

    values (#name#,#password#)

  </insert>

  <!-- 删除操作   -->

  <delete id="delUser" parameterClass="int">

    delete from userinfo where id = #id#

  </delete>

  <!-- 修改操作  -->

  <update id="modifyUser" parameterClass="userinfo">

    update userinfo set name=#name#,password=#password# where id=#id#

  </update> 

 

  <!-- Select with no parameters using the result map for Account class.

  <select id="selectAllAccounts" resultMap="AccountResult">

    select * from ACCOUNT

  </select>

 

   A simpler select example without the result map.  Note the

       aliases to match the properties of the target result class.

  <select id="selectAccountById" parameterClass="int" resultClass="Account">

    select

      ACC_ID as id,

      ACC_FIRST_NAME as firstName,

      ACC_LAST_NAME as lastName,

      ACC_EMAIL as emailAddress

    from ACCOUNT

    where ACC_ID = #id#

  </select>

  

  Insert example, using the Account parameter class

  <insert id="insertAccount" parameterClass="Account">

    insert into ACCOUNT (

      ACC_ID,

      ACC_FIRST_NAME,

      ACC_LAST_NAME,

      ACC_EMAIL

    values (

      #id#, #firstName#, #lastName#, #emailAddress#

    )

  </insert>

 

   Update example, using the Account parameter class

  <update id="updateAccount" parameterClass="Account">

    update ACCOUNT set

      ACC_FIRST_NAME = #firstName#,

      ACC_LAST_NAME = #lastName#,

      ACC_EMAIL = #emailAddress#

    where

      ACC_ID = #id#

  </update>

 

    Delete example, using an integer as the parameter class

  <delete id="deleteAccountById" parameterClass="int">

    delete from ACCOUNT where ACC_ID = #id#

  </delete>

  -->

 

</sqlMap>

 

2012-03-13
/  标签: ibatisJava
   
评论
坚信上帝的延迟,并不是上帝的拒绝。

那些还没有到来的,总会在找寻和尝试中来到。

Here,成长的角落。

Q_Q:317886534