创建项目
play new yabe
What is the application name? [yabe] Blog Engine
play eclipsify yabe
play run yabe
Eclipse引入项目
file -> import -> General ->Existing Projects into Workspaces -> select root directory ...
设置数据库
选择一个内存数据库
打开yabe/conf/application.conf,去掉下面这行的注释
db=mem #使用内存数据库,使用HSQL
【hsql 数据库是一款纯Java编写的免费数据库,体积小,才563kb。仅一个hsqldb.jar文件就包括了数据库引擎,数据库驱动,还有其他用户界面操作等内容。纯Java设计,又支持 SQL99,SQL2003大部分的标准】
************************************************************************************************************
增加模型User
package models; import javax.persistence.Entity; import play.db.jpa.Model; @Entity public class User extends Model { public String email; public String password; public String fullname; public boolean isAdmin; public User(String email,String password, String fullname) { this.email = email; this.password = password; this.fullname = fullname; } }
使用JUNIT进行单元测试
打开命令行,切换到测试模式
play test yabe
play提供了默认的测试文件,打开yabe\test\BasicTest.java
删除aVeryImportantThingToTest,新建一个测试
import org.junit.*; import java.util.*; import play.test.*; import models.*; /** * 测试单元 extends UnitTest * @author lenovo * */ public class BasicTest extends UnitTest { @Test public void createAndRetriveUser() { //Create a User and save new User("zs@162.com","******","ZS").save(); //retrieve User by emial User user1 = User.find("email", "zs@162.com").first(); //两种写法都可以 匹配email="zs@162.com"的User对象 User user2 = User.find("byEmail", "zs@162.com").first(); assertNotNull(user1); assertNotNull(user2); assertEquals(user1, user2); assertEquals("ZS", user1.fullname); assertEquals("ZS", user2.fullname); } }
打开http://localhost:9000/@tests
运行BasicTest,即上面写的测试单元,点击Start ! 进行测试
结果应该为绿色!
接下来测试User的email和password是否正确的方法
通过email和password两个条件来找到User对象
打开User.java,添加connect():
package models; import javax.persistence.Entity; import play.db.jpa.Model; @Entity public class User extends Model { public String email; public String password; public String fullname; public boolean isAdmin; public User(String email,String password, String fullname) { this.email = email; this.password = password; this.fullname = fullname; } /** * 联合email和password两个条件查询User * @param email * @param password * @return */ public static User connect(String email, String password) { return find("byEmailAndPassword", email, password).first(); } }
在BasicTest.java里添加测试方法
import org.junit.*; import java.util.*; import play.test.*; import models.*; /** * 测试单元 extends UnitTest * @author lenovo * */ public class BasicTest extends UnitTest { /** * 测试用户的创建和查找 */ @Test public void createAndRetriveUser() { //Create a User and save new User("zs@162.com","******","ZS").save(); //retrieve User by emial User user1 = User.find("email", "zs@162.com").first(); //两种写法都可以 匹配email="zs@162.com"的User对象 User user2 = User.find("byEmail", "zs@162.com").first(); assertNotNull(user1); assertNotNull(user2); assertEquals(user1, user2); assertEquals("ZS", user1.fullname); assertEquals("ZS", user2.fullname); } /** * 测试联合条件查询 */ @Test public void tryConnectUser() { //Create a user and save it new User("zs@1.com", "123", "zhangsan").save(); //Test assertNotNull(User.connect("zs@1.com", "123")); assertNull(User.connect("zs@1.com", "234")); assertNull(User.connect("zs@2.com", "234")); } }
测试BasicTest项,应该全部通过!
增加模型Post
Post类作为博客的实体类,与User之间为多对一的关系
package models; import java.util.Date; import javax.persistence.Entity; import javax.persistence.Lob; import javax.persistence.ManyToOne; import play.db.jpa.Model; @Entity public class Post extends Model { public String title; public Date postedAt; //@Lob标注:声明这是一个超大文本数据类型,用于存储发布的博客内容 @Lob public String content; //@ManyToOne:声明Post与User之间是多对一的关系 //一个用户可以发布多个博客,一个博客只能被一个用户所发布 @ManyToOne public User author; public Post(String title, String content, User author) { this.title = title; this.content = content; this.author = author; this.postedAt = new Date(); } }
对Post类进行测试
import org.junit.*; import java.util.*; import play.test.*; import models.*; /** * 测试单元 extends UnitTest * @author lenovo * */ public class BasicTest extends UnitTest { /** * 清空数据库中的数据,释放内存空间 * Fixtures帮助在测试期间管理数据库 */ @Before public void setup() { Fixtures.deleteDatabase(); } /** * 测试用户的创建和查找 */ @Test public void createAndRetriveUser() { //Create a User and save new User("zs@162.com","******","ZS").save(); //retrieve User by emial User user1 = User.find("email", "zs@162.com").first(); //两种写法都可以 匹配email="zs@162.com"的User对象 User user2 = User.find("byEmail", "zs@162.com").first(); assertNotNull(user1); assertNotNull(user2); assertEquals(user1, user2); assertEquals("ZS", user1.fullname); assertEquals("ZS", user2.fullname); } /** * 测试联合条件查询 */ @Test public void tryConnectUser() { //Create a user and save it new User("zs@1.com", "123", "zhangsan").save(); //Test assertNotNull(User.connect("zs@1.com", "123")); assertNull(User.connect("zs@1.com", "234")); assertNull(User.connect("zs@2.com", "234")); } /** * 测试Post类 */ @Test public void createPost() { //Create a User and save it User Mike = new User("ls@1.com", "111", "Mike").save(); //Create 2 Post new Post("First Blog", "first", Mike).save(); new Post("Second Blog", "second", Mike).save(); //测试是否成功创建了2个Post对象 assertEquals(2, Post.count()); //获取lisi发布的所有博客 List<Post> posts = Post.find("byAuthor", Mike).fetch(); assertEquals(2, posts.size()); assertNotNull(posts.get(0)); assertNotNull(posts.get(1)); assertEquals(Mike, posts.get(0).author); assertEquals("First Blog", posts.get(0).title); assertEquals("second", posts.get(1).content); assertNotNull(posts.get(1).postedAt); } }
增加模型Comment
Comment 与 Post 之间为多对一的关系,多条评论对应一篇博客
package models; import java.util.Date; import javax.persistence.Entity; import javax.persistence.Lob; import javax.persistence.ManyToOne; import play.db.jpa.Model; @Entity public class Comment extends Model { public String author; public Date postedAt; @Lob public String content; //一篇博客对应多条评论,一个评论属于一篇博客 //评论与博客的关系:多对一 @ManyToOne public Post post; public Comment(String author, String content, Post post) { super(); this.author = author; this.content = content; this.post = post; this.postedAt = new Date(); } }
修改模型Post,增加Comment属性
注:上面的User类也可以持有一个关于Post的集合,方法与此类似。
package models; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.Lob; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import play.db.jpa.Model; @Entity public class Post extends Model { public String title; public Date postedAt; //@Lob标注:声明这是一个超大文本数据类型,用于存储发布的博客内容 @Lob public String content; //@ManyToOne:声明Post与User之间是多对一的关系 //一个用户可以发布多个博客,一个博客只能被一个用户所发布 @ManyToOne public User author; //1篇博客对应多个评论 //删除某篇博客,则级联删除其评论 @OneToMany(mappedBy="post", cascade=CascadeType.ALL) public List<Comment> comments; public Post(String title, String content, User author) { this.comments = new ArrayList<Comment>(0); this.title = title; this.content = content; this.author = author; this.postedAt = new Date(); } /** * 在Post中实现评论的添加保存操作 * 同时更新Post所持有的Comment的集合! */ public Post addComment(String author, String content) { //保存对方 Comment newComment = new Comment(author, content, this).save(); //把对方加入到自己管理的集合中 this.comments.add(newComment); //同步到数据库 this.save(); return this; } }
所有的测试代码
import org.junit.*; import java.util.*; import play.test.*; import models.*; /** * 测试单元 extends UnitTest * @author lenovo * */ public class BasicTest extends UnitTest { /** * 清空数据库中的数据,释放内存空间 * Fixtures帮助在测试期间管理数据库 */ @Before public void setup() { Fixtures.deleteDatabase(); } /** * 测试用户的创建和查找 */ @Test public void createAndRetriveUser() { //Create a User and save new User("zs@162.com","******","ZS").save(); //retrieve User by emial User user1 = User.find("email", "zs@162.com").first(); //两种写法都可以 匹配email="zs@162.com"的User对象 User user2 = User.find("byEmail", "zs@162.com").first(); assertNotNull(user1); assertNotNull(user2); assertEquals(user1, user2); assertEquals("ZS", user1.fullname); assertEquals("ZS", user2.fullname); } /** * 测试联合条件查询 */ @Test public void tryConnectUser() { //Create a user and save it new User("zs@1.com", "123", "zhangsan").save(); //Test assertNotNull(User.connect("zs@1.com", "123")); assertNull(User.connect("zs@1.com", "234")); assertNull(User.connect("zs@2.com", "234")); } /** * 测试Post类 */ @Test public void createPost() { //Create a User and save it User Mike = new User("ls@1.com", "111", "Mike").save(); //Create 2 Post new Post("First Blog", "first", Mike).save(); new Post("Second Blog", "second", Mike).save(); //测试是否成功创建了2个Post对象 assertEquals(2, Post.count()); //获取lisi发布的所有博客 List<Post> posts = Post.find("byAuthor", Mike).fetch(); assertEquals(2, posts.size()); assertNotNull(posts.get(0)); assertNotNull(posts.get(1)); assertEquals(Mike, posts.get(0).author); assertEquals("First Blog", posts.get(0).title); assertEquals("second", posts.get(1).content); assertNotNull(posts.get(1).postedAt); } /** * 测试Post与User的多对一关系 */ @Test public void testPost2User() { User Mike = new User("ls@1.com", "111", "Mike").save(); Mike.addPost("First Blog", "first"); Mike.addPost("Second Blog", "second"); assertNotNull(Mike); assertEquals(2, Post.count()); //从新查询User Mike = User.connect("ls@1.com", "111"); //直接从User中获取Post的集合 assertEquals(2, Mike.posts.size()); assertEquals("first", Mike.posts.get(0).content); assertEquals("second", Mike.posts.get(1).content); } /** * 测试Comment类 * 单向,由Comment得到Post */ @Test public void testComment() { User Mike = new User("ls@1.com", "111", "Mike").save(); Post post = new Post("First Blog", "first", Mike).save(); new Comment("jeff", "nice post", post).save(); new Comment("henrry", "good post", post).save(); //获取博客的所有评论 List<Comment> comments = Comment.find("byPost", post).fetch(); assertEquals(2, comments.size()); Comment firstComment = comments.get(0); assertNotNull(firstComment); assertEquals("jeff", firstComment.author); assertEquals("nice post", firstComment.content); assertNotNull(firstComment.postedAt); Comment secondComment = comments.get(1); assertNotNull(secondComment); assertEquals("henrry", secondComment.author); assertEquals("good post", secondComment.content); assertNotNull(firstComment.postedAt); } /** * 测试Comment与Post的多对一关系 * 双向,由Post直接得到与此关联的Comment集合 */ @Test public void testComment2PostRelation() { //User User bob = new User("bob@123.com","111","Bob well").save(); //Post Post post = new Post("First post","hello kelly",bob).save(); //Comment post.addComment("jeff", "Nice Post!"); post.addComment("Tom", "I knew that!"); assertEquals(1, User.count()); assertEquals(1, Post.count()); assertEquals(2, Comment.count()); //Retrieve Bob's post post = Post.find("byAuthor", bob).first(); assertNotNull(post); //Navigation to comments assertEquals(2, post.comments.size()); assertEquals("jeff", post.comments.get(0).author); //delete Post post.delete(); //check that all commonts have been deleted assertEquals(1, User.count()); assertEquals(0, Post.count()); assertEquals(0, Comment.count()); } }
相关推荐
矢部 另一个博客引擎-Curso Play 1.3.1 看到
矢部 另一个博客引擎(Play Framework 1.3.x)
yabe:另一个后端库
包括三个演示服务器应用程序:通用服务器,壁挂式房间控制器模拟器,天气服务器(从中获取数据互联网网络服务)。 包括许多示例C#源代码。 Raspberry Pi服务器示例代码,也已在Beaglebone和Intel Edison上进行了...
学习BACnet/IP协议所使用的Yabe和VTS工具
BACNET测试工具、可以扫描所有的BACnet设备,点位读取。
这是一个小组摸索了近一个月的成果,资料太少了,走了太多弯路,希望BACNet尽快普及起来
Currently supports both BACnet IPv4, IPv6 + BACnet MSTP + BACnet PTP + BACnet Ethernet. Basic functions for read, write, read multiple, write multiple, iam, whois, subscribeCOV, notify, WriteFile, ...
yabe_sql 剧本1.3.x教程紧随其后
一些常用的BACnet点位扫描工具包括Yabe,BACnetscan 等。 BACnet主站模拟工具:主站模拟工具用于模拟BACnet主站的功能,以便测试和验证设备的响应和行为。它们允许开发人员发送和接收BACnet命令和数据,并模拟不同的...
语言:English 更好的书签popup 功能: 1.左键单击在新选项卡中打开书签。 2.中间单击或“Ctrl”单击单击“在”新建后台“选项卡中打开书签。 3.... 4.... 5.... v1.2.1 - 节点删除后还原滚动位置。 v1.2.0....
矢部网上商城
迅饶BACnet客户端免费调试助手—BACnetScanzip,BACnetSCAN用于扫描BACnet IP 和BACnet MSTP设备,导出数据到XLS和.csv文档进行编辑,导入到X2OPC,X2Modbus 非常方便
BACnet开发资料与调试工具-附件资源
BACnetScan是由上海迅饶自动化科技有限公司自主研发的BACnet客户端扫描软件 ,该软件同时具备BACnetIP和BACnetMS/TP扫描功能,并且具有读属性、写当前值属性、和导出EXCEL等功能。在PC上安装此软件,通过IP协议和...
在Hiroshi Yabe等提出的新拟牛顿方程基础上,给出一类新拟牛顿算法(称为MBFGS算法),同时在一定的假设条件下,结合Wolfe搜索准则,证明了MBFGS算法具有全局收敛性,并进行了数值试验,结果表明,对于一般的无约束...