- 浏览: 2567488 次
- 性别:
- 来自: 成都
-
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Playframework(5)Java Project and SQL Database
8. Accessing an SQL Database
Configuring JDBC connection pools
conf/application.conf
# Default database configuration
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
#Orders database
db.orders.driver=org.h2.Driver
db.orders.url="jdbc:h2:mem:order"
#customers database
db.customers.driver=org.h2.Driver
db.customers.url="jdbc:h2:mem:customers"
Accessing the JDBC database: Database ds = DB.getDatasource();
Obtaining a JDBC connection Connection connection = DB.getConnection();
Exposing the datasource through JNDI
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:men:play"
db.default.jndiName=DefaultDS
Using Ebean ORM
http://www.avaje.org/
conf/application.conf:
ebean.default="models.*"
ebean.orders="models.Order, models.OrderItem"
ebean.customer="models.Customer, models.Address"
Using the play.dv.ebean.Model Superclass
@Entity
public class Task extends Model{
@Id
@Constraints.Min(10)
public Long id;
@constraints.Required
public String name;
public boolean done;
@Format.DateTime(pattern="dd/MM/yyyy")
public Data dueDate = new Date();
public static Finder<Long,Task> find = new Finder<Long, Task>(
Long.class, Task.class
);
}
Allmost like hibernate
Transactional actions
We need to enable the helper for that on Ebean.
Intergrating with JPA
Exposing the datasource through JNDI
Adding a JPA implementation to my project
"org.hibernate" % "hibernate-entitymanager" % "3.6.9.Final"
Creating a persistence unit
conf/META-INF/persistence.xml
Annotating JPA actions with @Transactional
Using the play.db.jpa.JPA helper
public static Company findById(Long id){
return JPA.em().find(Company.class, id);
}
9 The Play Cache API
The default cache API uses EHCache. play.cache.Cache
Cache.set("item.key", frontPageNews);
News news = Cache.get("item.key");
Remove the cache, Cache.set("item.key", null, 0) Cache.remove("item.key")
Caching HTTP Responses
@Cached("homePage")
public static Result index(){
return ok("Hello world");
}
10. Calling WebService
play.libs.WS provides a way to make asynchronous HTTP calls.
We will use promise then.
11. Integration with Akka
12. Internationalization
13. Application Global Settings
The Global Object
We need to define a global object in the root package
import play.*;
public class Global extends GlobalSettings{
}
Intercepting application start-up and shutdown
public class Global extends GlobalSettings{
public void onStart(Application app){
Logger.info("Application has started");
}
public void onStop(Application app){
Logger.info("Application shutdown...");
}
}
Providing an application error page
When an exception occurs in my application, the onError operation will be called.
public class Global extends GlobalSettings{
public Result onError(Throwable t){
return internalSeverError(
views.html.errorPage(t)
);
}
}
Handling action not found
If the framework doesn't find an action method for a request, the onHandlerNotFound operation will be called:
public class Global extends GlobalSettings{
public Result onHandlerNotFound(String uri){
return notFound(
views.html.pageNotFound(uri)
);
}
}
The onBadRequest operation will be called if a route was found, but it was not possible to bind the request parameters.
public class Global extends GlobalSettings{
public Result onBadRequest(String uri, String error){
return badRequest("Don't try to hack the URI!");
}
}
Intercepting Requests
One important aspect of the GlobalSettings class is that it provides a way to intercept requests and execute business logic before a request is dispatched to an action.
public class Global extends GlobalSettings{
public Action onRequest(Request request, Method actionMethod){
System.out.println("before each request…" + request.toString());
return super.onRequest(request, actionMethod);
}
}
14. Testing Your Application
Using JUnit and Running a fake application
fakeApplication(inMemoryDatabase())
Writing functional tests
Testing a template
@Test
public void renderTemplate(){
Content html = views.html.index.render("Coco");
assertThat(contentType(html)).isEqualTo("text/html");
assertThat(ContentAsString(html)).contains("Coco");
}
Testing your controllers
@Test
public void callIndex(){
Result result = callAction(
controllers.routes.ref.Application.index("Kiko");
);
assertThat(status(result)).isEqualTo(OK);
assertThat(contentType(result)).isEqualTo("text/html");
assertThat(charset(result)).isEqualTo("utf-8");
assertThat(contentAsString(result)).contains("Hello Kiki");
}
Testing the router
Instead of calling the Action, we can let the Router do it.
@Test
public void badRoute(){
Result result = routeAndCall(fakeRequest(GET, "/xx/Kiki"));
assertThat(result).isNull();
}
Starting a real HTTP server
@Test
public void testInServer(){
running(testServe(3333), new Callback0(){
assertThat(
WS.url("http://localhot:3333").get().get().status
).isEqualTo(OK);
});
}
Testing from within a web browser
Selenium WebDriver
@Test
public void runInBrowser(){
running(testServer(3333), HTMLUNIT, new Callback<TestBrowser>(){
public void invoke(TestBrowser browser){
browser.goTo("http://localhost:3333");
assertThat(browser.$("#title").getTexts().get(0)).isEqualTo("Hello Guest");
browser.$("a").click();
assertThat(browser.url()).isEqualTo("http://localhost:3333/Coco");
assertThat(browser.$("#title", 0).getText()).isEqualTo("Hello Coco");
}
})
}
References:
http://www.playframework.org/documentation/2.0.4/JavaHome
http://www.playframework.org/documentation/2.0.4/JavaDatabase
8. Accessing an SQL Database
Configuring JDBC connection pools
conf/application.conf
# Default database configuration
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
#Orders database
db.orders.driver=org.h2.Driver
db.orders.url="jdbc:h2:mem:order"
#customers database
db.customers.driver=org.h2.Driver
db.customers.url="jdbc:h2:mem:customers"
Accessing the JDBC database: Database ds = DB.getDatasource();
Obtaining a JDBC connection Connection connection = DB.getConnection();
Exposing the datasource through JNDI
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:men:play"
db.default.jndiName=DefaultDS
Using Ebean ORM
http://www.avaje.org/
conf/application.conf:
ebean.default="models.*"
ebean.orders="models.Order, models.OrderItem"
ebean.customer="models.Customer, models.Address"
Using the play.dv.ebean.Model Superclass
@Entity
public class Task extends Model{
@Id
@Constraints.Min(10)
public Long id;
@constraints.Required
public String name;
public boolean done;
@Format.DateTime(pattern="dd/MM/yyyy")
public Data dueDate = new Date();
public static Finder<Long,Task> find = new Finder<Long, Task>(
Long.class, Task.class
);
}
Allmost like hibernate
Transactional actions
We need to enable the helper for that on Ebean.
Intergrating with JPA
Exposing the datasource through JNDI
Adding a JPA implementation to my project
"org.hibernate" % "hibernate-entitymanager" % "3.6.9.Final"
Creating a persistence unit
conf/META-INF/persistence.xml
Annotating JPA actions with @Transactional
Using the play.db.jpa.JPA helper
public static Company findById(Long id){
return JPA.em().find(Company.class, id);
}
9 The Play Cache API
The default cache API uses EHCache. play.cache.Cache
Cache.set("item.key", frontPageNews);
News news = Cache.get("item.key");
Remove the cache, Cache.set("item.key", null, 0) Cache.remove("item.key")
Caching HTTP Responses
@Cached("homePage")
public static Result index(){
return ok("Hello world");
}
10. Calling WebService
play.libs.WS provides a way to make asynchronous HTTP calls.
We will use promise then.
11. Integration with Akka
12. Internationalization
13. Application Global Settings
The Global Object
We need to define a global object in the root package
import play.*;
public class Global extends GlobalSettings{
}
Intercepting application start-up and shutdown
public class Global extends GlobalSettings{
public void onStart(Application app){
Logger.info("Application has started");
}
public void onStop(Application app){
Logger.info("Application shutdown...");
}
}
Providing an application error page
When an exception occurs in my application, the onError operation will be called.
public class Global extends GlobalSettings{
public Result onError(Throwable t){
return internalSeverError(
views.html.errorPage(t)
);
}
}
Handling action not found
If the framework doesn't find an action method for a request, the onHandlerNotFound operation will be called:
public class Global extends GlobalSettings{
public Result onHandlerNotFound(String uri){
return notFound(
views.html.pageNotFound(uri)
);
}
}
The onBadRequest operation will be called if a route was found, but it was not possible to bind the request parameters.
public class Global extends GlobalSettings{
public Result onBadRequest(String uri, String error){
return badRequest("Don't try to hack the URI!");
}
}
Intercepting Requests
One important aspect of the GlobalSettings class is that it provides a way to intercept requests and execute business logic before a request is dispatched to an action.
public class Global extends GlobalSettings{
public Action onRequest(Request request, Method actionMethod){
System.out.println("before each request…" + request.toString());
return super.onRequest(request, actionMethod);
}
}
14. Testing Your Application
Using JUnit and Running a fake application
fakeApplication(inMemoryDatabase())
Writing functional tests
Testing a template
@Test
public void renderTemplate(){
Content html = views.html.index.render("Coco");
assertThat(contentType(html)).isEqualTo("text/html");
assertThat(ContentAsString(html)).contains("Coco");
}
Testing your controllers
@Test
public void callIndex(){
Result result = callAction(
controllers.routes.ref.Application.index("Kiko");
);
assertThat(status(result)).isEqualTo(OK);
assertThat(contentType(result)).isEqualTo("text/html");
assertThat(charset(result)).isEqualTo("utf-8");
assertThat(contentAsString(result)).contains("Hello Kiki");
}
Testing the router
Instead of calling the Action, we can let the Router do it.
@Test
public void badRoute(){
Result result = routeAndCall(fakeRequest(GET, "/xx/Kiki"));
assertThat(result).isNull();
}
Starting a real HTTP server
@Test
public void testInServer(){
running(testServe(3333), new Callback0(){
assertThat(
WS.url("http://localhot:3333").get().get().status
).isEqualTo(OK);
});
}
Testing from within a web browser
Selenium WebDriver
@Test
public void runInBrowser(){
running(testServer(3333), HTMLUNIT, new Callback<TestBrowser>(){
public void invoke(TestBrowser browser){
browser.goTo("http://localhost:3333");
assertThat(browser.$("#title").getTexts().get(0)).isEqualTo("Hello Guest");
browser.$("a").click();
assertThat(browser.url()).isEqualTo("http://localhost:3333/Coco");
assertThat(browser.$("#title", 0).getText()).isEqualTo("Hello Coco");
}
})
}
References:
http://www.playframework.org/documentation/2.0.4/JavaHome
http://www.playframework.org/documentation/2.0.4/JavaDatabase
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 492NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 351Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 449Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 401Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 496NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 438Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 346Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 262GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 463GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 336GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 322Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 330Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 306Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 320Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 307NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 272Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 586NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 279Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 388Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 387Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
5. **框架应用**:项目中的“projectok_xx”可能使用了某种Java游戏服务器框架,例如Spring Boot、Netty或Play Framework。这些框架能简化服务器开发,提供自动配置、依赖注入等功能。 6. **游戏逻辑处理**:服务器...
MyBatis, on the other hand, simplifies database operations by mapping SQL queries to Java methods, enabling seamless integration with the application logic. JSP (JavaServer Pages), a core technology...
Play Framework是基于Java和Scala的全栈Web框架,支持HTTP、WebSocket和HTTP/2协议。它采用模型-视图-控制器(MVC)架构模式,提倡TDD(测试驱动开发)并提供了强大的路由系统。Play的异步I/O模型使得它在处理高并发...
车牌识别项目
python、yolo
Ollama本地模型对话、选择本地文件、本地图像对话 1、新增根据聊天记录回复的功能。 2、优化了部分ViewModel,将对应Model字段、属性移到Model中,方便后续扩展。 3、新增读取外部数据回复问题功能,目前支持txt文件。 4、新增添加图片提问题功能,模型需要支持视觉(如:minicpm-v:latest)。 5、优化了类结构,创建对应的Model(MainWindowModel),将所有字段、属性移到Model。 6、新增聊天记录窗体,修改了窗体加载时,加载聊天记录的功能。将其拆分成一个视图。 7、移除了折叠栏功能,更新为Grid区域的显示与隐藏。 将聊天记录列表从主窗体中分离)。 8、更新记录文件加载功能,显示提问日期。 新增选择文件类型设置预览图标。 9、新增功能,新聊天后第一次提问完成后,保存的记录刷新到记录列表、记录删除功能。 10、新增功能,创建新窗体判断显示Ollama服务运行状态。
车牌识别项目
人工智能、大语言模型相关学习资料
车牌识别项目
图像处理项目实战
P+F安全栅组态软件
图像处理项目实战
图像处理项目实战
车牌识别项目
COMBAT FURY.7z
车牌识别项目
系统选用B/S模式,后端应用springboot框架,前端应用vue框架, MySQL为后台数据库。 本系统基于java设计的各项功能,数据库服务器端采用了Mysql作为后台数据库,使Web与数据库紧密联系起来。 在设计过程中,充分保证了系统代码的良好可读性、实用性、易扩展性、通用性、便于后期维护、操作方便以及页面简洁等特点。
车牌识别项目
这是第2402节课的内容,作为复习资料
采用最新OCR引擎结合优化算法,使得识别性能极大提升!给广大文字工作者带来了福音......