设为首页 收藏本站
查看: 1133|回复: 0

[经验分享] NHibernate + SQLite + MVC 开发记录 三

[复制链接]

尚未签到

发表于 2016-11-30 11:26:40 | 显示全部楼层 |阅读模式

本节 建立实体类、映射、关系等


1:准备类库组件
<wbr style="line-height:22px">把<span style="line-height:26px; color:rgb(51,51,51); font-family:Arial"><span style="line-height:22px">Castle.DynamicProxy.dll</span></span><div style="line-height:22px"><span style="line-height:26px; color:rgb(51,51,51); font-family:Arial"><span style="line-height:22px">Iesi.Collections.dll</span></span></div><div style="line-height:22px"><span style="line-height:26px; color:rgb(51,51,51); font-family:Arial"><span style="line-height:22px">log4net.dll</span></span></div><div style="line-height:22px"><span style="line-height:26px; color:rgb(51,51,51); font-family:Arial"><span style="line-height:22px">NHibernate.dll</span></span></div><div style="line-height:22px"><span style="line-height:26px; color:rgb(51,51,51); font-family:Arial"><span style="line-height:22px">SQLite.NET.dll</span></span></div><div style="line-height:22px"><span style="line-height:26px; color:rgb(51,51,51); font-family:Arial"><span style="line-height:22px">拷贝到UI的bin\Debug下。</span></span><p style="line-height:22px; padding-top:0px; padding-bottom:0px; margin-top:10px; margin-bottom:10px; text-indent:2em"></p><div style="line-height:22px"> DSC0000.jpg <br></div></div><div style="line-height:22px"><span style="line-height:26px; color:rgb(51,51,51); font-family:Arial"><span style="line-height:22px">2:添加引用</span></span></div><div style="line-height:22px"><span style="line-height:26px; color:rgb(51,51,51); font-family:Arial"><span style="line-height:22px">各项目之间添加引用 UI引用BLL , Model; BLL引用DAL , Model ; DAL引用Model。</span></span></div><div style="line-height:22px"><span style="line-height:22px; color:rgb(51,51,51); font-family:Arial">对 DAL 添加</span><span style="line-height:26px; color:rgb(51,51,51); font-family:Arial"><span style="line-height:22px"><br style="line-height:22px"></span></span><span style="line-height:26px; color:rgb(51,51,51); font-family:Arial">Castle.DynamicProxy.dll<br style="line-height:22px">Iesi.Collections.dll<br style="line-height:22px">log4net.dll<br style="line-height:22px">NHibernate.dll<br style="line-height:22px">SQLite.NET.dll</span><div style="line-height:22px"><span style="line-height:26px; color:rgb(51,51,51); font-family:Arial">的引用。</span></div><div style="line-height:22px"><span style="line-height:26px; color:rgb(51,51,51); font-family:Arial"><br style="line-height:22px"></span></div><div style="line-height:22px"><span style="line-height:26px; color:rgb(51,51,51); font-family:Arial">3:拷贝数据库文件lstx.db和sqlite3.dll 到UI项目下,</span><span style="line-height:26px; color:rgb(51,51,51); font-family:Arial">并且在文件属性中,将文件的“复制到输出目录”设置为“如果较新则复制”。</span></div><div style="line-height:22px"><span style="line-height:26px; color:rgb(51,51,51); font-family:Arial"><br style="line-height:22px"></span></div><div style="line-height:22px"><span style="line-height:26px; color:rgb(51,51,51); font-family:Arial">准备工作做好了 我们在Model里写实体类,我就先挑个简单的</span><span style="line-height:26px; color:rgb(51,51,51); font-family:Arial">User类</span><span style="line-height:26px; color:rgb(51,51,51); font-family:Arial">来写吧。</span></div><div style="line-height:22px"><span style="font-family:Arial; color:#333333; line-height:22px"><span style="line-height:26px">User类在我的数据库中对应o_user表,表中有userID INTEGER,userName VARCHAR,userPwd VARCHAR,playerID INT , 其中playerID 为外键,对应到o_player表中的playerID。类结构如下:</span></span></div><div style="line-height:22px"><span style="font-family:Arial; color:#333333; line-height:22px"><span style="line-height:26px"></span></span><textarea readonly name="code" class="csharp">    public class User{#region 成员变量private int userID { get; set; }private String userName { get; set; }private String userPwd { get; set; }private int playerID { get; set; }#endregion#region 构造函数public User() { }public User(int uid, String uname, String upwd, int pid){this.userID = uid;this.userName = uname;this.userPwd = upwd;this.playerID = pid;}#endregion}}</textarea><br><br></div><div style="line-height:22px"><span style="font-family:Arial; color:#333333; line-height:22px"><span style="line-height:26px"><br></span></span></div><div style="line-height:22px"><p style="line-height:22px; padding-top:0px; padding-bottom:0px; margin-top:10px; margin-bottom:10px; color:rgb(51,51,51); text-indent:2em"></p><p style="line-height:22px; padding-top:0px; padding-bottom:0px; margin-top:10px; margin-bottom:10px; color:rgb(51,51,51); text-indent:2em">因为另一个Player类还没有创建 暂时这里的playerID就用一个int来代替吧,我想先看看能不能正常的插入单表数据。</p><div style="line-height:22px; color:rgb(51,51,51)"><br style="line-height:22px"></div><div style="line-height:22px; color:rgb(51,51,51)">接下来写该类的映射文件</div><div style="line-height:22px; color:rgb(51,51,51)">在Model项目里添加一个xml文件User.hbm.xml。</div><div style="line-height:22px"><div style="line-height:22px"><div style="line-height:22px"><div style="line-height:22px"><span style="color:#333333; line-height:22px"></span><div style="line-height:22px"><textarea readonly name="code" class="html">&lt;?xml version="1.0" encoding="utf-8" ?&gt;&lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"  assembly="Model" namespace="Model" default-lazy="false"&gt;&lt;class name="Model.User, Model" table="o_user"&gt;&lt;id name="userID" column="userID " type="int" unsaved-value="0"&gt;&lt;generator class="increment" /&gt;&lt;/id&gt;&lt;property name="userName" column="userPwd" unique="true" not-null="true" type="String" /&gt;&lt;property name="userPwd" column="userPwd" not-null="true" type="String" /&gt;&lt;property name="playerID" column="playerID" type="String"/&gt;&lt;/class&gt;&lt;/hibernate-mapping&gt;</textarea><br><br></div></div></div></div></div><p style="line-height:22px; padding-top:0px; padding-bottom:0px; margin-top:10px; margin-bottom:10px; color:rgb(51,51,51); text-indent:2em"></p><div style="line-height:22px"><span style="line-height:22px; color:rgb(51,51,51)">assembly="</span><span style="color:#333333; line-height:22px">Model" 这是程序集的名称</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px">namespace="Model" 这是命名空间的名称</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px">class name="Model.User, Model" 指向命名空间下的类文件,</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px">table="o_user" 指向数据库中该类对应的表名</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px">接下来id标签设置主键</span></div><div style="line-height:22px"><span style="line-height:22px; color:rgb(51,51,51)">unsaved-value判断被操作的对象究竟是一个已经持久化过的持久对象还是临时对象</span></div><div style="line-height:22px"><span style="line-height:22px; color:rgb(51,51,51)"></span><div style="line-height:22px">默认unsaved-value="null"</div><div style="line-height:22px">主键是对象类型,hebernate判断project的主键是否位null,来判断project是否已被持久化</div><div style="line-height:22px">是的话,对project对象发送save(project),</div><div style="line-height:22px">若自己设置了主键则直接生成update的sql,发送update(project),即便数据库里没有那条记录。</div><span style="line-height:22px; color:rgb(51,51,51)"></span><div style="line-height:22px">其它的值还有 none,any等 ,但主键是基本类型如int/long/double/ 的话 一般就设置为0,</div><div style="line-height:22px; color:rgb(51,51,51)">这样的话save和update操作都不会报错。</div><div style="line-height:22px; color:rgb(51,51,51)">再接下来的property标签就没有什么好说的了。最后不要忘记把这个文件的属性设置为嵌入的资源。否则出现“ failed: NHibernate.MappingException : No persister for: NHibernateSample.Domain.Entities.Customer”异常。</div><div style="line-height:22px; color:rgb(51,51,51)"><div style="line-height:22px"> DSC0001.jpg <br></div></div><div style="line-height:22px; color:rgb(51,51,51)">然后我们去UI项目中添加一个App.config文件,在其中配置hibernate和log4net等</div><div style="line-height:22px"><div style="line-height:22px"><span style="color:#333333"></span><textarea readonly name="code" class="html">&lt;?xml version="1.0" encoding="utf-8" ?&gt;&lt;configuration&gt;&lt;configSections&gt;&lt;section name="log4net"type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /&gt;&lt;section name="hibernate-configuration"type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/&gt;&lt;/configSections&gt;&lt;hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" &gt;&lt;session-factory&gt;&lt;property name="hibernate.show_sql"&gt;true&lt;/property&gt;&lt;property name="hibernate.connection.release_mode"&gt;on_close&lt;/property&gt;&lt;property name="connection.provider"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/property&gt;&lt;property name="connection.driver_class"&gt;NHibernate.Driver.SQLiteDriver&lt;/property&gt;&lt;property name="connection.connection_string"&gt;Data Source=&lt;!-- 数据库名--&gt;lstx.db;Version=3&lt;/property&gt;&lt;property name="dialect"&gt;NHibernate.Dialect.SQLiteDialect&lt;/property&gt;&lt;property name="query.substitutions"&gt;true=1;false=0&lt;/property&gt;&lt;!-- 在这里添加assembly程序集的话可以减少后面设置程序集的麻烦 它将自动映射Model下所有的配置 --&gt;&lt;mapping assembly="Model" /&gt;&lt;/session-factory&gt;&lt;/hibernate-configuration&gt;&lt;log4net&gt;&lt;appender name="console"type="log4net.Appender.ConsoleAppender, log4net"&gt;&lt;layout type="log4net.Layout.PatternLayout,log4net"&gt;&lt;param name="ConversionPattern"value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n" /&gt;&lt;/layout&gt;&lt;/appender&gt;&lt;appender name="fileAppender" type="log4net.Appender.FileAppender"&gt;&lt;file value="log-file.txt" /&gt;&lt;appendToFile value="true" /&gt;&lt;layout type="log4net.Layout.PatternLayout"&gt;&lt;conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /&gt;&lt;/layout&gt;&lt;/appender&gt;&lt;root&gt;&lt;level value="WARN" /&gt;&lt;appender-ref ref="console" /&gt;&lt;appender-ref ref="fileAppender" /&gt;&lt;/root&gt;&lt;/log4net&gt;&lt;/configuration&gt;</textarea></div></div></div></div></div><div style="line-height:22px"><span style="color:#333333; line-height:22px"><br></span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px">在DAL项目中实现对SQLite的操作访问</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px">可能大家的设计思路不一样,我只是介绍我自己的一种方案,高手可以直接跳过去。</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px">我觉得一个通用的DAO应该具备以下几种方法:</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px"></span><div style="line-height:22px"><textarea readonly name="code" class="csharp">public interface IGenericDao{Object save(Object entity);   //增加bool delete(Object entity); //删除bool deleteById(Type type, object id);//根据主键删除Object modify(Object obj);    //修改Object findById(Type type, object id);//根据主键查找IList findByCriteria(ICriteria criteria, Object obj);//QBC查询,我喜欢用,当然你也可以用别的List&lt;Object&gt; findAll(Type type);//查询所有List&lt;Object&gt; findByQueryString(String queryString);//字符串查询}</textarea><br><br></div>我先实现save方法来测试一下是否能正常对SQLlite插入数据</div><div style="line-height:22px"><span style="color:#333333; line-height:22px">首先实现IGenericDao:</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px"></span><div style="line-height:22px"> DSC0002.jpg <br></div>然后创建接口IUserDao,它的实现类UserDao继承自GenericDao.</div><div style="line-height:22px"><span style="color:#333333; line-height:22px"></span><div style="line-height:22px">using System;</div><div style="line-height:22px">namespace DAL</div><div style="line-height:22px">{</div><div style="line-height:22px">  public interface IUserDao</div><div style="line-height:22px">  {</div><div style="line-height:22px">    void regUser(Model.User user);</div><div style="line-height:22px">  }</div><div style="line-height:22px">}</div><div style="line-height:22px">到这里DAL就可以先放下了 去BLL里写业务逻辑层</div><div style="line-height:22px">在BLL里创建接口 IUserService,它的实现类UserService中有一个IUserDao类型的变量.</div><div style="line-height:22px"><div style="line-height:22px">using System;</div><div style="line-height:22px">namespace BLL</div><div style="line-height:22px">{</div><div style="line-height:22px">  public interface IUserService</div><div style="line-height:22px">  {</div><div style="line-height:22px">    void regUser(Model.User user);</div><div style="line-height:22px">  }</div><div style="line-height:22px">}</div></div><div style="line-height:22px">好BLL也写完了,最后是去UI里创建一个测试的窗体Reg.cs.</div><div style="line-height:22px"><div style="line-height:22px"> DSC0003.jpg <br></div></div><span style="line-height:22px; color:rgb(51,51,51)">namespace UI</span><div style="line-height:22px"><span style="color:#333333; line-height:22px">{</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px">  public partial class Reg : Form</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px">  {</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px">    public Reg()</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px">    {</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px">      InitializeComponent();</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px">    }</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px">    private IUserService service = new UserService();</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px"><br style="line-height:22px"></span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px">    private void btnReg_Click(object sender, EventArgs e)</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px">    {</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px">      String name = this.txtUserName.Text;</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px">      String pwd = this.txtUserPwd.Text;</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px">      int pid = Int32.Parse(this.txtPlayerID.Text);</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px">      User user = new User(0, name, pwd, pid);</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px">      service.regUser(user);</span></div><div style="line-height:22px"><span style="line-height:22px; color:rgb(51,51,51)">    }</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px">  }</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px">}</span></div></div><div style="line-height:22px"><span style="color:#333333; line-height:22px"><br style="line-height:22px"></span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px"><br style="line-height:22px"></span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px">到这里我们这个Hibernate+SQLite+MVC基本的配置就写完了</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px">来一张现在的解决方案截图;</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px"></span><div style="line-height:22px"> DSC0004.jpg <br></div>生成后的Debug目录:</div><div style="line-height:22px"><span style="color:#333333; line-height:22px"></span><div style="line-height:22px"><div style="line-height:22px"> DSC0005.jpg <br></div></div><span style="line-height:22px; color:rgb(51,51,51)">我们来插入一个User看看。</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px"></span><div style="line-height:22px"><br></div>单击注册,啊报错了。。。</div><div style="line-height:22px"><span style="color:#333333; line-height:22px"></span><div style="line-height:22px"> DSC0006.jpg <br></div><div style="line-height:22px">could not insert: [Model.User#1][SQL: INSERT INTO o_user (userPwd, userPwd, playerID, userID ) VALUES (?, ?, ?, ?)]</div><div style="line-height:22px">不难看出 是我的列名写错了 有两个userPwd,我们去User.hbm.xml中看一下,果然。</div><div style="line-height:22px">property name="userName" column="userPwd" 把这里的userPwd改成userName。再试一次。</div>这次点击注册后没有任何反应了,起码我们知道没有报错了。去数据库里看看。</div><div style="line-height:22px"><span style="color:#333333; line-height:22px">我懒得打命令 正好SQL Expert又是打开的 所以我直接进去看了。嗯 ,还真有。</span></div><div style="line-height:22px"><span style="color:#333333; line-height:22px"></span><div style="line-height:22px"> DSC0007.jpg <br></div><div style="line-height:22px">证明我们之前的配置可以正常的使用Hibernate来操作SQLite。</div><div style="line-height:22px">今天写太多了,不知道有没有人发现playerID是一个外键,但是我随便输入了一个Int值它都可以正常插入,</div><div style="line-height:22px">这样的看来外键似乎没有起到什么作用。其实我在之前就说过了,不明白的人可以回去看看。就到这里,下次见。</div></div><div><span style="color:#333333; line-height:22px"><br></span></div></wbr>


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-307693-1-1.html 上篇帖子: Android Sqlite数据库中判断某个表是否存在的SQL语句 下篇帖子: sqlite客户端安装以及一些类型和函数
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表