Home > Back-end >  Mybatis OGNL in SQL statements stitching problems
Mybatis OGNL in SQL statements stitching problems

Time:09-21

Mybatis configuration file using the where and if label, did not give the id assignment, but joining together to the SQL statement
The HTML code is as follows: (the database user attributes are the id, username, password)
The XML configuration file:
 & lt; ? The XML version="1.0" encoding="utf-8"?> 
PUBLIC "-//mybatis.org//DTD Mapper/3.0/EN"
"Http://mybatis.org/dtd/mybatis-3-mapper.dtd" & gt;

SelectByCondition (User condition); - & gt;



The test code:
 package com. Yny. Test; 

import java.io.InputStream;
import java.util.List;

The import org. Apache. Ibatis. IO. Resources;
The import org. Apache. Ibatis. Session. SqlSession;
The import org. Apache. Ibatis. Session. SqlSessionFactory;
The import org. Apache. Ibatis. Session. The SqlSessionFactoryBuilder is;
The import org. Junit. Test;

The import com. Yny. Beans. The User;
The import com. Yny. Dao. UserMapperDynamicSQL;

Public class TestMybatisDynamicSQL {

@ Test
Public void test () throws the Exception {
The String resource="mybatis - config. XML";
InputStream InputStream=Resources. GetResourceAsStream (resource);
SqlSessionFactory SqlSessionFactory=new the SqlSessionFactoryBuilder is (). The build (inputStream);
System. The out. Println (sqlSessionFactory);

SqlSession session.=sqlSessionFactory openSession ();
Try {
UserMapperDynamicSQL mapper=session. GetMapper (UserMapperDynamicSQL. Class);
The User condition=new User ();
Condition. SetUsername (" aa ");
Condition. The setPassword (" 123 ");

List User=mapper. SelectByCondition (condition);
System.out.println(user);

} the finally {
session.close();
}

}

}

After the operation console output shows
 org. Apache. Ibatis. Session. Defaults. DefaultSqlSessionFactory @ 3327 bd23 
The DEBUG 04-24 21:59:44, 306==& gt; Preparing: select id, username and password from the user WHERE id=? And the username=? And password=? (BaseJdbcLogger. Java: 145)
The DEBUG 04-24 21:59:44, 344==& gt; The Parameters: 0 (Integer), aa (String), 123 (String) (145) BaseJdbcLogger. Java:
The DEBUG 04-24 21:59:44, 377 & lt;==Total: 0 (145) BaseJdbcLogger. Java:
[]

id into joining together into the console SQL statements, do not check in data, there should be no stitching id, which a great god can help us have a look?

CodePudding user response:

Your id field in the entity classes may be int type, when the new object initialized to zero by default, you can instead of type integer, so the default is null, it won't be joining together

CodePudding user response:

Agree with the 1st floor

CodePudding user response:

Uh huh is of type int, thank you

CodePudding user response:



Curious, the query: why do you want to have a password?

CodePudding user response:

references to walk in the four seasons, 4/f response:
, this query: why do you want to have a password?
this is logged in, of course have to check the password

CodePudding user response:

refer to fifth floor 221 B Baker Street response:
Quote: reference to go in the four seasons, 4/f, reply:



Curious, the query: why do you want to have a password?
this is logged in, of course have to check my password


Ok, login password in the database are generally commonly save encrypted, SQL login generally no ID parameter, anyway, it is good that oneself like, goodbye,

CodePudding user response:

The User condition=new User (); The inside of the id into a wrapper class Integer types

CodePudding user response:

refer to 6th floor walk in the four seasons of reply:
Quote: refer to fifth floor 221 B Baker Street response:

Quote: reference to go in the four seasons, 4/f, reply:



Curious, the query: why do you want to have a password?
this is logged in, of course have to check my password


Ok, login password in the database are generally generally save the encrypted, SQL login generally no ID parameter, however, it is good that oneself like, goodbye,
oh oh, no project experience, thank you
  • Related