解决属性名和数据库字段名不一致的问题
1.查询为null的问题
1.查看之前的数据库字段名
2.java实体类设计
3.接口
User getUserbyid(int id);//根据id查询用户
4.mapper映射文件
<select id="getUser" resultType="com.xiong.pojo.User">
select * from mybatis.user;
</select>
4.测试
@Test
public void test(){
//获得sqlsession对象
SqlSession sqlSession = MyBatisUtils.getSqlSession();
// System.out.println(sqlSession);
//执行sql
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> user = mapper.getUser();
for (User user1 : user) {
System.out.println(user1);
}
sqlSession.close();
}
password出现了null,表示存在了问题。
结果原因:
select * from user where id = #{id} 可以看做
select id,name,pwd from user where id = #{id}
而mybatis会根据这些查询的列名(会将列名转化为小写,数据库不区分大小写) , 去对应的实体类中查找相应列名的set方法设值 , 由于找不到setPwd() , 所以password返回null ; 【自动映射】
2.解决方案
方案一:在sql语句中为列名指定别名,别名和实体类的属性名一致
<select id="getUser" resultType="com.xiong.pojo.User">
select id,name,pwd as password from mybatis.user;
</select>
方案二:使用结果集映射–> ResultMap [推荐]
<resultMap id="UserMapper" type="com.xiong.pojo.User">
<id column="id" property="id"/>
<!-- column是数据库表的列名 , property是对应实体类的属性名 -->
<result column="name" property="name"/>
<result column="pwd" property="password"/>
</resultMap>
<select id="getUser" resultMap="UserMapper">
select * from mybatis.user;
</select>
3.日志工厂
思考:我们在测试SQL的时候,要是能够在控制台输出 SQL 的话,是不是就能够有更快的排错效率?
如果一个 数据库相关的操作出现了问题,我们可以根据输出的SQL语句快速排查问题。
对于以往的开发过程,我们会经常使用到debug模式来调节,跟踪我们的代码执行过程。但是现在使用Mybatis是基于接口,配置文件的源代码执行过程。因此,我们必须选择日志工具来作为我们开发,调节程序的工具。
Mybatis内置的日志工厂提供日志功能,具体的日志实现有以下几种工具:
SLF4J
Apache Commons Logging
Log4j 2
Log4j
JDK logging
标准日志实现:
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
测试,可以看到控制台有大量的输出!我们可以通过这些输出来判断程序到底哪里出了Bug
要注意一下 这个顺序:
结果:
4.分页
limit实现分页
在学习mybatis等持久层框架的时候,会经常对数据进行增删改查操作,使用最多的是对数据库进行查询操作,如果查询大量数据的时候,我们往往使用分页进行查询,也就是每次处理小部分数据,这样对数据库压力就在可控范围内。
#语法
SELECT * FROM table LIMIT stratIndex,pageSize
SELECT * FROM table LIMIT 5,10; // 检索记录行 6-15
#为了检索从某一个偏移量到记录集的结束所有的记录行,可以指定第二个参数为 -1:
SELECT * FROM table LIMIT 95,-1; // 检索记录行 96-last.
#如果只给定一个参数,它表示返回最大的记录行数目:
SELECT * FROM table LIMIT 5; //检索前 5 个记录行
#换句话说,LIMIT n 等价于 LIMIT 0,n。
实现分页的步骤:
1.修改mapper文件
<select id="getUser" resultMap="UserMapper" parameterType="map">
select * from mybatis.user limit #{startindex},#{pagesize};
</select>
2.mapper接口,参数为map
//查询
List<User> getUser(Map<String,Integer> map);
3.在测试类中传入参数进行测试
@Test
public void test(){
//获得sqlsession对象
SqlSession sqlSession = MyBatisUtils.getSqlSession();
// System.out.println(sqlSession);
//执行sql
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap<String, Integer> map = new HashMap<>();
map.put("startindex",2);
map.put("pagesize",2);
List<User> user = mapper.getUser(map);
for (User user1 : user) {
System.out.println(user1);
}
sqlSession.close();
}
结果:
5.RowBounds分页
我们除了使用Limit在SQL层面实现分页,也可以使用RowBounds在Java代码层面实现分页,当然此种方式作为了解即可。我们来看下如何实现的!
1.mapper接口
//选择全部用户RowBounds实现分页
List<User> getUserByRowBounds();
2.mapper文件
<select id="getUserByRowBounds" resultType="com.xiong.pojo.User">
select * from mvbatis.user
</select>
3.测试
@Test
public void test(){
//获得sqlsession对象
SqlSession sqlSession = MyBatisUtils.getSqlSession();
// System.out.println(sqlSession);
//执行sql
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap<String, Integer> map = new HashMap<>();
map.put("startindex",2);
map.put("pagesize",2);
List<User> user = mapper.getUser(map);
for (User user1 : user) {
System.out.println(user1);
}
sqlSession.close();
}
我的博客即将搬运同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan