使用结果集(resultMap)来解决实体类,和数据库字段不一致的问题
1.1、UserMapper
接口:
public interface UserMapper {
List<User> queryUsers();
}
1.2、UserMapper.xml
配置文件:
column 数据库中列, property 实体类中的属性
<?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="com.qn.dao.UserMapper">
<select id="queryUsers" resultMap="userMap">
select *
from mybatis.user
</select>
<resultMap id="userMap" type="user">
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="pwd" property="password"/>
</resultMap>
</mapper>
1.3、测试代码:
@Test
public void queryUsers() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.queryUsers();
for (User user : userList) {
System.out.println(user.toString());
}
sqlSession.close();
}
1.4、结果:
1.5、注意点:
要使用 resultMap
不是 resultType
,值也要和 resultMap
中的 id
对应
为什么要分页:
语法
select * from mybatis.user limit startIndex,pageSize
select * from mybatis.user limit 0,5; -- 查询 从0 到5 人数据
2.1、UserMapper
接口:
public interface UserMapper {
// 分页查询
List<User> getUserByLimit(Map<String, Integer> map);
}
2.2、UserMapper.xml
配置文件:
column 数据库中列, property 实体类中的属性
<?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="com.qn.dao.UserMapper">
<select id="getUserByLimit" parameterType="map" resultMap="userMap">
select *
from mybatis.user
limit #{startIndex},#{pageSize}
</select>
<resultMap id="userMap" type="user">
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="pwd" property="password"/>
</resultMap>
</mapper>
2.3、测试代码:
@Test
public void getUserByLimit() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("startIndex",0);
map.put("pageSize",3);
List<User> userList = mapper.getUserByLimit(map);
for (User user : userList) {
System.out.println(user.toString());
}
sqlSession.close();
}
2.4、测试结果:
3.1、代码实现
public interface UserMapper {
@Select("select * from mybatis.user")
List<User> getUserList();
@Select("select *from mybatis.user where id = #{id}")
User getUserById`@Param("id") int id);
@Insert("insert into mybatis.user(id, name, pwd)values (#{id},#{name},#{pwd})")
int addUser(User user);
@Update("update user set name=#{name},pwd=#{pwd} where id=#{id}")
int updateUser(User user);
@Delete("delete from user where id=#{id}")
int deleteUser(@Param("id") int id);
}
3.2、测试:
测试方式 与 xml 配置开发相同
@Test
public void queryUsers() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserList();
for (User user : userList) {
System.out.println(user.toString());
}
sqlSession.close();
}
3.3、注意点:
必须要将接口注册绑定到 mybatis的核心配置文件中
@Param("id")
注解
#和$符号:
相同点:都是对参数进行标记的符号
不同点:
原文:https://www.cnblogs.com/Right-A/p/14794960.html