MyBatis-
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="usernp">
<!--<sql>+<include> 查詢user表中id是1的記錄 -->
<sql id="columns">
id,name,addr,age
</sql>
<select id="findOne" resultType="User">
select <include refid="columns"/> from user <where> id= #{id}</where>
</select>
<!-- if標籤-->
<select id="ifsql" resultType="User">
select <include refid="columns"/> from user
<where>
<if test="id!=0">id=#{id}</if>
<if test="name!=null">and name=#{name}</if>
<if test="addr!=null">and addr=#{addr}</if>
</where>
</select>
</mapper>
相應的測試類
@Test
public void ifSql() {
// 2.建立SQLsession,執行SQL
SqlSession session = ssf.openSession();
// 第一個引數是sql的定位i,第二個是sql的引數值
User user = session.selectOne("usernp.findOne", 1);
// 3.處理結果
System.out.println(user);
// 4.釋放資源
session.close();
}