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;
Select id, username and password from the userAnd id=# {id}
And the username=# {username}
And password=# {password}
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 ");
ListUser=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 togetherCodePudding user response:
Agree with the 1st floorCodePudding user response:
Uh huh is of type int, thank youCodePudding user response:
Curious, the query: why do you want to have a password?
CodePudding user response: