一定要下载源码自己去琢磨,自己配置一遍
谈到SSM,我在网上看了很多整合教程,我也跟着一起整合过,都有大大小小的问题,所以今天元旦假期,我就抽一上午写出我自己的教程,一是Spring和MyBatis的整合,二是加上SpringMVC,即SSM大功告成。
首先我得说一下我的版本(我觉得版本是次要的,只要你弄清楚配置文件的关系,即怎么配置配置文件,什么版本都一样,只是版本最大的问题我觉得是与jdk和tomcat这些有关)
MyBatis 3.2.7
Spring 3.2.0
再给大家数据库吧,我是MySQL,我把sql文件给你们,直接放数据库运行就可以。
废话不多说,先谈谈mybatis和spring整合的思路
1、让spring管理SqlSessionFactory
2、让spring管理mapper对象和dao。
使用spring和mybatis整合开发mapper代理及原始dao接口。
自动开启事务,自动关闭 sqlsession.
3、让spring管理数据源( 数据库连接池)
在eclipse创建Java工程就行(还没到SpringMVC呢)
先看看目录结构图,因为我装了Spring的插件,所以项目会有个S。dao层是为了实现dao和Mapper两种不同的开发,也可以先忽略,config是资源文件夹。
加入jar包
在这里就把所有jar包都一次给你,包括SpringMVC的。
mybatis与spring整合全部jar包(包括springmvc
1、mybatis3.2.7本身的jar包
2、数据库驱动包
3、spring3.2.0
4、spring和mybatis整合包
从mybatis的官方下载spring和mybatis整合包
log4j.properties这个也是Spring依赖的日志文件,大家如果需要自行下载工程查看哈。
重点(配置文件),别急着复制配置文件,因为具体的要看开发情况,先了解一下
SqlMapconfig.xml
mybatis配置文件:别名、settings,数据源不在这里配置(因为有了Spring来管理)
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><!-- 定义 别名 --><typeAliases><!--
单个别名的定义
alias:别名,type:别名映射的类型 --><!-- <typeAlias type="com.hust.mybatis.po.User" alias="user"/> --><!-- 批量别名定义
指定包路径,自动扫描包下边的pojo,定义别名,别名默认为类名(首字母小写或大写)
--><packagename="com.hust.springmybatis.po"/></typeAliases><mappers><!-- 通过resource引用mapper的映射文件 --><mapperresource="sqlmap/User.xml" /><mapperresource="mapper/UserMapper.xml" /></mappers></configuration>
applicationContext.xml
1、数据源(dbcp连接池)(db.properties自行下载工程查看哈,再说这个一般人都会)
2、SqlSessionFactory
3、mapper或dao
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"><!-- 配置数据源 --><context:property-placeholderlocation="classpath:db.properties"/><!-- 如果是用额外的配置文件 --><beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><propertyname="driverClassName"value="${jdbc.driver}"></property><propertyname="url"value="${jdbc.url}"></property><propertyname="username"value="${jdbc.username}"></property><propertyname="password"value="${jdbc.password}"></property><propertyname="maxActive"value="10"></property><propertyname="maxIdle"value="5"></property></bean><!-- SqlSessionFactory --><beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 数据源 --><propertyname="dataSource"ref="dataSource"></property><!-- mybatis配置文件 --><propertyname="configLocation"value="classpath:mybatis/SqlMapConfig.xml"></property></bean><beanid="userDao"class="com.hust.springmybatis.dao.UserDaoImpl"><propertyname="sqlSessionFactory"ref="sqlSessionFactory"></property></bean><!-- 配置Mapper
MapperFactoryBean:用于生成mapper代理对象
--><!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.hust.springmybatis.mapper.UserMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean> --><!-- MapperScannerConfigurer:mapper的扫描器,将包下边的mapper接口自动创建代理对象
自动创建到spring容器中,bean的id是mapper的类名(首字母小写)
--><beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 配置扫描包路径
如果要扫描多个包,中间使用半角逗好隔开
--><propertyname="basePackage"value="com.hust.springmybatis.mapper"></property><propertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"></property></bean></beans>
整合开发原始dao接口
配置SqlSessionFactory
在 applicationContext.xml配置SqlSessionFactory
开发dao,这里就举findUserById这个例子
dao接口
dao的实现类,在这儿调用配置文件配置的东西(test是配置文件的namespace)
user.xml配置文件,这个id必须和dao里面的方法名一致
最有别忘了去Spring的配置文件applicationContext.xml里面配置dao
以上完成之后,就可以开始测试dao接口
这样Dao的开发就大功告成,不过不要惊喜,我们还要实现Mapper的代理方法。
开发mapper.xml和mapper.java
一种是使用MapperFactoryBean,使用此方法对于每个mapper都需要配置,比较繁琐。所以我们使用第二种,MapperScannerConfigurer(扫描mapper)
在spring的配置文件配置扫描mapper
开发mapper,还是以findUserById为例
UserMapper.java
UserMapper.xml
这个xml可以放在和UserMapper.java一个包里面,就会被Spring扫描到,这个工程,我是单独放在资源文件的mapper里面,所以得在在SqlMapConfig.xml里面配置
测试mapper接口
至此,Spring与MyBaits的整合与开发都可以了。