5、ResultMap
结果集映射
<resultMap id="UserMap" type="User">
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="pwd" property="password"/>
</resultMap>
6、日志
6.1、日志工厂
- SLF4J
- LOG4J【掌握】
- LOG4J2
- JDK_LOGGING
- COMMONS_LOGGING
- STDOUT_LOGGING【掌握】
- NO_LOGGING
STDOUT_LOGGING标准准日志输出
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
6.2、LOG4J
导入maven包
log4j.properties
#将等级为DEBUG的日志信息输出到console和file这两个目的地,console和file的定义在下面的代码
log4j.rootLogger=DEBUG,console,file
#控制台输出的相关设置
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%c]-%m%n
#文件输出的相关设置
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/ijuy.log
log4j.appender.file.MaxFileSize=10mb
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n
#日志输出级别
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
配置log4j的实现
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
简单使用
1、导包
import org.apache.log4j.Logger;
2、加载log对象
static Logger logger = Logger.getLogger(DaoTest.class);
3、日志级别
logger.info("info:进入了testLog4j");
logger.debug("debug:进入了testLog4j");
logger.error("error:进入了testLog4j");
7、分页
为什么要分页
- 减少数据的处理量
7.1、使用limit分页(主)
语法:
select * from mybatis.user limit startIndex,pageSize;
select * from mybatis.user limit 0,5;
测试
<select id="getUserByLimit" parameterType="map" resultMap="UserMap">
select * from mybatis.user limit #{startIndex},#{pageSize}
</select>
@Test
//查询全部
public void test2(){
try(SqlSession session = MybatisUtils.getSqlSession()) {
UserMapper mapper = session.getMapper(UserMapper.class);
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("startIndex", 0);
map.put("pageSize", 2);
List<User> userList = mapper.getUserByLimit(map);
for (User user : userList) {
System.out.println(user);
}
}
}
7.2、RowBounds
不建议开发中使用
<select id="getUserByRowBounds" resultMap="UserMap">
select * from mybatis.user
</select>
@Test
//分页2
public void test3(){
try(SqlSession session = MybatisUtils.getSqlSession()) {
UserMapper mapper = session.getMapper(UserMapper.class);
RowBounds rowBounds = new RowBounds(0, 4);
List<User> list = session.selectList("com.ijuy.dao.UserMapper.getUserByRowBounds", null, rowBounds);
for (User user : list) {
System.out.println(user);
}
}
}