在Spring3中使用MyBatis
1.MyBatis 例子
首先,单独使用MyBatis时:
import java.io.IOException; import java.io.Reader; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MyBatisUtil { // 每一个MyBatis的应用程序都以一个SqlSessionFactory对象的实例为核心 // 使用SqlSessionFactory的最佳实践是在应用运行期间不要重复创建多次,最佳范围是应用范围 private final static SqlSessionFactory sqlSessionFactory; static { String resource = "configuration.xml"; Reader reader = null; try { reader = Resources.getResourceAsReader(resource); } catch (IOException e) { System.out.println(e.getMessage()); } // SqlSessionFactory对象的实例可以通过SqlSessionFactoryBuilder对象来获得 // SqlSessionFactoryBuilder实例的最佳范围是方法范围(也就是本地方法变量)。 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } public static SqlSessionFactory getSqlSessionFactory() { return sqlSessionFactory; } }
配置文件:
<?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"> <mapper namespace="Mapper.UserMapper"> <!-- 这里namespace必须是UserMapper接口的路径,不然要运行的时候要报错 “is not known to the MapperRegistry”--> <insert id="insertUser" parameterType="User"> insert into vincent_user(name,age) values(#{name},#{age}) <!-- 这里sql结尾不能加分号,否则报“ORA-00911”的错误 --> </insert> <!-- 这里的id必须和UserMapper接口中的接口方法名相同,不然运行的时候也要报错 --> <select id="getUser" resultType="User" parameterType="java.lang.String"> select * from vincent_user where name=#{name} </select> </mapper>
使用的测试类为:
import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.junit.Test; public class TestMapper { static SqlSessionFactory sqlSessionFactory = null; static { sqlSessionFactory = MyBatisUtil.getSqlSessionFactory(); } @Test public void testAdd() { SqlSession sqlSession = sqlSessionFactory.openSession(); try { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = new User("tom",new Integer(5)); userMapper.insertUser(user); sqlSession.commit();//这里一定要提交,不然数据进不去数据库中 } finally { sqlSession.close(); } } @Test public void getUser() { SqlSession sqlSession = sqlSessionFactory.openSession(); try { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = userMapper.getUser("jun"); System.out.println("name: "+user.getName()+"|age: "+user.getAge()); } finally { sqlSession.close(); } } }
2.整合Spring3后,单独使用Mybatis
首先,Spring3配置文件中(applicationContext.xml)有:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:ibatis-config.xml" /> <property name="dataSource" ref="dataSource" /> <!-- mapper和resultmap配置路径 --> <property name="mapperLocations"> <list> <value>classpath:com/log/bean/mapper/*.xml</value> </list> </property> </bean>
<!-- 通过扫描的模式,扫描目录在com/log/bean/mapper目录下,所有的mapper都继承 SQLMapper接口的接口, 这样一个bean就可以了 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.log.bean.mapper"/> <property name="markerInterface" value="com.log.bean.mapper.SQLMapper"/> </bean>
工具类:
import org.apache.ibatis.session.SqlSessionFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class MyBatisUtil { private final static SqlSessionFactory sqlSessionFactory; static { ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:applicationContext.xml"); sqlSessionFactory = (SqlSessionFactory)ac.getBean("sqlSessionFactory"); } public static SqlSessionFactory getSqlSessionFactory() { return sqlSessionFactory; } }
本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!