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# 为参数;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值