1.mybatis的jar包
2.驱动包
3.文件配置
3.1创建cofing文件夹
3.2创建SqlMapperConfig.xml
文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>

3.4 在config目录下床架sqlmap文件夹以保存sql.xml映射文件

Mybatis环境配置
3.5 将写好的mapper文件加载到sqlmapconfig.xml中,这样才能加载调用pojo对象
Mybatis环境配置
3.6 mapper文件格式如下:

<?xml version="1.0" encoding="UTF-8"?> select * from employee where id=#{id} 3.6 编写单元测试 package cn.com.mybatis.first;

import java.io.InputStream;

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.junit.Test;

import cn.com.mybatis.pojo.Employee;

public class MybatisFirst {

@Test
public void findEmployeeByid ()throws Exception{
	//配置mybatis加载文件
	try {
		String resouce="SqlMapConfig.xml";
		//得到配置文件
		InputStream sourceStream=Resources.getResourceAsStream(resouce);
		SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(sourceStream);
		SqlSession session =sqlSessionFactory.openSession();
		Employee employee=session.selectOne("emp.querEmployeeById","1");
		System.out.println(employee.getName());
	} catch (Exception e) {
		e.printStackTrace();
	}
	
	
	
} 

}

4 配置日志文件 log4j.properties
log4j.rootLogger=DEBUG,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

相关文章: