This repository has been archived on 2019-06-21. You can view files and clone it, but cannot push or open issues or pull requests.
go-xorm.github.io/index.html
2016-01-04 23:17:35 +08:00

391 lines
16 KiB
HTML
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>XORM - 官方博客</title>
<meta name="keywords" content="golang,orm,xorm"/>
<meta name="description" content="Xorm 是一个简单而强大的Go语言ORM框架"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<link rel="stylesheet" href="/static/css/style.css"/>
<link rel="stylesheet" href="/static/css/prism.css"/>
<script src="/static/js/jquery-2.1.4.min.js"></script>
<script type="text/javascript">var postType = "index";</script>
</head>
<body>
<aside class="cover index" style="background: url('/static/media/cover.jpg') center/cover no-repeat fixed">
<div class="profile">
<a id="avatar-link" title="link to homepage" href="/">
<img src="/static/media/author.png" alt="pugo-robot" class="avatar">
<h1 id="profile-title">XORM</h1>
</a>
<hr class="divider long">
<p class="bio">
官方博客<br/>Xorm 是一个简单而强大的Go语言ORM框架
</p>
<hr class="divider short">
<div class="navigation">
<ul class="nav">
<li role="presentation" class="active">
<a href="/">文章
</a>
</li>
<li role="presentation" class="">
<a href="/archive.html">归档
</a>
</li>
<li role="presentation" class="">
<a href="/about.html">关于
</a>
</li>
</ul>
</div>
<ul class="social">
<li><a href="/feed.xml">
<i class="fa fa-rss"></i>
</a></li>
</ul>
</div>
</aside>
<section id="main" class="index">
<div class="container">
<div id="article-list">
<article class="article">
<div class="row">
<header class="header">
<h3 class="title">
<a href="/2016/1/4/1-about-mapper.html">关于XORM中Mapper的使用</a>
</h3>
</header>
<section class="brief"><p>XORM中的Mapper是一个接口机制专门用来在程序名称和数据库名称之间进行转换。XORM库中带有三种Mapper的实现SnakeMapper, SameMapper 以及 GonicMapper。同时为了方便还提供了CacheMapper, PrefixMapper 以及 SuffixMapper这几种Mapper可以和前面的组合使用。</p>
<h1>SnakeMapper</h1>
<p>SnakeMapper是默认的映射机制他支持数据库表采用匈牙利命名法而程序中采用驼峰式命名法。下面是一些常见的映射</p>
<table>
<thead>
<tr>
<th>表中名称</th>
<th>程序名称</th>
</tr>
</thead>
<tbody>
<tr>
<td>user_info</td>
<td>UserInfo</td>
</tr>
<tr>
<td>id</td>
<td>Id</td>
</tr>
</tbody>
</table>
<h1>SameMapper</h1>
<p>SameMapper就是数据库中的命名法和程序中是相同的。那么鉴于在Go中基本上要求首字母必须大写。所以一般都是表中和程序中均采用驼峰式命名。下面是一些常见的映射</p>
<table>
<thead>
<tr>
<th>表中名称</th>
<th>程序名称</th>
</tr>
</thead>
<tbody>
<tr>
<td>UserInfo</td>
<td>UserInfo</td>
</tr>
<tr>
<td>Id</td>
<td>Id</td>
</tr>
</tbody>
</table>
<h1>GonicMapper</h1>
<p>GonicMapper是在SnakeMapper的基础上增加了特例对于常见的缩写不新增下划线处理。这个同时也符合golint的规则。下面是一些常见的映射</p>
<table>
<thead>
<tr>
<th>表中名称</th>
<th>程序名称</th>
</tr>
</thead>
<tbody>
<tr>
<td>user_info</td>
<td>UserInfo</td>
</tr>
<tr>
<td>id</td>
<td>ID</td>
</tr>
<tr>
<td>url</td>
<td>URL</td>
</tr>
</tbody>
</table>
<h1>CacheMapper</h1>
<p>CacheMapper 通过使用一个map来将已经映射过的内容保存起来下次直接查找即可获得。</p>
<h1>PrefixMapper</h1>
<p>PrefixMapper 前缀映射可以组合SnakeMapper, SameMapper或者GonicMapper来在表名或者字段名的映射完成后再新增一个固定的前缀。比如和SnakeMapper组合</p>
<pre><code class="language-Go">engine.SetTableMapper(core.NewPrefixMapper(core.SnakeMapper{}, &quot;tb_&quot;))
engine.SetColumnMapper(core.SnakeMapper{})
</code></pre>
<p>那么对于结构体<code>User</code>,数据库中对应的表名即为<code>tb_user</code>,而表中字段名比如<code>id</code>对应的程序中结构体字段名仍为<code>Id</code></p>
<h1>SuffixMapper</h1>
<p>SuffixMapper 后缀映射类似与前缀映射也可以组合SnakeMapper, SameMapper或者GonicMapper来在表名或者字段名的映射完成后再新增一个固定的前缀。比如和SnakeMapper组合</p>
<pre><code class="language-Go">engine.SetTableMapper(core.NewSuffixMapper(core.SnakeMapper{}, &quot;_table&quot;))
engine.SetColumnMapper(core.SnakeMapper{})
</code></pre>
<p>那么对于结构体<code>User</code>,数据库中对应的表名即为<code>user_table</code>,而表中字段名比如<code>id</code>对应的程序中结构体字段名仍为<code>Id</code></p>
<h1>表名和字段名分别映射</h1>
<p>在前面的例子中也看到了,这种映射可以对表名和字段名采用相同的映射,也可以表名和字段名采用不同的映射方案。</p>
<ul>
<li><p>表名和字段名使用相同映射方案</p>
<pre><code class="language-Go">engine.SetMapper(mapper)
</code></pre></li>
<li><p>表名和字段名使用不同的映射方案</p>
<pre><code class="language-Go">engine.SetTableMapper(mapper1)
engine.SetColumnMapper(mapper2)
</code></pre></li>
</ul>
<p>最后关于名称映射更多的方法可以参考XORM文档 <a href="http://gobook.io/read/go-xorm/manual-zh-cn/chapter-02/3.tags.html">使用Table和Tag改变名称映射</a></p>
</section>
<aside class="aside clearfix">
<span class="date">
<span class="year">2016</span>/
<span class="month">1</span>/
<span class="day">4</span>
</span>
<span class="tags">
<a class="tag" href="/tags/go.html">go</a>
<a class="tag" href="/tags/golang.html">golang</a>
<a class="tag" href="/tags/xorm.html">xorm</a>
<a class="tag" href="/tags/orm.html">orm</a>
<a class="tag" href="/tags/sql.html">sql</a>
<a class="tag" href="/tags/database.html">database</a>
</span>
<a class="btn btn-primary btn-lg pull-right" href="/2016/1/4/1-about-mapper.html">继续阅读</a>
</aside>
</div>
</article>
<article class="article">
<div class="row">
<header class="header">
<h3 class="title">
<a href="/2015/12/1/1-xorm-join-extends-usage.html">在Xorm中使用Join和Extends标记</a>
</h3>
</header>
<section class="brief"><p>本文主要针对对Xorm已经有了一定了解的读者如果您是第一次了解Xorm请先阅读<a href="http://xorm.io/docs">xorm操作手册</a></p>
<p>Xorm的基本操作都是比较简单的可能大家也都比较熟悉了。今天主要讲解extends标记和join的使用。通过使用join和extends可以解决许多需要级联进行的操作。</p>
<p>一般我们会针对数据库中的每一个表,建立一个对应的结构体。比如:</p>
<pre><code class="language-Go">type User struct {
Id int64
Name string
}
type Account struct {
Id int64
UserId int64 `xorm:&quot;index&quot;`
Amount int64
}
type Car struct {
Id int64
UserId int64 `xorm:&quot;index&quot;`
Type int
}
</code></pre>
<p>我们定义了三个结构体,对应数据库的三个表,我们在启动时通过:</p>
<pre><code class="language-Go">engine.Sync2(new(User), new(Account), new(Car))
</code></pre>
<p>来进行数据库结构的同步。在这个数据库结构中我们假设一个用户拥有一个Account一个用户拥有多个Car。</p>
<p>OK。复杂需求来了。</p>
<p>1我们需要获得所有的用户的姓名和对应的账户的余额</p>
<pre><code class="language-Go">type AccountUser struct {
Account `xorm:&quot;extends&quot;`
User `xorm:&quot;extends&quot;`
}
var accounts = make([]*AccountUser, 0)
engine.Table(&quot;account&quot;).Join(&quot;INNER&quot;, &quot;user&quot;, &quot;account.user_id = user.id&quot;).Find(&amp;accounts)
</code></pre>
<p>OK。这样我们就取出了user和对应的account我们通过<code>account.Account</code>可以获取到Account的信息通过<code>account.User</code>可以获取到User的信息。</p>
<p>这个是两个表Join那么如果是三个表也是类似的做法。</p>
<p>2我只需要用户名不需要其它的内容</p>
<pre><code class="language-Go">type AccountUser struct {
Account `xorm:&quot;extends&quot;`
Name string
}
var accounts = make([]*AccountUser, 0)
engine.Table(&quot;account&quot;).Join(&quot;INNER&quot;, &quot;user&quot;, &quot;account.user_id = user.id&quot;).Find(&amp;accounts)
</code></pre>
<p>其实我们代码也是差不多的但是这里我们实际上在查询数据库的时候是查询了user表的所有内容的。只是在最后赋值到结构体时按需赋值。</p>
<p>3更复杂的我们还想知道每人有几辆车。</p>
<pre><code class="language-Go">type AccountUser struct {
Account `xorm:&quot;extends&quot;`
Name string
NumCars int
}
var accounts = make([]*AccountUser, 0)
engine.Sql(&quot;select account.*, user.name, (select count(id) from car where car.user_id = user.id) as num_cars from account, user where account.user_id = user.id&quot;).Find(&amp;accounts)
</code></pre>
<p>在这样的复杂需求下我们使用了Sql函数和extends标记结合来完成这个操作。</p>
</section>
<aside class="aside clearfix">
<span class="date">
<span class="year">2015</span>/
<span class="month">12</span>/
<span class="day">1</span>
</span>
<span class="tags">
<a class="tag" href="/tags/go.html">go</a>
<a class="tag" href="/tags/golang.html">golang</a>
<a class="tag" href="/tags/xorm.html">xorm</a>
<a class="tag" href="/tags/orm.html">orm</a>
<a class="tag" href="/tags/sql.html">sql</a>
<a class="tag" href="/tags/database.html">database</a>
</span>
<a class="btn btn-primary btn-lg pull-right" href="/2015/12/1/1-xorm-join-extends-usage.html">继续阅读</a>
</aside>
</div>
</article>
<article class="article">
<div class="row">
<header class="header">
<h3 class="title">
<a href="/2015/10/1/welcome.html">Xorm博客定型</a>
</h3>
</header>
<section class="brief"><p>终于迎来了个人认为最有发展潜力的静态站点生成工具 <a href="https://github.com/go-xiaohei/pugo">Pugo</a>发布了第一个稳定版本本博客将使用其搭建将会发布关于xorm的高质量文章和新版本等信息.</p>
</section>
<aside class="aside clearfix">
<span class="date">
<span class="year">2015</span>/
<span class="month">10</span>/
<span class="day">1</span>
</span>
<span class="tags">
<a class="tag" href="/tags/go.html">go</a>
<a class="tag" href="/tags/golang.html">golang</a>
<a class="tag" href="/tags/xorm.html">xorm</a>
<a class="tag" href="/tags/orm.html">orm</a>
<a class="tag" href="/tags/sql.html">sql</a>
<a class="tag" href="/tags/database.html">database</a>
</span>
<a class="btn btn-primary btn-lg pull-right" href="/2015/10/1/welcome.html">继续阅读</a>
</aside>
</div>
</article>
<article class="article">
<div class="row">
<header class="header">
<h3 class="title">
<a href="/2014/1/1/1-7-weapons.html">XORM的七种武器</a>
</h3>
</header>
<section class="brief"><p><a href="https://github.com/lunny/xorm">xorm</a> 是一个简单而强大的 Go 语言开源 ORM 库. 通过它可以使数据库操作非常简便。</p>
<h1>xorm 的七种武器</h1>
<p><a href="https://github.com/lunny/xorm">xorm</a> 是一个简单而强大的 Go 语言开源 ORM 库. 通过它可以使数据库操作非常简便。</p>
<p>了解过 Go 的人可能会有疑问Go 已经提供了 database/sql 接口,操作各种数据库接口都一致了,还有必要再使用 ORM 吗?也有人觉得对于复杂 SQL 语句ORM 是无法应付的。</p>
<p>是的,<a href="https://github.com/lunny/xorm">xorm</a> 不是为了取代 SQL它甚至可以和 SQL 混用,它是在 databse/sql 接口的基础之上提供了更多的特性。我们将这些功能和特性比喻成七种武器,来帮助开发者快速的完成数据库的操作。</p>
</section>
<aside class="aside clearfix">
<span class="date">
<span class="year">2014</span>/
<span class="month">1</span>/
<span class="day">1</span>
</span>
<span class="tags">
<a class="tag" href="/tags/go.html">go</a>
<a class="tag" href="/tags/golang.html">golang</a>
<a class="tag" href="/tags/xorm.html">xorm</a>
<a class="tag" href="/tags/orm.html">orm</a>
<a class="tag" href="/tags/sql.html">sql</a>
<a class="tag" href="/tags/database.html">database</a>
</span>
<a class="btn btn-primary btn-lg pull-right" href="/2014/1/1/1-7-weapons.html">继续阅读</a>
</aside>
</div>
</article>
<div class="article-pager">
<span class="current">Page 1 of 1</span>
</div>
</div>
<footer id="footer">
<div class="container text-center">
<p class="left">© 2015 XORM.
<a href="http://creativecommons.org/licenses/by/3.0/">Some rights reserved </a> |
<a href="/feed.xml">Feed</a> |
<a href="/sitemap.xml">Sitemap</a>
</p>
<p class="right">Powered by <a href="https://github.com/go-xiaohei/pugo">PuGo 0.9.0.0101</a>. Theme by Uno.
</p>
</div>
</footer>
</div>
</section>
<script>
$(function () {
$(".navigation li.active a").on("click", function () {
var $cover = $('.cover');
var $main = $('#main');
$cover.hasClass("index") ? $cover.removeClass("index") && $main.show() : $cover.addClass("index") && $main.hide();
return false;
});
})
</script>
<script src="/static/js/prism.min.js"></script>
<script>
$(document).ready(function () {
$("pre code").addClass("line-numbers")
});
</script>
</body>
</html>