Mybatis动态SQL - 使用xml配置
Mybatis支持动态的拼接sql语句,以完成动态的业务逻辑,简化开发。查询数据库中tb_student的数据。
1. Student.java
2. Mapper.xml中通常配置: 配置resultMap用以声明数据库查询结果与实体类之间的映射关系。
<resultMap type="Student" id="StudentMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<result property="grade" column="class"/>
<result property="school" column="school"/>
</resultMap>
<if>: 如果传入的参数列表中‘age’不为空,则会在sql语句中加上if语句体中声明的sql语句片段。
<select id="findStudentUsingIf" parameterType="Map" resultMap="StudentMap">
select * from tb_student where 1=1