以自己写的一个简单案例
SSM所需的配置文件为:
spring配置
1.spring的核心配置文件applicationContext.xml
<!--开启包扫描 -->
<context:component-scan base-package="service"/>
2.在web.xml中配置监听器
目的是为了在启动服务器是,自动开启spring容器
<!--配置监听器 -->
<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>
springMVC的配置
1.在web.xml中配置前端控制器
<param-value>classpath:/springmvc.xml</param-value>指明springMVC的核心配置文件的路径
<!--配置前端控制器 -->
<servlet>
<servlet-name>springmvc</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>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
2.SpringMVC的核心配置文件springmvc.xml
开启mvc注解和包扫描(base-package="controller")
试图解析器是通过mode返回的试图名称,拼接前缀后缀,进行页面的渲染或者数据的动态加载
<!--启动springMVC注解 -->
<mvc:annotation-driven/>
<context:component-scan base-package="controller"/>
<!--视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
3.web.xml中配置全站乱码问题
<!--解决全站乱码问题 -->
<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>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
mybatis的配置
mybatis的配置比较多,准备文件
1.核心配置文件sqlMapConfig.xml
2.映射文件UserMapper.xml,配置如下:命名控件对应的路径为mapper接口文件所在路径
<mapper namespace="mapper.UserMapper">
<select id="findAll" resultType="pojo.User">
select * from user
</select>
</mapper>
3.mapper接口文件(UserMapper.java)如案例:
package mapper;
import java.util.List;
import org.springframework.stereotype.Component;
import pojo.User;
public interface UserMapper {
public List<User> findAll();
}
4.数据库配置参数(jdbc.properties)
5.最后在applicationContext.xml中整合mybatis
5.1 配置数据源
<!--引入外部配置文件 -->
<context:property-placeholder location="classpath:/jdbc.properties"/>
<!--配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${c3p0.driver}"></property>
<property name="jdbcUrl" value="${c3p0.url}"></property>
<property name="user" value="${c3p0.user}"></property>
<property name="password" value="${c3p0.password}"></property>
</bean>
5.2 配置事务管理器
<!--配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--配置事务切面 -->
<aop:config>
<aop:pointcut expression="execution(* service..*(..))" id="pc"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
</aop:config>
5.3 整合mybatis
<!--整合mybatis -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!--引入核心配置文件 -->
<property name="configLocation" value="classpath:/sqlMapConfig.xml"></property>
<!--引入映射文件 -->
<property name="mapperLocations" value="classpath:/pojo/*.xml"></property>
</bean>
<!--配置mapper接口 扫描mapper包 并且为接口文件创建代理对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper"></property>
</bean>