Java-SSM框架整合
SSM框架整合后的目录结构:
1.Spring
- 配置依赖(pom.xml)
配置maven工程中的依赖jar包,如Junit、数据库驱动、数据库连接池、Mybatis等等
junit
mysql-connector-java
c3p0
servlet-api
jsp-api
jstl
mybatis
mybatis-spring
spring-webmvc
spring-jdbc
aspectjweaver
aspectjrt等等,按你的需求导入,导入时注意版本问题,再配置一个静态资源导出问题的build
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
- 自动扫描配置(applicationContext.xml)
<context:component-scan base-package="com.huang.ssmbuild">
<!-- Spring不扫描控制器 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
- Spring启动监听器(web.xml)
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
2.Mybatis
- 引入数据源配置(applicationContext.xml)
创建db.properties
在applicationContext.xml中引入db.properties
db.properties
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssmbuild?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.user=
jdbc.password=
jdbc.minPoolSize=10
jdbc.maxPoolSize=20
jdbc.maxIdleTime=1800
jdbc.initialPoolSize=2
jdbc.acquireIncrement=2
jdbc.idleConnectionTestPeriod=1800
jdbc.acquireRetryAttempts=2
<context:property-placeholder location="classpath*:db.properties"/>
<!-- 数据源配置:c3p0 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 数据库驱动 -->
<property name="driverClass" value="${jdbc.driver}" />
<!-- 数据库连接串 -->
<property name="jdbcUrl" value="${jdbc.jdbcUrl}" />
<!-- 数据库用户 -->
<property name="user" value="${jdbc.user}" />
<!-- 数据库密码 -->
<property name="password" value="${jdbc.password}" />
<!-- 最小连接数 -->
<property name="minPoolSize" value="${jdbc.minPoolSize}" />
<!-- 最大连接数 -->
<property name="maxPoolSize" value="${jdbc.maxPoolSize}" />
<!-- 最大空闲的时间,单位是秒,无用的链接再过时后会被回收 -->
<property name="maxIdleTime" value="${jdbc.maxIdleTime}" />
<!-- 数据库连接池初始化时获取的连接数 -->
<property name="initialPoolSize" value="${jdbc.initialPoolSize}" />
<!-- 在当前连接数耗尽的时候,一次获取的新的连接数 -->
<property name="acquireIncrement" value="${jdbc.acquireIncrement}" />
<!-- 每隔1800S检查数据库空闲连接 -->
<property name="idleConnectionTestPeriod"
value="${jdbc.idleConnectionTestPeriod}" />
<!-- 当数据库连接失败以后尝试 重新连接的次数 -->
<property name="acquireRetryAttempts" value="${jdbc.acquireRetryAttempts}" />
</bean>
- 配置mybatis整合(applicationContext.xml)
mybatis整合时,mybatis-config.xml中可以自己配置一些全局配置,但要注意配置扫描mapper的地址
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- mybatis全局配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml">
</property>
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置扫描mapper -->
<property name="mapperLocations" value="classpath:mappers/*.xml"/>
</bean>
- 配置扫描器:将mybatis接口实现加入到ioc容器中(applicationContext.xml)
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描所有的dao接口的实现,加入到ioc容器中 -->
<property name="basePackage" value="com.huang.ssmbuild.dao"/>
</bean>
- 事务控制等(applicationContext.xml)
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启注解支持的事务控制,比较重要的使用配置实现 -->
<aop:config>
<!-- 配置切入点 -->
<aop:pointcut expression="execution(* com.huang.ssmbuild.service..*(..))" id="txPoint"/>
<!-- 配置事务增强 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
</aop:config>
<!-- 配置事务增强 -->
<tx:advice id="txAdvice">
<tx:attributes>
<!-- 所有方法都是事务方法-->
<tx:method name="*" />
<!-- 所有get开头的方法,配置优化 -->
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
3.SpringMVC
- 扫描容器组件(扫描Controller)(springMVC.xml)
<context:component-scan base-package="com.huang.ssmbuild" use-default-filters="false">
<!-- 只扫描controller-->
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
- 配置视图解析器(springMVC.xml)
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--url前缀-->
<property name="prefix" value="/WEB-INF/views/"></property>
<!--url后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 3. 两个标准配置:将springMVC不能处理的请求交给默认处理器处理[tomcat]-->
<mvc:default-servlet-handler />
<!-- 添加注解支持、映射动态请求、JSR303校验、快捷AJAX等 -->
<mvc:annotation-driven/>
- 在web.xml中配置SpringMVC前端控制器DispatcherServlet、配置编码过滤等
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 3. 配置编码过滤,spring4以后需要配置请求/响应编码 -->
<filter>
<filter-name>characterEncoding</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>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>