SSM框架的整合,持久层使用mybatis框架,表现层springmvc,业务层spring
1、创建一个父工程模块。
配置pom.xml配置文件。
更改父工程的pom.xml的文件,添加<packaging>pom</packaging>
作用是:以pom方式打包。
2、创建子工程模块。
2.1 DAO层模块
DAO模块 dao层是与数据库相互交互的需要的配置,
在pom.xml下配置具体根据自己的DAO层的名字
<artifactId>Spring_Dao</artifactId>
配置文件:applicationContext.xml
配置文件的内容:
连接数据库:DriverManagerDataource
配置工厂:创建SqlSessionFactoryBean对象后配置工厂想扫描的包。扫描DAO类接口并放入到IOC容器中,(Spring整合mybatis)
配置的类:MapperScannerConfigurer然后设置要扫描的的接口放到IOC容器中
提供pojo对象dao接口不需要实现类(变成注解)
dao配置基本上完毕。(有不对的请在下面留言以便更改)。
配置的代码试例
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>
<property name="jdbcUrl" value="jdbc:oracle:thin:@192.168.30.10:1521:orcl"/>
<property name="user" value="liubo"/>
<property name="password" value="root"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--配置SqlSeesionFactory扫描包-->
<property name="typeAliasesPackage" value="xyz.yqys.domain"/>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>
reasonable=true
</value>
</property>
</bean>
</array>
</property>
</bean>
<!--配置扫描的Dao接口并放入到IOC中-->
<bean id="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="xyz.yqys.Dao"/>
</bean>
</beans>
2.2配置Service层
Service是与DAO层交互的
在配置service层的时一般需要三步
2.2.1、引入DAO层的applicationContext.xml
2.2.2 开启组件扫描
2.2.3配置事务管理
需要引入DAO层的applicationContext.xml
配置文件:applicationContext.xml
引入dao的配置文件<import>中间省略</import>
开启组件扫描
<context:component-scan base-package=””/>
开启事务管理
DataTransactionManager
提供service层的接口 并提供实现类 使用注解注入到IOC容器中。
配置的代码试例
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--引入dao的配置文件-->
<import resource="classpath:Spring/applicationContext.xml"/>
<!--开启组件扫描-->
<context:component-scan base-package="xyz.yqys.service"/>
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
Web层模块 web层是 与service层交互的,和上面一样需要引入service的applicationContext.xml配置文件。
web层需要配置web.xml,springmvc.xml applicationContext.xml
配置web.xml文件:
filter过滤器:CahracterEncodingFilter
前端控制器:DispathcheServlet
核心监听器 :ContextLoaderLister
由于web.xml是默认扫描WEB-INF下的applicationContext.xml,所以一般情况下需要重新指定路径。
配置springmvc.xml文件
开启组件扫描:
开启注解支持
mvc:annotation-driven/
配置视图解析器:
InternalRsourceViewResolver
释放静态资源
mvc:default-servlet-handler/
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--开启组件扫描-->
<context:component-scan base-package="xyz.yqys.webController"/>
<!--开启注解支持-->
<mvc:annotation-driven/>
<!--配置视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--释放静态资源-->
<mvc:default-servlet-handler/>
</beans>
在配置的时候要注意:
拦截配置的位置配置在spring的主配置文件的时候只能放在service用注解,配置springmvc需要放在controller层。
注意:配置父子工程的时候要注意在引入之前要在pom.xml中注入依赖。