SSM框架整合
方便自己學習的時候複製粘貼
整合最基礎的SSM框架 需要用到配置5個文件
從左到右依次爲 Spring核心配置文件 SpringMVC核心配置文件 Mybatis核心配置文件 Mybatis映射配置文件 web核心配置文件
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: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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--Spring 配置包註解掃描-->
<context:component-scan base-package="cn.kinggm520">
<!--配置黑名單-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--加載jdbc.properties-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置數據源對象-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置事務管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置事務管理-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" read-only="false"/>
<tx:method name="save*" read-only="false"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="update*" read-only="false"/>
<tx:method name="delete*" read-only="false"/>
</tx:attributes>
</tx:advice>
<!--配置織入-->
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* cn.kinggm520.service.impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config>
<!--整合Mybatis-->
<!--創建SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:SqlMapConfig.xml"/>
</bean>
<!--引入映射配置文件-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.kinggm520.dao"/>
</bean>
</beans>
spring-mvc.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: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 http://www.springframework.org/schema/context/spring-context.xsd
">
<!--註解包掃描-->
<context:component-scan base-package="cn.kinggm520.controller"/>
<!--mvc 註解驅動 方便解析json-->
<mvc:annotation-driven/>
<!--靜態資源權限開放-->
<mvc:default-servlet-handler/>
<!--視圖解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--配置簡單映射異常處理器-->
<!-- <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="error"/>
</bean>-->
<!--配置權限攔截器-->
<mvc:interceptors>
<mvc:interceptor>
<!--配置對哪些資源執行攔截操作-->
<mvc:mapping path="/**"/>
<!--配置哪些資源排除攔截操作-->
<mvc:exclude-mapping path="/user/login"/>
<bean class="cn.kinggm520.interceptor.MyInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
SqlMapConfig.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: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 http://www.springframework.org/schema/context/spring-context.xsd
">
<!--註解包掃描-->
<context:component-scan base-package="cn.kinggm520.controller"/>
<!--mvc 註解驅動 方便解析json-->
<mvc:annotation-driven/>
<!--靜態資源權限開放-->
<mvc:default-servlet-handler/>
<!--視圖解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--配置簡單映射異常處理器-->
<!-- <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="error"/>
</bean>-->
<!--配置權限攔截器-->
<mvc:interceptors>
<mvc:interceptor>
<!--配置對哪些資源執行攔截操作-->
<mvc:mapping path="/**"/>
<!--配置哪些資源排除攔截操作-->
<mvc:exclude-mapping path="/user/login"/>
<bean class="cn.kinggm520.interceptor.MyInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.kinggm520.dao.UserMapper">
<!--登錄-->
<select id="login" parameterType="user" resultType="user">
select * from sys_user where username=#{username} and password=#{password}
</select>
<!--查詢用戶列表-->
<select id="findAll" resultType="user">
select * from sys_user
</select>
<!--刪除用戶-->
<delete id="delUser" parameterType="long">
delete from sys_user where id =#{userId}
</delete>
<!--刪除用戶角色關係-->
<delete id="delUserRoleRel" parameterType="long">
delete from sys_user_role where userId = #{userId}
</delete>
<!--添加用戶-->
<insert id="save" parameterType="user">
<!-- 保存成功以後, 獲取字段爲id的值,賦給User對象的id屬性 -->
<selectKey keyProperty="id" keyColumn="id" resultType="Long" order="AFTER">
select last_insert_id()
</selectKey>
insert into sys_user values(NULL ,#{username},#{email},#{password},#{phoneNum})
</insert>
<!--添加用戶角色關係-->
<insert id="saveUserRoleRel" parameterType="long">
insert into sys_user_role VALUES (#{userId},#{roleId})
</insert>
<!--搜索用戶-->
<select id="search" resultType="user" parameterType="string">
select * from sys_user
<where>
username like #{username}
</where>
</select>
</mapper>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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="3.1">
<!--解決亂碼的過濾器-->
<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>
<!--全局的初始化參數-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--Spring的監聽器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--SpringMVC的前端控制器-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
log4j.properties
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c:/mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=info, stdout
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_studyspring
jdbc.username=root
jdbc.password=123456