Dynamic SQL of Mybatis_day3_Mybatis

<If> and <where> tags of dynamic SQL

  • We use different SQL statements to query according to the different values ​​of the entity class. For example, if the id is not empty, you can query based on the id, and if the username is different, add the username as a condition. This situation is often encountered in our multi-condition combination query.
  • Steps for usage
  1. Persistent layer Dao interface
/**
* 根据用户信息,查询用户列表
* @param user
* @return
*/
List<User> findByUser(User user);

  1. Persistent layer Dao mapping configuration
<select id="findByUser" resultType="user" parameterType="user">
	select * from user where 1=1
		<if test="username!=null and username != '' ">
			and username like #{username}
		</if>
		<if test="address != null">
			and address like #{address}
		</if>
</select>
  • Note: test attribute tag attribute names are written in the object, if the object is a wrapper class to use OGNLexpressions written.
    Also pay attention to the role of where 1 = 1 ~!
  • In order to simplify the conditional assembly of where 1 = 1 above, we can use <where>tags to simplify development:
<!-- 根据用户信息查询 -->
<select id="findByUser" resultType="user" parameterType="user">
	select * from user
	<where>
		<if test="username!=null and username != '' ">
			and username like #{username}
		</if>
		<if test="address != null">
			and address like #{address}
		</if>
	</where>
</select>

  1. test
@Test
public void testFindByUser() {
	User u = new User();
	u.setUsername("%王%");
	u.setAddress("%顺义%");
	//6.执行操作
	List<User> users = userDao.findByUser(u);
	for(User user : users) {
		System.out.println(user);
	}
}


<Foreach> tags of dynamic tags

Requirements:
Incoming multiple IDs to query user information, using the following two SQL implementation:

  1. SELECT * FROM USERS WHERE username LIKE ‘%张%’ AND (id =10 OR id =89 OR id=16)
  2. SELECT * FROM USERS WHERE username LIKE ‘%张%’ AND id IN (10,89,16)
  • In this way, when we perform range query, we must dynamically add the values ​​in a collection as parameters.
    So how will we pass the parameters?

  • The methods are as follows:
    1. Add a List collection in QueryVo to encapsulate the parameters

/**
* 
* <p>Title: QueryVo</p>
* <p>Description: 查询的条件</p>
* 
*/
public class QueryVo implements Serializable {
private List<Integer> ids;

public List<Integer> getIds() {
		return ids;
	}

public void setIds(List<Integer> ids) {
		this.ids = ids;
	}
}

  1. Persistent layer Dao interface
/**
* 根据 id 集合查询用户
* @param vo
* @return
*/
List<User> findInIds(QueryVo vo);

  1. Persistent layer Dao mapping configuration
<!-- 查询所有用户在 id 的集合之中 -->
<select id="findInIds" resultType="user" parameterType="queryvo">
	select * from user
	<where>
		<if test="ids != null and ids.size() > 0">
			<foreach collection="ids" open="id in ( " close=")" item="uid" separator=",">
				#{uid}
			</foreach>
		</if>
	</where>
</select>
  • SQL statement:

    • select 字段 from user where id in (?)
  • <foreach>The label is used to traverse the collection, its attributes:

    • collection : Represents the collection elements to be traversed
    • open : represents the beginning of the statement
    • close : represents the end part
    • item : represents the variable name generated by traversing each element of the collection
    • sperator : stands for separator

  1. Write test method
@Test
public void testFindInIds() {
	QueryVo vo = new QueryVo();
	List<Integer> ids = new ArrayList<Integer>();
	ids.add(41);
	ids.add(42);
	ids.add(43);
	ids.add(46);
	ids.add(57);
	vo.setIds(ids);
//6.执行操作
	List<User> users = userDao.findInIds(vo);
	for(User user : users) {
		System.out.println(user);
	}
}



Fragment of simplified writing in Mybatis

  • Repeated sql can be extracted in Sql, use include reference when using, and finally achieve the purpose of sql reuse.
  • Proceed as follows
  1. Define code snippets
<!-- 抽取重复的语句代码片段 -->
<sql id="defaultSql">
	select * from user
</sql>

  1. Quoting code snippets
<!-- 配置查询所有操作 -->
<select id="findAll" resultType="user">
	<include refid="defaultSql"></include>
</select>
<!-- 根据 id 查询 -->
<select id="findById" resultType="UsEr" parameterType="int">
	<include refid="defaultSql"></include>
	where id = #{uid}
</select>
  • include refid="defaultSql"></include>
    representative
    select * from user
Published 94 original articles · praised 0 · visits 2097

Guess you like

Origin blog.csdn.net/qq_16836791/article/details/104778152