项目结构
如果log4j.peoperties放于resource目录下,会自动读取,自动起作用。
这里修改了路径,在外面加了一层properties文件夹
mybatis获取sqlSession对象工具类
package com.zzb.util;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class TestUtil {
//声明SqlSession对象
private static SqlSession sqlSession = null;
//使用静态代码块初始化SqlSession对象
static {
//1.读取核心配置文件
InputStream is = null;
try {
is = Resources.getResourceAsStream("mybatis/mybatis-config.xml");
} catch (IOException e) {
e.printStackTrace();
}
//2.创建工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.生产数据操作对象
sqlSession = sqlSessionFactory.openSession();
}
//创建SqlSession对象
public static SqlSession initSqlSession(){
return sqlSession;
}
}
测试类
PropertyConfigurator.configure(properties);引入log4j配置文件
package com.zzb.test;
import com.github.pagehelper.PageHelper;
import com.zzb.dao.GInboxDao;
import com.zzb.entity.GInbox;
import com.zzb.util.TestUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.PropertyConfigurator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
public class MybatisTest {
private SqlSession sqlSession = null;
@Before
public void before() throws IOException {
sqlSession = TestUtil.initSqlSession();
Properties properties = new Properties();
InputStream is = MybatisTest.class.getClassLoader().getResourceAsStream("properties\\log4j.properties");
properties.load(is);
//用这个来引入log4j的配置文件
PropertyConfigurator.configure(properties);
}
@After
public void after(){
sqlSession.close();
}
@Test
public void test() throws IOException {
PageHelper.startPage(0,5);
GInboxDao personMapper = sqlSession.getMapper(GInboxDao.class);
GInbox gInbox = personMapper.selectByPrimaryKey("220424103609703rWmwQ0iUaiYRUH3d");
System.out.println(gInbox);
}
}
pom.xml文件maven依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!--MyBatis核心包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<!-- MySQL驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<!-- junit测试包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<!--<scope>test</scope>-->
<!-- 仅仅测试中可依赖,/java 目录下的类以来不到 -->
<!-- 换成compile 即可/java 目录下有效 -->
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<!-- pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.9</version>
</dependency>
log4j.properties
# Global logging configuration,建议开发环境中要用debug
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n