注意,在核心配置中语法一定不能出错(大小写和空格是否有多加都可能导致运行出错)
如:
STDOUT_LOGGING标准日志工厂
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
若在/前面多加了一个空格都会导致输出出错。
加上settings之后输出:
关键部分:
Opening JDBC Connection
Created connection 1629687658.
Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@61230f6a]
> Preparing: select * from data1.user where id = ?
> Parameters: 2(Integer)
< Columns: id, name, pwd
< Row: 2, 两个孔, 341
<== Total: 1
User{id=2, name=‘两个孔’, password=‘341’}
LOG4J的日志实现
先导入jar包
log4j.properties
log4j.rootLogger = debug,D,E,info
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = logs.txt
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = [%-5p]%d{yyyy-MM-dd HH:mm:ss} method:%l%n%m%n
log4j.appender.D.filter.infoFilter = org.apache.log4j.varia.LevelRangeFilter
log4j.appender.D.filter.infoFilter.LevelMin=DEBUG
log4j.appender.D.filter.infoFilter.LevelMax=DEBUG
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File = ./log/kuang.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = DEBUG
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = [%-5p]%d{yyyy-MM-dd HH:mm:ss} method:%l%n%m%n
log4j.appender.E.filter.infoFilter = org.apache.log4j.varia.LevelRangeFilter
log4j.appender.E.filter.infoFilter.LevelMin=ERROR
log4j.appender.E.filter.infoFilter.LevelMax=ERROR
log4j.appender.info=org.apache.log4j.RollingFileAppender
log4j.appender.info.Threshold=INFO
log4j.appender.info.File=logs.txt
log4j.appender.info.MaxFileSize=200MB
log4j.appender.info.MaxBackupIndex=5
log4j.appender.info.layout=org.apache.log4j.PatternLayout
log4j.appender.info.layout.ConversionPattern=[%-5p]%d{yyyy-MM-dd HH:mm:ss} method:%l%n%m%n
log4j.appender.info.filter.infoFilter = org.apache.log4j.varia.LevelRangeFilter
log4j.appender.info.filter.infoFilter.LevelMin=INFO
log4j.appender.info.filter.infoFilter.LevelMax=INFO
xml:
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
测试使用
注意!!导包时要导apache下的包:
import org.apache.log4j.Logger;
运行测试类:
public class UserDaotest {
static Logger logger = Logger.getLogger(UserDaotest.class);
@Test
public void testlog(){
logger.info("进入log4j:");
}
@Test
public void query(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserDao que = sqlSession.getMapper(UserDao.class);
User user = que.query(2);
System.out.println(user);
sqlSession.close();
}
}
运行:
发现目录下多了一个文件:
查看文件内容:
分页查询
测试类:
@Test
public void getuserbylimit(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserDao getbylimt = sqlSession.getMapper(UserDao.class);
HashMap<String,Integer>map = new HashMap<String,Integer>();
map.put("startindex",0);
map.put("pagesize",6);
List<User>userList = getbylimt.getuserbylimit(map);
for(User user:userList){
System.out.println(user);
}
sqlSession.close();
}
UserDao接口:
List<User>getuserbylimit(Map<String,Integer>map);
sql语句:
<select id="getuserbylimit" parameterType="map" resultType="Usera">
select * from data1.user limit #{startindex},#{pagesize}
</select>