找了半天,发现自己暂时能写一写的可能就是这个东东了,第一篇博文,难免有各种问题,欢迎各位大神拍砖
mybatis的集成是用的 Mybatis-Spring,由于Spring暂时还只对ibatis 提供了支持,而在ibatis升级到了版本3改了个马甲叫"Mybatis"之后,Spring并没有对Mybatis提供支持,索性,Mybatis开发团队自己写了一个和Spring进行集成的东东,也就是Mybatis-Spring啦.
附上一个Mybatis-Spring官方文档的网址,中文版本的,看起来还是很不错的: Mybatis-Spring官方文档中文版
楼主决定要偷懒了,将项目的包结构神马的就省略了,直接就附上了一个Spring的配置文件(但愿发表了之后可以编辑哈,以后修改...2点了,to-do-list还没搞定 T_T)
这里有几点要说明下的:
- 首先你的项目当然得加入mybatis的jar包和mybatis-spring的jar包在类路径下面的啦,不然运行肯定会报错的
- 然后下面这个配置文件其实就是spring的 applicationContext.xml
这样配置的好处是什么呢?
- typeAlicase不用自己一行一行的敲啦,写好的对应的包的路径之后自动帮你搞定,所以在mapper文件(写SQL的地方), 参数类型和返回值的类型可以直接这样写了resultType="Admin" parameterType="Admin"
- mapper的xml文件也不用去建一个专门的xml配置文件,然后写N多行告诉mybatis自己创建了哪些mapper的xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<context:component-scan base-package="com.geekstyle" />
<!-- production&test enviroment <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/atii"></property> </bean> -->
<!-- development enviroment -->
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- auto scan domains and register type alias -->
<property name="typeAliasesPackage" value="com.geekstyle.mjreserch.domain" />
</bean>
<!-- auto register mappers -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.geekstyle.mjreserch.dao" />
</bean>
</beans>
额.省略了很多,轻拍哈,这周把这份博文完善下