本文简单介绍Grails
(1)
(2)
grails create-domain-class Member
grails create-domain-class Team
grails create-domain-class Membership
(3)
class Member {
String name
static hasMany =
static mappedBy =
def teams()
{
return memberships.collect{it.team}
}
List addToTeams(Team team) {
Membership.link(this, team)
return teams()
}
List removeFromTeams(Team team) {
Membership.unlink(this, team)
return teams()
}
}
(4)
class Team {
String name
Member member
static belongsTo = Member
static hasMany =
List members() {
return memberships.collect{it.member}
}
List addToMembers(Member member) {
Membership.link(member, this)
return members()
}
List removeFromMembers(Member member) {
Membership.unlink(member, this)
return memberes()
}
}
在多对多的映射中,只需在两端都指定hasMany
(5)
在hibernate
class Membership {
Member member
Team team
static Membership link(member, team) {
def m = Membership.findByMemberAndTeam(member, team)
if (!m)
{
m = new Membership()
member?.addToMemberships(m)
team?.addToMemberships(m)
m.save()
}
return m
}
static void unlink(member, team) {
def m = Membership.findByMemberAndTeam(member, team)
if (m)
{
member?.removeFromMemberships(m)
team?.removeFromMemberships(m)
m.delete()
}
}
}
(6)
grails generate-all Member
grails generate-all Team
grails generate-all Membership
(7)
在浏览器下输入:http://localhost:8080/GormDemo2
欢迎光临 黑色海岸线论坛 (http://bbs.thysea.com/) | Powered by Discuz! 7.2 |