处理实体类属性名和数据库字段名不一致问题
例如,数据库的有一个字段名为pwd,而实体类的有一属性名为passWord,其他都与数据库字段名一样。
方式一
为列名指定别名 , 别名和java实体类的属性名一致
<select id="selectUserById" resultType="User">
select id , name , pwd as password from user where id = #{id}
</select>
方式二
使用结果集映射->ResultMap 【推荐】
<resultMap id="UserMap" type="User">
<!-- id为主键 -->
<id column="id" property="id"/>
<!-- column是数据库表的列名 , property是对应实体类的属性名 -->
<result column="name" property="name"/>
<result column="pwd" property="password"/>
</resultMap>
<select id="selectUserById" resultMap="UserMap">
select id , name , pwd from user where id = #{id}
</select>
分页操作
使用Limit实现分页
- Mapper接口,参数为map
//选择全部用户实现分页
List<User> selectUser(Map<String,Integer> map);
- Mapper.xml
<select id="selectUser" parameterType="map" resultType="user">
select * from user limit #{startIndex},#{pageSize}
</select>
- 测试
int currentPage = 1; //第几页
int pageSize = 2; //每页显示几个
Map<String,Integer> map = new HashMap<String,Integer>();
//起始位置 = (当前页面 - 1 ) * 页面大小
map.put("startIndex",(currentPage-1)*pageSize);
map.put("pageSize",pageSize);
List<User> users = mapper.selectUser(map);