在Spring3中使用MyBatis
来源:互联网 发布:南斯拉夫战争 知乎 编辑:程序博客网 时间:2024/04/29 12:18
Mybatis Spring3
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; } }
- 在Spring3中使用MyBatis
- Spring3:如何在Spring3中配置并使用FreeMarker?
- 【MyBatis】在MyBatis中使用in语句
- 在spring中使用MyBatis
- 在项目中使用mybatis
- 在Eclipse中使用Mybatis
- 在Spring3中使用注解(@Scheduled)创建计划任务
- 在Spring3中使用注解(@Scheduled)创建计划任务
- 在Spring3中使用注解(@Scheduled)创建计划任务
- 在Spring3中使用注解(@Scheduled)创建计划任务
- 在Spring3中使用注解(@Scheduled)创建计划任务
- 在Spring3中使用注解(@Scheduled)创建计划任务
- 在Spring3中使用注解(@Scheduled)创建计划任务
- 在Spring3中使用注解(@Scheduled)创建计划任务源码
- 在Spring3中使用注解(@Scheduled)创建计划任务
- 在Spring3中使用注解(@Scheduled)创建计划任务
- 在Spring3中使用注解(@Scheduled)创建计划任务
- Mybatis+Spring3
- 如何导入android sdk 的 sample中的源码
- ubuntu 10.04 安装sun java
- Linux系统调用过程
- closest node to the target in BST
- GetPrivateProfileString
- 在Spring3中使用MyBatis
- c++ vector 使用效率问题
- oracle表空间用户角色权限
- 《C++Primer》 第18章:特殊工具与技术
- office2010 每次打开WORD 都会弹出安装文件的框框
- windows内核安全编程——内核上机指导
- 在navigationBar上面添加多个任意控件
- GWT开发 - 开发环境搭建 Maven+Eclipse+Tomcat
- GWT开发 - 开发环境搭建 Maven+NetBeans+Tomcat