很多时候并不需要查询数据库中的全部字段,用*号的话会降低查询效率,后期数据库的字段会不断增加,很多字段的组合是经常被使用到的,每次都单独写出来很麻烦
Mybatis的映射文件中引入了<include refid="Base_Column_List" /> ,意思就是固定几个需要查询的字段,在sql语句中不需要写具体的字段了。具体使用如下:
<sql id="Base_Column_List">
id, username, password, email, phone, question, answer, role, create_time, update_time
</sql>
<select id="selectByUsername" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from mall_user
where username = #{username,jdbcType=VARCHAR}
</select>