这段时间空闲时间比较多一点,对使用IBatisNet的一些体会进行整理,在这里感觉善友大哥,
我IBatis的入门就是看的他的文章,这个demo也是改的他的代码。
hibernate 的强大在于完全的对象化,对于对象之间的关系解决的比较好,
如1对1,1对多,多对1,以及多对多。当然也包括继承关系。而ibatis这方面就比较逊色了,
不过对于也支持简单的关连查询,如1对1,和1对多。对于一般的情况来说,这两种已经足够了。
下面进入正文讲一下IBatisNet的一对多配置。
开发环境:VS2005 SqlServer2000 IBatisNet框架(IBatisNet.DataMapper1.6.1)
主要内容:1.准备数据表结构 2.编写实体类 3.构建配置信息
1.准备数据表结构
在这个例子中,我们引入了两个对象User、Post,它们之间的关系是一对多,即一个User有多篇Post。需要用到的数据表结构如下
CREATE
TABLE
Users (
UserID int identity NOT NULL ,
UserName nvarchar ( 40 ) NOT NULL ,
Password nvarchar ( 20 ) NOT NULL ,
EmailAddress nvarchar ( 40 ) NOT NULL ,
LastLogon datetime ,
PRIMARY KEY (UserID)
)
CREATE TABLE Posts (
PostID int identity NOT NULL ,
Title nvarchar ( 100 ) NOT NULL ,
Message ntext ,
CreatedDate datetime ,
ModifiedDate datetime ,
UserID int NOT NULL ,
PRIMARY KEY (PostID),
FOREIGN KEY (UserID) References Users(UserID)
)
2.编写实体类
UserID int identity NOT NULL ,
UserName nvarchar ( 40 ) NOT NULL ,
Password nvarchar ( 20 ) NOT NULL ,
EmailAddress nvarchar ( 40 ) NOT NULL ,
LastLogon datetime ,
PRIMARY KEY (UserID)
)
CREATE TABLE Posts (
PostID int identity NOT NULL ,
Title nvarchar ( 100 ) NOT NULL ,
Message ntext ,
CreatedDate datetime ,
ModifiedDate datetime ,
UserID int NOT NULL ,
PRIMARY KEY (PostID),
FOREIGN KEY (UserID) References Users(UserID)
)
public
class
User
{
#region UserID
private Int32 m_userID;
/// <summary> Gets or sets UserID </summary>
public Int32 UserID
{
get { return m_userID; }
set { m_userID = value; }
}
#endregion
#region UserName
private String m_userName;
/// <summary> Gets or sets UserName </summary>
public String UserName
{
get { return m_userName; }
set { m_userName = value; }
}
#endregion
#region Password
private String m_password;
/// <summary> Gets or sets Password </summary>
public String Password
{
get { return m_password; }
set { m_password = value; }
}
#endregion
#region EmailAddress
private String m_emailAddress;
/// <summary> Gets or sets EmailAddress </summary>
public String EmailAddress
{
get { return m_emailAddress; }
set { m_emailAddress = value; }
}
#endregion
#region LastLogon
private DateTime ? m_lastLogon;
/// <summary> Gets or sets LastLogon </summary>
public DateTime ? LastLogon
{
get { return m_lastLogon; }
set { m_lastLogon = value; }
}
#endregion
private IList < Post > m_postList;
public IList < Post > Posts
{
get { return m_postList; }
set { m_postList = value; }
}
}
public class Post
{
#region PostID
private Int32 m_postID;
/// <summary> Gets or sets PostID </summary>
public Int32 PostID
{
get { return m_postID; }
set { m_postID = value; }
}
#endregion
#region Title
private String m_title;
/// <summary> Gets or sets Title </summary>
public String Title
{
get { return m_title; }
set { m_title = value; }
}
#endregion
#region Message
private String m_message;
/// <summary> Gets or sets Message </summary>
public String Message
{
get { return m_message; }
set { m_message = value; }
}
#endregion
#region CreatedDate
private DateTime ? m_createdDate;
/// <summary> Gets or sets CreatedDate </summary>
public DateTime ? CreatedDate
{
get { return m_createdDate; }
set { m_createdDate = value; }
}
#endregion
#region ModifiedDate
private DateTime ? m_modifiedDate;
/// <summary> Gets or sets ModifiedDate </summary>
public DateTime ? ModifiedDate
{
get { return m_modifiedDate; }
set { m_modifiedDate = value; }
}
#endregion
#region UserID
private User m_user;
/// <summary> Gets or sets UserID </summary>
public User User
{
get { return m_user; }
set { m_user = value; }
}
#endregion
}
3.编写配置文件
{
#region UserID
private Int32 m_userID;
/// <summary> Gets or sets UserID </summary>
public Int32 UserID
{
get { return m_userID; }
set { m_userID = value; }
}
#endregion
#region UserName
private String m_userName;
/// <summary> Gets or sets UserName </summary>
public String UserName
{
get { return m_userName; }
set { m_userName = value; }
}
#endregion
#region Password
private String m_password;
/// <summary> Gets or sets Password </summary>
public String Password
{
get { return m_password; }
set { m_password = value; }
}
#endregion
#region EmailAddress
private String m_emailAddress;
/// <summary> Gets or sets EmailAddress </summary>
public String EmailAddress
{
get { return m_emailAddress; }
set { m_emailAddress = value; }
}
#endregion
#region LastLogon
private DateTime ? m_lastLogon;
/// <summary> Gets or sets LastLogon </summary>
public DateTime ? LastLogon
{
get { return m_lastLogon; }
set { m_lastLogon = value; }
}
#endregion
private IList < Post > m_postList;
public IList < Post > Posts
{
get { return m_postList; }
set { m_postList = value; }
}
}
public class Post
{
#region PostID
private Int32 m_postID;
/// <summary> Gets or sets PostID </summary>
public Int32 PostID
{
get { return m_postID; }
set { m_postID = value; }
}
#endregion
#region Title
private String m_title;
/// <summary> Gets or sets Title </summary>
public String Title
{
get { return m_title; }
set { m_title = value; }
}
#endregion
#region Message
private String m_message;
/// <summary> Gets or sets Message </summary>
public String Message
{
get { return m_message; }
set { m_message = value; }
}
#endregion
#region CreatedDate
private DateTime ? m_createdDate;
/// <summary> Gets or sets CreatedDate </summary>
public DateTime ? CreatedDate
{
get { return m_createdDate; }
set { m_createdDate = value; }
}
#endregion
#region ModifiedDate
private DateTime ? m_modifiedDate;
/// <summary> Gets or sets ModifiedDate </summary>
public DateTime ? ModifiedDate
{
get { return m_modifiedDate; }
set { m_modifiedDate = value; }
}
#endregion
#region UserID
private User m_user;
/// <summary> Gets or sets UserID </summary>
public User User
{
get { return m_user; }
set { m_user = value; }
}
#endregion
}
User类的配置文件
<?
xml version="1.0" encoding="utf-8"
?>
< sqlMap namespace ="User"
xmlns ="http://ibatis.apache.org/mapping"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
< alias >
< typeAlias alias ="User" type ="IBatisNetDemo.Domain.User,IBatisNetDemo" />
</ alias >
< resultMaps >
< resultMap id ="UserResult" class ="User" >
< result property ="UserID" column ="UserID" dbType ="Int" />
< result property ="UserName" column ="UserName" dbType ="NVarChar" />
< result property ="Password" column ="Password" dbType ="NVarChar" />
< result property ="EmailAddress" column ="EmailAddress" dbType ="NVarChar" />
< result property ="LastLogon" column ="LastLogon" dbType ="DateTime" />
</ resultMap >
< resultMap id ="User-Result" class ="User" extends ="UserResult" >
< result property ="Posts" resultMapping ="Post.PostResult" />
</ resultMap >
< resultMap id ="User_Result" class ="User" extends ="UserResult" >
< result property ="Posts" column ="UserID" lazyLoad ="true" select ="GetPostsForUser" />
</ resultMap >
</ resultMaps >
< statements >
< select id ="FindAllUser" resultMap ="User_Result" >
SELECT *
FROM [dbo].[Users]
</ select >
< select id ="GetAllUser" resultMap ="User-Result" >
select u.UserID,u.UserName,u.Password,u.EmailAddress,u.LastLogon
,p.PostID,p.Title,p.Message,p.CreatedDate,p.ModifiedDate
from Users u
left outer join Posts p
on u.UserID=p.UserID
</ select >
< select id ="FindByUserId" parameterClass ="int" resultMap ="User_Result" >
SELECT *
FROM [dbo].[Users]
WHERE
([Users].[UserID] = #UserID,dbType=Int#)
</ select >
< insert id ="InsertUser" parameterClass ="User" >
< selectKey property ="UserID" type ="post" resultClass ="int" >
${selectKey}
</ selectKey >
INSERT INTO [dbo].[Users] (
[UserName]
, [Password]
, [EmailAddress]
, [LastLogon]
) VALUES (
#UserName,dbType=NVarChar#
, #Password,dbType=NVarChar#
, #EmailAddress,dbType=NVarChar#
, #LastLogon,dbType=DateTime#
)
</ insert >
< update id ="UpdateUser" parameterClass ="User" >
UPDATE [dbo].[Users] SET
[UserName] = #UserName,dbType=NVarChar#
, [Password] = #Password,dbType=NVarChar#
, [EmailAddress] = #EmailAddress,dbType=NVarChar#
, [LastLogon] = #LastLogon,dbType=DateTime#
WHERE
([UserID] = #UserID,dbType=Int#)
</ update >
< delete id ="DeleteUser" parameterClass ="int" >
DELETE FROM [dbo].[Users]
WHERE
([UserID] = #UserID,dbType=Int#)
</ delete >
</ statements >
</ sqlMap >
这里用两种方法实现了一对多关系,使用内在的resultMap解决问题完成一对多的映射
< sqlMap namespace ="User"
xmlns ="http://ibatis.apache.org/mapping"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
< alias >
< typeAlias alias ="User" type ="IBatisNetDemo.Domain.User,IBatisNetDemo" />
</ alias >
< resultMaps >
< resultMap id ="UserResult" class ="User" >
< result property ="UserID" column ="UserID" dbType ="Int" />
< result property ="UserName" column ="UserName" dbType ="NVarChar" />
< result property ="Password" column ="Password" dbType ="NVarChar" />
< result property ="EmailAddress" column ="EmailAddress" dbType ="NVarChar" />
< result property ="LastLogon" column ="LastLogon" dbType ="DateTime" />
</ resultMap >
< resultMap id ="User-Result" class ="User" extends ="UserResult" >
< result property ="Posts" resultMapping ="Post.PostResult" />
</ resultMap >
< resultMap id ="User_Result" class ="User" extends ="UserResult" >
< result property ="Posts" column ="UserID" lazyLoad ="true" select ="GetPostsForUser" />
</ resultMap >
</ resultMaps >
< statements >
< select id ="FindAllUser" resultMap ="User_Result" >
SELECT *
FROM [dbo].[Users]
</ select >
< select id ="GetAllUser" resultMap ="User-Result" >
select u.UserID,u.UserName,u.Password,u.EmailAddress,u.LastLogon
,p.PostID,p.Title,p.Message,p.CreatedDate,p.ModifiedDate
from Users u
left outer join Posts p
on u.UserID=p.UserID
</ select >
< select id ="FindByUserId" parameterClass ="int" resultMap ="User_Result" >
SELECT *
FROM [dbo].[Users]
WHERE
([Users].[UserID] = #UserID,dbType=Int#)
</ select >
< insert id ="InsertUser" parameterClass ="User" >
< selectKey property ="UserID" type ="post" resultClass ="int" >
${selectKey}
</ selectKey >
INSERT INTO [dbo].[Users] (
[UserName]
, [Password]
, [EmailAddress]
, [LastLogon]
) VALUES (
#UserName,dbType=NVarChar#
, #Password,dbType=NVarChar#
, #EmailAddress,dbType=NVarChar#
, #LastLogon,dbType=DateTime#
)
</ insert >
< update id ="UpdateUser" parameterClass ="User" >
UPDATE [dbo].[Users] SET
[UserName] = #UserName,dbType=NVarChar#
, [Password] = #Password,dbType=NVarChar#
, [EmailAddress] = #EmailAddress,dbType=NVarChar#
, [LastLogon] = #LastLogon,dbType=DateTime#
WHERE
([UserID] = #UserID,dbType=Int#)
</ update >
< delete id ="DeleteUser" parameterClass ="int" >
DELETE FROM [dbo].[Users]
WHERE
([UserID] = #UserID,dbType=Int#)
</ delete >
</ statements >
</ sqlMap >
<
resultMap
id
="User-Result"
class
="User"
extends
="UserResult"
>
< result property ="Posts" resultMapping ="Post.PostResult" />
</ resultMap >
< select id ="GetAllUser" resultMap ="User-Result" >
select u.UserID,u.UserName,u.Password,u.EmailAddress,u.LastLogon
,p.PostID,p.Title,p.Message,p.CreatedDate,p.ModifiedDate
from Users u
left outer join Posts p
on u.UserID=p.UserID
</ select >
还有就是在resultMap 中定义嵌套查询GetPostsForUser,实现了关联数据的读取。
< result property ="Posts" resultMapping ="Post.PostResult" />
</ resultMap >
< select id ="GetAllUser" resultMap ="User-Result" >
select u.UserID,u.UserName,u.Password,u.EmailAddress,u.LastLogon
,p.PostID,p.Title,p.Message,p.CreatedDate,p.ModifiedDate
from Users u
left outer join Posts p
on u.UserID=p.UserID
</ select >
需要注意的是,这里有一个潜在的性能问题,也就是所谓“n+1”Select问题,但IBatisNet支持懒加载lazyLoad="true",来解决这个性能问题。
<
resultMap
id
="User_Result"
class
="User"
extends
="UserResult"
>
< result property ="Posts" column ="UserID" lazyLoad ="true" select ="GetPostsForUser" />
</ resultMap >
Post类的配置文件
< result property ="Posts" column ="UserID" lazyLoad ="true" select ="GetPostsForUser" />
</ resultMap >
<?
xml version="1.0" encoding="utf-8"
?>
< sqlMap namespace ="Post"
xmlns ="http://ibatis.apache.org/mapping"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
< alias >
< typeAlias alias ="Post" type ="IBatisNetDemo.Domain.Post,IBatisNetDemo" />
</ alias >
< resultMaps >
< resultMap id ="PostResult" class ="Post" >
< result property ="PostID" column ="PostID" dbType ="Int" />
< result property ="Title" column ="Title" dbType ="NVarChar" />
< result property ="Message" column ="Message" dbType ="NText" />
< result property ="CreatedDate" column ="CreatedDate" dbType ="DateTime" />
< result property ="ModifiedDate" column ="ModifiedDate" dbType ="DateTime" />
</ resultMap >
< resultMap id ="Post-Result" class ="Post" extends ="PostResult" >
< result property ="User" resultMapping ="User.UserResult" />
</ resultMap >
</ resultMaps >
< statements >
< select id ="FindAllPost" resultMap ="Post-Result" >
select p.PostID,p.Title,p.Message,p.CreatedDate,p.ModifiedDate,
u.UserID,u.UserName,u.Password,u.EmailAddress,u.LastLogon
from Posts p,Users u
where p.UserID=u.UserID
</ select >
< select id ="GetAllPost" resultMap ="PostResult" >
select p.PostID,p.Title,p.Message,p.CreatedDate,p.ModifiedDate
from Posts p
</ select >
< select id ="FindByPostId" parameterClass ="Post" resultMap ="PostResult" extends ="FindAllPost" >
WHERE
([Posts].[PostID] = #PostID,dbType=Int#)
</ select >
< select id ="GetPostsForUser" parameterClass ="int" resultMap ="PostResult" extends ="FindAllPost" >
WHERE
([Posts].[UserID] = #User.UserID,dbType=Int#)
</ select >
< insert id ="InsertPost" parameterClass ="Post" >
< selectKey property ="PostID" type ="post" resultClass ="int" >
${selectKey}
</ selectKey >
INSERT INTO [dbo].[Posts] (
[Title]
, [Message]
, [CreatedDate]
, [ModifiedDate]
, [UserID]
) VALUES (
#Title,dbType=NVarChar#
, #Message,dbType=NText#
, #CreatedDate,dbType=DateTime#
, #ModifiedDate,dbType=DateTime#
, #User.UserID,dbType=Int#
)
</ insert >
< update id ="UpdatePost" parameterClass ="Post" >
UPDATE [dbo].[Posts] SET
[Title] = #Title,dbType=NVarChar#
, [Message] = #Message,dbType=NText#
, [CreatedDate] = #CreatedDate,dbType=DateTime#
, [ModifiedDate] = #ModifiedDate,dbType=DateTime#
, [UserID] = #User.UserID,dbType=Int#
WHERE
([PostID] = #PostID,dbType=Int#)
</ update >
< delete id ="DeletePost" parameterClass ="Post" >
DELETE FROM [dbo].[Posts]
WHERE
([PostID] = #PostID,dbType=Int#)
</ delete >
</ statements >
</ sqlMap >
小弟第一次把文章发在首页,诚惶诚恐,欢迎大家拍砖
< sqlMap namespace ="Post"
xmlns ="http://ibatis.apache.org/mapping"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
< alias >
< typeAlias alias ="Post" type ="IBatisNetDemo.Domain.Post,IBatisNetDemo" />
</ alias >
< resultMaps >
< resultMap id ="PostResult" class ="Post" >
< result property ="PostID" column ="PostID" dbType ="Int" />
< result property ="Title" column ="Title" dbType ="NVarChar" />
< result property ="Message" column ="Message" dbType ="NText" />
< result property ="CreatedDate" column ="CreatedDate" dbType ="DateTime" />
< result property ="ModifiedDate" column ="ModifiedDate" dbType ="DateTime" />
</ resultMap >
< resultMap id ="Post-Result" class ="Post" extends ="PostResult" >
< result property ="User" resultMapping ="User.UserResult" />
</ resultMap >
</ resultMaps >
< statements >
< select id ="FindAllPost" resultMap ="Post-Result" >
select p.PostID,p.Title,p.Message,p.CreatedDate,p.ModifiedDate,
u.UserID,u.UserName,u.Password,u.EmailAddress,u.LastLogon
from Posts p,Users u
where p.UserID=u.UserID
</ select >
< select id ="GetAllPost" resultMap ="PostResult" >
select p.PostID,p.Title,p.Message,p.CreatedDate,p.ModifiedDate
from Posts p
</ select >
< select id ="FindByPostId" parameterClass ="Post" resultMap ="PostResult" extends ="FindAllPost" >
WHERE
([Posts].[PostID] = #PostID,dbType=Int#)
</ select >
< select id ="GetPostsForUser" parameterClass ="int" resultMap ="PostResult" extends ="FindAllPost" >
WHERE
([Posts].[UserID] = #User.UserID,dbType=Int#)
</ select >
< insert id ="InsertPost" parameterClass ="Post" >
< selectKey property ="PostID" type ="post" resultClass ="int" >
${selectKey}
</ selectKey >
INSERT INTO [dbo].[Posts] (
[Title]
, [Message]
, [CreatedDate]
, [ModifiedDate]
, [UserID]
) VALUES (
#Title,dbType=NVarChar#
, #Message,dbType=NText#
, #CreatedDate,dbType=DateTime#
, #ModifiedDate,dbType=DateTime#
, #User.UserID,dbType=Int#
)
</ insert >
< update id ="UpdatePost" parameterClass ="Post" >
UPDATE [dbo].[Posts] SET
[Title] = #Title,dbType=NVarChar#
, [Message] = #Message,dbType=NText#
, [CreatedDate] = #CreatedDate,dbType=DateTime#
, [ModifiedDate] = #ModifiedDate,dbType=DateTime#
, [UserID] = #User.UserID,dbType=Int#
WHERE
([PostID] = #PostID,dbType=Int#)
</ update >
< delete id ="DeletePost" parameterClass ="Post" >
DELETE FROM [dbo].[Posts]
WHERE
([PostID] = #PostID,dbType=Int#)
</ delete >
</ statements >
</ sqlMap >