根据学习,现在整理一下基于XML的SSM框架整合.
~~
1.1 创建一个JavaWeb模块~~
1.2 创建一个我们使用的数据库(MySQL)
drop database if exists ssm;
create database ssm;
use ssm;
##创建图书表
create table t_book(
`id` int(11) primary key auto_increment, ## 主键
`name` varchar(50) not null, ## 书名
`author` varchar(50) not null, ## 作者
`price` decimal(11,2) not null, ## 价格
`sales` int(11) not null, ## 销量
`stock` int(11) ## 库存
);
## 插入初始化测试数据
insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`)
values(null , 'java从入门到放弃' , '爱你哟' , 80 , 9999 , 9 );
1.3 然后在web工程下导入所需要的的所有jar包
如果你使用maven,这一段就可以自动跳过啦~
数据库连接池:
druid-1.1.9.jar
切入点表达式解析:
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
Junit测试包:
junit_4.12.jar
org.hamcrest.core_1.3.0.jar
Mybatis的jar包:
mybatis-3.5.1.jar
Mybatis整合Spring的包:
mybatis-spring-2.0.4.jar
mysql数据库驱动jar:
mysql-connector-java-5.1.37-bin.jar
Spring核心层包:
spring-beans-5.2.5.RELEASE.jar
spring-context-5.2.5.RELEASE.jar
spring-core-5.2.5.RELEASE.jar
spring-expression-5.2.5.RELEASE.jar
Spring的日记桥接包( 是核心包的依赖包必须导 ):
spring-jcl-5.2.5.RELEASE.jar
Spring切面包:
spring-aop-5.2.5.RELEASE.jar
spring-aspects-5.2.5.RELEASE.jar
Spring数据库访问以及事务:
spring-jdbc-5.2.5.RELEASE.jar
spring-orm-5.2.5.RELEASE.jar
spring-tx-5.2.5.RELEASE.jar
Spring的测试包:
spring-test-5.2.5.RELEASE.jar
SpringMVC需要的jar包:
spring-web-5.2.5.RELEASE.jar
spring-webmvc-5.2.5.RELEASE.jar
**
**
2.1在src目录下创建配置文件
具体文件名及目录如下
每个文件配置内容如下:
applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
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-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 扫描除SpringMVC之外所有组件 -->
<context:component-scan base-package="com.atguigu">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>
<!-- 加载jdbc.properties属性配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置数据库连接池对象 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="user" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
<property name="url" value="${jdbc.url}" />
<property name="driverClassName" value="${jdbc.driver}" />
<property name="initialSize" value="${jdbc.initialSize}" />
<property name="maxActive" value="${jdbc.maxActive}" />
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- Mybatis整合Spring的核心配置之一 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="mapperLocations" value="classpath:com/atguigu/dao/*.xml" />
</bean>
<!-- Mybatis整合Spring的核心配置之二 老式的将Mapper接口注入到SpringIOC容器中
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.csdn.dao"></property>
</bean>
-->
<!-- Mybatis整合Spring的核心配置之二 扫描并将Mapper接口注入到SpringIOC容器中 -->
<mybatis:scan base-package="com.csdn.mapper" />
<!-- 配置事务属性 -->
<tx:advice id="tx_ssm" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 配置事务切面 -->
<aop:config>
<aop:advisor advice-ref="tx_ssm"
pointcut="execution(* com..service..*.*(..))" />
</aop:config>
</beans>
jdbc.properties文件:
user=root //这里不能使用username 会跟spring中默认的adminor冲突
password=root
url=jdbc:mysql://localhost:3306/ssm
driverClassName=com.mysql.jdbc.Driver
initialSize=5
maxActive=10
log4j.properties文件:
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
mbg.xml文件:
这个文件在applicationContext中加入也可以,分开也可以.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="DB2Tables" targetRuntime="MyBatis3">
<!– 去掉逆向工程中的全部注释 –>
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!– jdbc 链接4大属性 –>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ssm"
userId="root"
password="5650">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!– 生成javabean的配置–>
<javaModelGenerator targetPackage="com.csdn.pojo" targetProject=".\ssm_demo\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!–生成mapper.xml的配置–>
<sqlMapGenerator targetPackage="com.csdn.mapper" targetProject=".\ssm_demo\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!–生成mapper接口的配置–>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.csdn.mapper" targetProject=".\ssm_demo\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!–生成 javabean–>
<table schema="DB2ADMIN" tableName="t_book" domainObjectName="Book"></table>
</context>
</generatorConfiguration>
mybatis-config.xml配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 打开延迟加载的开关 -->
<setting name="lazyLoadingEnabled" value="true" />
<!-- 将积极加载改为消极加载 按需加载 -->
<setting name="aggressiveLazyLoading" value="false" />
</settings>
</configuration>
2.2web-info目录下的配置文件
如下:
dispatcher-servlet.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
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-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 扫描springMVC的类和异常处理 -->
<context:component-scan base-package="com.csdn" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/book/" />
<property name="suffix" value=".jsp"/>
</bean>
<!-- SpringMVC标签的两个mvc标签 -->
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
</beans>
web.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>ssm</display-name>
<!-- 解决乱码的Filter过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*
HiddenHttpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
HiddenHttpMethodFilter
/*
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
dispatcher
org.springframework.web.servlet.DispatcherServlet
1
dispatcher
/