1,新增测试类
namespace Demo.Demo9
{
public class person
{
public int id { get; set; }
public string name { get; set; }
public int age { get; set; }
}
}
2,新增配置文件
<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="Ibatis" xmlns="http://ibatis.apache.org/mapping" xmlns:xls="http://www.w3.org/2001/XMLSchema-instance">
<alias>
<typeAlias alias="person" type="Demo.Demo9.person,Demo" />
</alias>
<statements>
<insert id="InsertPerson" parameterClass="person" >
<!--此处获取插入的ID-->
<selectKey property="id" type="post" resultClass="int">
<![CDATA[ select @@IDENTITY ]]>
</selectKey>
<![CDATA[ insert into person(name,age) values(#name#,#age#) ]]>
</insert>
<delete id="DeletePerson" parameterClass="int">
<![CDATA[ delete from person where id=#id# ]]>
</delete>
<!--parameterClass定义:传参类型-->
<update id="UpdatePerson" parameterClass="System.Collections.IDictionary">
<![CDATA[ update person set name=#value1#,age=#value2# where id=#id# ]]>
</update>
<!--resultClass定义:返回值类型-->
<select id="SelectPerson" resultClass="person">
<!-- CDATA避免转义字符在XML的错误识别 -->
<![CDATA[ select * from person ]]>
</select>
</statements>
</sqlMap>
3,代码实现
public void Demo9()
{
//参数准备
Dictionary<string,object> dic = new Dictionary<string,object>();
dic.Add("id",15);
dic.Add("value1","张三");
dic.Add("value2", 25);
ISqlMapper mapper = IBatisConfig.GetInstance;
//事务支持
try{
mapper.BeginTransaction();
//增删改查
var addID = (int)mapper.Insert("InsertPerson", new person() { name = "李四", age = 20 });
var delRow = mapper.Delete("DeletePerson", 2);
var updateRow = mapper.Update("UpdatePerson", dic);
var personlist = mapper.QueryForList<person>("SelectPerson", null);
mapper.CommitTransaction();
}
catch (Exception)
{
mapper.RollBackTransaction();
}
}
博客主要讲述了三方面内容,一是新增测试类,二是新增配置文件,三是进行了代码实现,这些操作都与信息技术开发相关。
2840

被折叠的 条评论
为什么被折叠?



