两种方式都包含了:
package com.test.mybatis; import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import com.blog.bean.Passport; /**
* @author wangx
* @Date: Nov 25, 2016
* @func: 手工(非spring整合)的方式使用mybatis
* @Copyright: 2016 wangx. All rights reserved.
*/
public class ManualDaoHandle {
private final Logger log = LoggerFactory.getLogger(ManualDaoHandle.class);
public static void main(String[] args) {
SqlSession sqlSession = null;
try {
//第一种方式 使用sqlsesiontemplate
sqlSession = new SqlSessionTemplate(new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("com/test/mybatis/mybatis-config.xml")));
//第二种方式 直接使用session
//sqlSession = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("com/test/mybatis/mybatis-config.xml")).openSession();
List<Passport> list = sqlSession.selectList("getPassport",null);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(sqlSession != null) {
sqlSession.close();
}
}
}
}
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- <!DOCTYPE configuration SYSTEM "mybatis-3-config.dtd"> -->
<configuration>
<properties resource="jdbc.properties" />
<settings>
<setting name="lazyLoadingEnabled" value="false" />
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="" value="" />
</transactionManager>
<dataSource type="UNPOOLED">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/coremapper.xml" />
</mappers>
</configuration>
coremapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace和定义的Mapper接口对应,并实现其中的方法 -->
<mapper namespace="com.blog.interfaces.CoreMapper">
<select id="getPassport" parameterType="string" resultType="com.blog.bean.Passport">
select * from t_passport
</select>
</mapper>
==
区别在于,直接使用sqlsession在多线程中是不安全的,而sqlsessiontemplate是多线程安全,粗略看了下代码,挺复杂,sqlsessiontemplate在做操作,比如delete时,其实是调用的内部的sqlSessionProxy,即一个sqlsession的代理来完成的操作,而这个sqlsession的代理是这么来的:
this.sqlSessionProxy = (SqlSession) newProxyInstance(
SqlSessionFactory.class.getClassLoader(),
new Class[] { SqlSession.class },
new SqlSessionInterceptor());
第一个参数是指定由哪个类加载器来加载我们的代理对象,实际上不一定要使用上面代码中的这个CLASSLOADER,甚至可以随意自定义一个类比如class Shit{},无论这个类在哪个包中,都可以使用它的classloader来加载我们的SqlSessionInterceptor,但不能使用new HashMap().getClass().getClassLoader(),这样会报错:
Exception in thread "main" java.lang.IllegalArgumentException: interface com.test.pattern.proxy.Sqlsession is not visible from class loader
at java.lang.reflect.Proxy$ProxyClassFactory.apply(Proxy.java:616)
at java.lang.reflect.Proxy$ProxyClassFactory.apply(Proxy.java:592)
at java.lang.reflect.WeakCache$Factory.get(WeakCache.java:244)
at java.lang.reflect.WeakCache.get(WeakCache.java:141)
at java.lang.reflect.Proxy.getProxyClass0(Proxy.java:455)
at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:738)
at com.test.pattern.proxy.Test.main(Test.java:47)
原因是因为HashMapl来自rt.jar包,该包的classloader为bootstrap classloader,而我们自定义的类,由system classloader加载。
第二个参数是我们的代理对象需要实现的接口
第三个参数是我们的动态代理类
==