Grails-无法使用Mysql执行选择位置
我试图在grails中执行一个SELECTWHERE查询来实现一个登录页面。我不应该使用PHP或Spring进行数据验证 user.groovyGrails-无法使用Mysql执行选择位置,mysql,select,grails,groovy,Mysql,Select,Grails,Groovy,我试图在grails中执行一个SELECTWHERE查询来实现一个登录页面。我不应该使用PHP或Spring进行数据验证 user.groovy package loginmysql class UserController { def UserService def index() { UserService.createTables(); //UserService.insertToTables() // upon Inserting, must not
package loginmysql
class UserController {
def UserService
def index() {
UserService.createTables();
//UserService.insertToTables() // upon Inserting, must not be inserted twice
}
def login() {
UserService.searchLoginInput(params.userName,params.password)
redirect(action: "index") // return to index page again and display login message*/
}
}
package loginmysql
import grails.transaction.Transactional
import groovy.sql.Sql
@Transactional
class UserService {
def dataSource
def searchLoginInput(String input_user, String input_pass) {
def sql = new Sql(dataSource)
def sql2 = new Sql(dataSource)
int userNum = 0
sql.eachRow('SELECT userName FROM users WHERE userName=$input_user') { row ->
if (input_user == row.userName) {
sql2.eachRow('SELECT password FROM users WHERE password=$input_pass') { row2 ->
if (input_pass == row2.password) {
flash.message = " login succeded!!!"
session.user = input_user // keep info on who connected
userNum++
} else {
flash.message = "login failed!!!"
}
}
}
}
if (userNum==0) flash.message = "Username not found!!!"
}
def createTables(){
def sql = new Sql(dataSource)
sql.execute '''
CREATE TABLE IF NOT EXISTS users (
userId INTEGER AUTO_INCREMENT PRIMARY KEY NOT NULL,
userName VARCHAR(50) UNIQUE,
password VARCHAR(100)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
'''
}
def insertToTables(){
def sql = new Sql(dataSource)
def params = ['vagg77', 'pass']
sql.execute 'INSERT INTO users (userName, password) VALUES (?, ?)', params
params = ['mary', 'pass']
sql.execute 'INSERT INTO users (userName, password) VALUES (?, ?)', params
}
}
软件包登录mysql
class User {
String userName
String password
static constraints = {
userName (unique:true)
password (password:true)
}
}
userController.groovy
package loginmysql
class UserController {
def UserService
def index() {
UserService.createTables();
//UserService.insertToTables() // upon Inserting, must not be inserted twice
}
def login() {
UserService.searchLoginInput(params.userName,params.password)
redirect(action: "index") // return to index page again and display login message*/
}
}
package loginmysql
import grails.transaction.Transactional
import groovy.sql.Sql
@Transactional
class UserService {
def dataSource
def searchLoginInput(String input_user, String input_pass) {
def sql = new Sql(dataSource)
def sql2 = new Sql(dataSource)
int userNum = 0
sql.eachRow('SELECT userName FROM users WHERE userName=$input_user') { row ->
if (input_user == row.userName) {
sql2.eachRow('SELECT password FROM users WHERE password=$input_pass') { row2 ->
if (input_pass == row2.password) {
flash.message = " login succeded!!!"
session.user = input_user // keep info on who connected
userNum++
} else {
flash.message = "login failed!!!"
}
}
}
}
if (userNum==0) flash.message = "Username not found!!!"
}
def createTables(){
def sql = new Sql(dataSource)
sql.execute '''
CREATE TABLE IF NOT EXISTS users (
userId INTEGER AUTO_INCREMENT PRIMARY KEY NOT NULL,
userName VARCHAR(50) UNIQUE,
password VARCHAR(100)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
'''
}
def insertToTables(){
def sql = new Sql(dataSource)
def params = ['vagg77', 'pass']
sql.execute 'INSERT INTO users (userName, password) VALUES (?, ?)', params
params = ['mary', 'pass']
sql.execute 'INSERT INTO users (userName, password) VALUES (?, ?)', params
}
}
userService.groovy
package loginmysql
class UserController {
def UserService
def index() {
UserService.createTables();
//UserService.insertToTables() // upon Inserting, must not be inserted twice
}
def login() {
UserService.searchLoginInput(params.userName,params.password)
redirect(action: "index") // return to index page again and display login message*/
}
}
package loginmysql
import grails.transaction.Transactional
import groovy.sql.Sql
@Transactional
class UserService {
def dataSource
def searchLoginInput(String input_user, String input_pass) {
def sql = new Sql(dataSource)
def sql2 = new Sql(dataSource)
int userNum = 0
sql.eachRow('SELECT userName FROM users WHERE userName=$input_user') { row ->
if (input_user == row.userName) {
sql2.eachRow('SELECT password FROM users WHERE password=$input_pass') { row2 ->
if (input_pass == row2.password) {
flash.message = " login succeded!!!"
session.user = input_user // keep info on who connected
userNum++
} else {
flash.message = "login failed!!!"
}
}
}
}
if (userNum==0) flash.message = "Username not found!!!"
}
def createTables(){
def sql = new Sql(dataSource)
sql.execute '''
CREATE TABLE IF NOT EXISTS users (
userId INTEGER AUTO_INCREMENT PRIMARY KEY NOT NULL,
userName VARCHAR(50) UNIQUE,
password VARCHAR(100)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
'''
}
def insertToTables(){
def sql = new Sql(dataSource)
def params = ['vagg77', 'pass']
sql.execute 'INSERT INTO users (userName, password) VALUES (?, ?)', params
params = ['mary', 'pass']
sql.execute 'INSERT INTO users (userName, password) VALUES (?, ?)', params
}
}
以下是登录页面时发生的情况:
如果我在searchLoginInput()中添加println()函数,那么我会看到值正确地从我的控制器发送到我的服务
这正是我的错误:
我想知道我输入的查询在MySQL上是否有错误的语法,所以我尝试在MySQL控制台上执行查询
数据库条目
控制台中的MySQL查询
Groovy字符串(或gstring)中的变量替换要求使用双引号,而不是单引号。另外,使用大括号 因此,在您的情况下,创建SQL语句的方式如下所示:
“从userName=${input\u user}的用户中选择用户名”
为什么要使用SQL而不是Gorm?
如果要更改查询,只需使用以下选项:
User.findByUsername(input_user)
gstring使用双引号,而不是单引号。所以应该是
sql.eachRow(“从userName=${input\u user}的用户中选择userName”)。
还要注意大括号的使用。感谢大家的提醒,从格式糟糕的代码中可以看出,我完全是grails的初学者。非常感谢您的帮助“但是,作为旁注。您不应该像这样创建SQL语句。这样做会让您自己面临SQL注入攻击。”-我不认为这是真的。Sql
类截取该属性访问并进行适当的转义。这是通过使用重载版本的方法来完成的,如eachRow
,该方法接受GString
作为参数,以便字符串在作为参数传递之前不会被插值。看见从那里,各个参数值从GString
中提取出来,其余的字符串值从GString
中检索出来,最后创建一个PreparedStatement
并用参数填充,因此整个过程是SQL注入安全的。@JeffScottBrown,一如既往,谢谢您。为了这个,还有很多别的!没问题。这是一个很好的特性。我被告知使用sql查询,注入数据源而不是GORM:POk,但是如果您是Grails的初学者,您应该知道使用sql查询而不是GORM是个坏主意。除非您需要使用存储过程,否则应该使用Grails标准。“…您应该知道使用sql查询而不是GORM是个坏主意。”-这并不总是正确的。使用SQL有很好的理由。GORM(以及通常的ORM)并不总是适用于所有场景的最佳解决方案。有时候SQL是完全合理的。当然可以,但通常不需要编写SQL,除非您需要运行存储过程之类的东西或太特殊的东西。对不起,我的英语不好:/“当然可以,但一般来说,除非您需要运行存储过程之类的东西或过于具体,否则不需要编写SQL。”-一般来说,这是合理的,但除了存储过程之外,SQL可能比使用ORM更有意义。我是在回答“你应该知道,用sql查询代替GORM是个坏主意”。