ibatis的使用心得

ibatis的使用心得;

1、配置

* 拷贝jar包、驱动文件、ibatis的配置文件和映射文件(这两个一般在测试类中);
* sqlmapconfig.xml配置 如下:

<sqlMapConfig>
<settings useStatementNamespaces="true" statementCachingEnabled="true" /> //开启命名模式,如果为true的话,后期在测试类中的方法引用都需要加上类名 如 Student.updateStudent
  <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/ibatisdb"/>
      <property name="JDBC.Username" value="root"/>
      <property name="JDBC.Password" value="xiaotong"/>
    </dataSource>
  </transactionManager>
  <sqlMap resource="Student.xml"/> //映射信息

</sqlMapConfig>

注:配置文件的路径一般放在src下;
映射文件一般也放在src下;

* 映射文件的配置信息如下:
(自己使用的时候不必要的方法可以删除掉,根据自己的需求来配)

<sqlMap namespace="Student">

  <typeAlias alias="Student" type="com.rydl.pojo.Student"/>
  <select id="AllStudent" resultClass="Student">
    select * from Student
  </select>

  <select id="StudentById" parameterClass="int" resultClass="Student"> //参数类型 和 返回结果类型
    select
      id,name,password
    from Student
    where id = #id#
  </select>
 
  <select id="selectStudentByName" parameterClass="String" resultClass="Student">
  select id,name,password from Student where name like '%$name$%' //ibatis下的模糊查询需要这样表示'%$name$%'  这里的%号替代了#号;
  </select>
 
  <insert id="insertStudent" parameterClass="Student">
  insert into Student(id,name,password) values (#id#,#name#,#password#)
  </insert>
  
  <update id="updateStudent" parameterClass="Student">
    update Student set
      name = #name#,
      password = #password#
    where
      id = #id#
  </update>

  <delete id="deleteStudentById" parameterClass="int">
    delete from Student where id = #id#
  </delete>

</sqlMap>

注:#name# 为参数;

你可能感兴趣的:(xml,ibatis)