用mybatis将SQL查询语句”select * from user”的封装为配置文件
定义一个xml映射文件,文件名见名知意。如user-mapper.xml,文件内容如下:
<?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="suibian">
<!-- 定义查询语法的标签.
id - 语法命名, 由数字,字母,_ 组成. 数字不作为开头字符.
resultType - 定义结果类型. 代表一行数据对应一个什么java类型的对象.
-->
<select id="selectAllUsers" resultType="com.pojo.User">
<!-- 标签体中,定义要执行的SQL语句. -->
select * from tb_user
</select>
<select id="selectUserById" resultType="com.pojo.User">
select ID, Name, user_name as username, PassWORd, age from tb_user where id = 1
</select>
<select id="selectUsers" resultType="com.pojo.User">
<![CDATA[
select * from tb_user where id > 100
]]>
</select>
</mapper>