SqlSessionFactory ssf = null;
// 创建sqlsessionFactory工厂
@Before
public void init() {
try {
InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
ssf = new SqlSessionFactoryBuilder().build(in);
} catch (IOException e) {
e.printStackTrace();
}
}
// 测试一对一的关系
@Test
public void associtation() {
// 创建sqlsession,执行SQL
SqlSession session = ssf.openSession();
//利用接口开发
UserInfoDao dao = session.getMapper(UserInfoDao.class);
//调用接口里的方法
UserInfo info=dao.findExtraByUser(1);
//调用接口里的方法,把数据放入缓存中,从一级缓存中取
UserInfo info2=dao.findExtraByUser(1);
// 处理结果
System.out.println(info);
System.out.println(info2);
// 释放资源
session.close();
}
只发出一次sql
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 1468303011.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@578486a3]
DEBUG [main] - ==> Preparing: SELECT * FROM user_info t1,user_extra t2 WHERE t1.id=t2.user_id AND t1.id=?
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <== Total: 1
UserInfo [id=1, userName=韩梅梅, userAddr=上海, userAge=20, userExtra=UserExtra [id=1, work=程序员, salary=100000.0, userId=1], orders=null]
UserInfo [id=1, userName=韩梅梅, userAddr=上海, userAge=20, userExtra=UserExtra [id=1, work=程序员, salary=100000.0, userId=1], orders=null]
DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@578486a3]
DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@578486a3]
DEBUG [main] - Returned connection 1468303011 to pool.
SqlSessionFactory ssf = null;
// 创建sqlsessionFactory工厂
@Before
public void init() {
try {
InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
ssf = new SqlSessionFactoryBuilder().build(in);
} catch (IOException e) {
e.printStackTrace();
}
}
// 测试一对一的关系
@Test
public void associtation() {
// 创建sqlsession,执行SQL
SqlSession session = ssf.openSession();
//利用接口开发
UserInfoDao dao = session.getMapper(UserInfoDao.class);
//调用接口里的方法
UserInfo info=dao.findExtraByUser(1);
//调用接口里的方法,把数据放入缓存中,从一级缓存中取
UserInfo info2=dao.findExtraByUser(2);
// 处理结果
System.out.println(info);
System.out.println(info2);
// 释放资源
session.close();
}
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 1468303011.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@578486a3]
DEBUG [main] - ==> Preparing: SELECT * FROM user_info t1,user_extra t2 WHERE t1.id=t2.user_id AND t1.id=?
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <== Total: 1
DEBUG [main] - ==> Preparing: SELECT * FROM user_info t1,user_extra t2 WHERE t1.id=t2.user_id AND t1.id=?
DEBUG [main] - ==> Parameters: 2(Integer)
DEBUG [main] - <== Total: 1
UserInfo [id=1, userName=韩梅梅, userAddr=上海, userAge=20, userExtra=UserExtra [id=1, work=程序员, salary=100000.0, userId=1], orders=null]
UserInfo [id=2, userName=王海涛, userAddr=北京, userAge=30, userExtra=UserExtra [id=2, work=教师, salary=1000.0, userId=2], orders=null]
DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@578486a3]
DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@578486a3]
DEBUG [main] - Returned connection 1468303011 to pool.