Article Directory
mybatis fuzzy query (3 types)
The first
select * from user where username like "%" #{name} "%"
The second
select * from user where username like "%${value}%"
The third
<!--concat拼接字符串 mysql独有的函数-->
select * from user where username like concat("%",#{username},"%")
The whole network says this, but I don’t know how to implement injection.
-
#{} is pre-compilation processing, ${} is string replacement. When Mybatis processes #{}, it replaces #{} in sql with a? Sign, and calls the set method of PreparedStatement to assign;
-
When Mybatis processes ${}, it replaces ${} with the value of the variable.
-
Using #{} can effectively prevent SQL injection and improve system security.
Configure aliases (typeAliases)
The first way : add in the mybatis-config.xml core configuration file <configuration></configuration>
:
<typeAlias type="com.lu.pojo.User" alias="User"/>
User can be used wherever com.lu.pojo.User is used.
The second way:
You can also specify a package name, MyBatis will search for the required Java Bean under the package name, for example:
<typeAliases>
<package name="com.lu.pojo"/>
</typeAliases>
In the absence of annotations, the unqualified class name of Bean's first letter and lowercase will be used as its alias. For example, the alias of com.lu.pojo.User is user; if there is an annotation, the alias is the annotation value. such as:
@Alias("user")
public class User{
...
}
Inconsistent property name and field name
Question: The password in the database is represented by pwd, and the password is represented by the entity class. The query result will be null.
The first way : alias with as in sql statement.
<select id="getUserById" resultType="com.lu.pojo.User">
select id,username, pwd as password from user where id = #{id}
</select>
The second way: use resultMap for mapping.
<!--结果集映射-->
<resultMap id="userMap" type="com.lu.pojo.User">
<!--property是实体类中的属性,column是数据库中的字段 -->
<result property="password" column="pwd" />
</resultMap>
<select id="getUserById" resultMap="userMap">
select id,username, pwd from user where id = #{id}
</select>
3 ways of paging query
1. Paging through limit
mapper interface:
//使用limit实现分页
List<User> getUserByLimit(Map<String,Integer> map);
In Mapper.xml:
<select id="getUserByLimit" parameterType="map" resultType="com.lu.pojo.User">
select * from user limit #{offset}, #{pageSize}
</select>
testing method:
@Test
public void getUserByLimit(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("offset",1);
map.put("pageSize",2);
List<User> users = userMapper.getUserByLimit(map);
for (User user : users) {
System.out.println(user);
}
sqlSession.close();
}
2. Paging through RowBounds
mapper interface:
//通过RowBounds实现分页
List<User> getUserByRowBounds(RowBounds rowBounds);
In Mapper.xml:
<select id="getUserByRowBounds" resultType="com.lu.pojo.User">
select * from user
</select>
testing method:
@Test
public void getUserByRowBounds(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
RowBounds rowBounds = new RowBounds(1, 2);
List<User> users = userMapper.getUserByRowBounds(rowBounds);
for (User user : users) {
System.out.println(user);
}
sqlSession.close();
}
3. Realize paging through the paging plugin pagehelper
Import dependencies in pom.xml:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
Add in mybatis-config.xml core configuration file:
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
mapper:
//通过pagehelper分页插件实现
List<User> getUserByPageHelper();
mapper.xml:
<select id="getUserByPageHelper" resultType="com.lu.pojo.User">
select * from user
</select>
testing method:
Import first at the top of the class, otherwise PageHelper cannot be used
import com.github.pagehelper.PageHelper;
Write the test class:
@Test
public void getUserByPageHelper(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
PageHelper.startPage(1,3);
List<User> users = userMapper.getUserByPageHelper();
for (User user : users) {
System.out.println(user);
}
sqlSession.close();
}
Use Lombok steps
1. Install the Lombok plugin in IDEA.
Click File in the upper left corner —> Click Settings —> Select Pulgins —> Enter Lombok search —> Click Install to install. As shown below
2. Import the lombok jar package into the project
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
3. Add annotations to the entity class
@NoArgsConstructor
: Generate a parameterless constructor;
@AllArgsConstructor
: Generate full parameter constructor;
@Data
: Acting on the class, is a collection of the following annotations:
@ToString @EqualsAndHashCode @Getter @Setter @RequiredArgsConstructor