Mybatis使用篇之四:整合Spring
Mybaits为什么要整合Spring?
说白了其实就想使用Spring提供的服务,比如Spring的事务管理、Spring的IOC对Bean进行管理等。?
?
Mybatis怎么整合Spring?
?由于目前Spring官方还没有出整合Mybatis的特性,所以这里在Spring框架和MyBatis框架上再添加用于整合的框架“mybatis-spring-1.0.2.jar” (该框架时MyBatis官方自己出的)。
?
(1)新建一个Web工程,名称为MybatisSpring。
?
(2)将Spring3.0.3、Mybatis3.0.6、Mybatis-Spring1.0.2、log4j、Oracle驱动和DBCP连接池的JAR包放到Web工程的lib下面,具体的JAR包如下:
?
(6)在com.user.sqlmap下面新建UserInfo.xml文件,该文件的内容如下:
?
?
(7)在src下面新建mybatis.cfg.xml文件,该文件的内容如下:
?
?
(8)新建一个Java类UserService.java,该类的内容如下:
?
?
?
(10)新建一个测试Java类UserInfoTest.java,该类的具体内容如下:
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).log4j:WARN Please initialize the log4j system properly.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.2012-02-11 21:13:42,156 DEBUG [java.sql.PreparedStatement] -==> Executing: select * from user_info where id = ? 2012-02-11 21:13:42,156 DEBUG [java.sql.PreparedStatement] -==> Parameters: 1(String)ID: 1, Name: 张三?
?
?
?
?
?
?
?
1 楼 蓝色的墨 2012-03-05 例子不错。也测试成功了。
正在学习spring+mybatis开发。
如果换成调用存储过程 这样写可以吗?
user = (UserInfo) sqlSession.selectOne("User.selectUser", "1");
2 楼 chenjc_it 2012-03-07 具体示例你参考一下http://chenjc-it.iteye.com/admin/blogs/1443432这篇文章。