play framework 连接数据库, 查询数据。简单的表单提交

本文档介绍如何在Play Framework项目中连接数据库,处理表单提交,并解决相关错误。首先,通过修改routes文件并添加新路径,然后在Application.java中添加对应的方法。接着,创建实体类User并实现CRUD操作。在视图中,使用Scala语法和表单库展示表单,并处理从控制器传递的数据。当遇到错误时,完善Application.java的方法并导入所需包。此外,需要配置application.conf以连接数据库,并在build.sbt中添加MySQL依赖。

创建一个简单的helloword之后修改routes文件,添加三条新路径,如果现在就启动会报错,

 

# Home page
GET     /                           controllers.Application.index()

GET     /jump                       controllers.Application.jump()
GET     /addUser                    controllers.Application.addUser()
GET     /findUserList               controllers.Application.findUserList()
POST    /deleteU/:id/delete         controllers.Application.deleteU(id: Integer)

# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               controllers.Assets.at(path="/public", file)

 

这时候需要在Application.java中添加三个对应的方法,暂时不做任何处理直接返回页面(如果访问http://localhost:9000 会一直在routes找 /  进入index方法,这时候想跳到其他方法可以用redirect  )

 

 public static Result index() {
    	System.out.println("进入index");
        return redirect(routes.Application.jump());
    }

    public static Result jump(){
    	System.out.println("进入jump成功!");
    	 return TODO;
    }

    public static Result addUser(){
    	return TODO;
    }
    public static Result deleteU(int id){
    	return TODO;
    }
    public static Result findUserList(){
    	return TODO;
    }

model中添加实体类User.java  在实体类中添加静态的增删改查方法,方便后面用到

 

 

package models;

import 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值