ibatis

来源:互联网 发布:梦幻西游for mac 编辑:程序博客网 时间:2024/05/12 02:09

Student.xml:

<?xml version="1.0" encoding="UTF-8"?>
<sqlMap>
    <typeAlias alias="Student" type="com.cn.entity.Student"/>
    <insert id="insertStudentBySequene" parameterClass="Student">
        <selectKey resultClass="int" keyProperty="sid">
            select studentPKSequence.nextVal as sid from dual
        </selectKey>
        insert into Student(sid,sname,major,birth) values (#sid#,#sname#,#major#,#birth#)
    </insert>
    <select id="selectAllStudents" resultClass="Student">
        select * from student
    </select>
    <select id="selectStudentById" parametername="int"  resultClass="Student">
        select * from student where sid = #sid#
    </select>
    <insert id="insertStudent" parameterClass="Student">
        insert into Student(sid,sname,major,birth) values (#sid#,#sname#,#major#,#birth#)
    </insert>
    <delete id="deleteStudentById" parameterClass="int">
        delete from Student where sid = #sid#
    </delete>
    <update id="updateStudentById" parameterClass="Student">
        update Student set sname=#sname#,marjor=#major#,birth=#birth# where id="#sid#"
    </update>    
    <select id="selectStudentByName" parameterClass="String" resultClass="Student">
        select * from student where sname like '%$sname$%'    
    </select>
    <!--
    以下个为例写动态sql
    判断是否为空
    判断是否存在
    -->
    <select id="selectStudentByName" parameterClass="String" resultClass="Student">
        select * from student
        <dynamic prepend=" where ">
            <isNotEmpty property="sname" prepend="and" open="(" close=")">
                sname like '%$sname$%'
            </isNotEmpty>
            <isNotNull property="sname">
                ...
            </isNotNull>
        </dynamic>
    </select>   
</sqlMap>



SqlMap.properties:

driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl
username=scott
password=scott


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>

  <properties resource="com/cn/SqlMap.properties"/>
 
  <settings
    useStatementNamespaces="true"
    cacheModelsEnabled="true"/>
    
  <transactionManager type="JDBC">
    <dataSource type="SIMPLE">
      <property value="${driver}" name="JDBC.Driver"/>
      <property value="${url}" name="JDBC.ConnectionURL"/>
      <property value="${username}" name="JDBC.Username"/>
      <property value="${password}" name="JDBC.Password"/>
    </dataSource>
  </transactionManager>

  <sqlMap resource="com/cn/entity/Student.xml"/>

</sqlMapConfig>