SSM整合-配置文件
-
MySQL :5.5
-
jdk:8
-
tomcat:8
-
idea:2020.1.3
-
maven:3.6
项目结构

运行步骤
新建一个空的maven项目
右键项目选择:add framework support , 选择web application点击确定

按shift+Ctrl+Alt+s打开这个面板

添加本地tomcat

点击+选择artifact

可以选择本地tomcat安装目录、端口号、部署方式
确定后就可以运行了

二、配置文件
mybatisConfig.xml
<configuration>
<!-- 批量设置类的别名 -->
<typeAliases>
<package name="com.wanglz.pojo"/>
</typeAliases>
<!--批量映射-->
<!-- mapper接口名称和mapper映射文件名相同,且放在同一目录下-->
<!-- <mappers>
<package name="com.wanglz.mapper"/>
</mappers>-->
</configuration>
database.properties
jdbc.driver=com.mysql.jdbc.Driver
# 8.0mysql要设置时区
jdbc.url=jdbc:mysql://localhost:3306/springboot_crud?characterEncoding=utf-8
jdbc.username=root
jdbc.password=admin
jdbc.maxPoolSize=20
jdbc.minPoolSize=5
applicationContext-mvc.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--spring mvc只扫描控制器,禁用默认规则-->
<context:component-scan base-package="com.wanglz" use-default-filters="false">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--扫静态资源-->
<mvc:default-servlet-handler/>
<!--扫动态-->
<mvc:annotation-driven/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--文件上传解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/>
<property name="maxUploadSize" value="#{1024*1024*20}"/>
</bean>
</beans>
applicationContext.xml
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://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">
<!--spring 除了控制器不要扫,剩下的业务逻辑组件都要,包括mapper、service-->
<context:component-scan base-package="com.wanglz">
<!--扫描排除不写use-default-filters="false"用默认的排除规则-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--0、导入外部配置文件-->
<context:property-placeholder location="classpath:database.properties"/>
<!--1、使用c2p0数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>
<property name="minPoolSize" value="${jdbc.minPoolSize}"/>
</bean>
<!--2、配置使用mybatis操作数据库。根据配置文件得到sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--配置文件位置-->
<property name="configLocation" value="classpath:mybatis/mybatisConfig.xml"/>
<!--配置xml映射文件位置-->
<property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/>
</bean>
<!--3、把每一个dao接口的实现注入到spring ioc容器中,在service可以@Autowired dao-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--指定dao接口所在的包-->
<property name="basePackage" value="com.wanglz.mapper"/>
</bean>
<!--4、配置事务控制 配置事务管理器,让他控制数据源里面连接的 关闭和提交-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--5、aop事务支持,基于xml配置,哪些方法切入事务,还要写切入点表达式-->
<aop:config>
<!--切入点表达式-->
<aop:pointcut id="txPoint" expression="execution(* com.wanglz.service.*.*(..))"/>
<aop:advisor advice-ref="myTx" pointcut-ref="txPoint"/>
</aop:config>
<!--6、配置事务增强(事务属性、事务建议)
transaction-manager=""指定要配置的事务管理器的id-->
<tx:advice id="myTx" transaction-manager="transactionManager">
<!--配置事务属性-->
<tx:attributes>
<tx:method name="*" rollback-for="java.lang.Exception"/>
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
</beans>
web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--启动初始化spring ioc容器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--extends ContextLoader implements ServletContextListener-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--前端控制器DispatcherServlet-->
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 字符过滤器,配置在所有过滤器的前面 -->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--支持rest风格过滤器-->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--Session-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>

浙公网安备 33010602011771号