SSM整合

  1. <!--导入依赖-->
  2. <dependencies>
  3. <!--spring ioc核心-->
  4. <dependency>
  5. <groupId>org.springframework</groupId>
  6. <artifactId>spring-context</artifactId>
  7. <version>5.0.8.RELEASE</version>
  8. </dependency>
  9. <dependency>
  10. <groupId>commons-logging</groupId>
  11. <artifactId>commons-logging</artifactId>
  12. <version>1.1.1</version>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework</groupId>
  16. <artifactId>spring-test</artifactId>
  17. <version>5.0.8.RELEASE</version>
  18. </dependency>
  19. <!--junit-->
  20. <dependency>
  21. <groupId>junit</groupId>
  22. <artifactId>junit</artifactId>
  23. <version>4.11</version>
  24. <scope>test</scope>
  25. </dependency>
  26. <!--事务-->
  27. <dependency>
  28. <groupId>org.springframework</groupId>
  29. <artifactId>spring-tx</artifactId>
  30. <version>5.0.8.RELEASE</version>
  31. </dependency>
  32. <!--jdbc核心-->
  33. <dependency>
  34. <groupId>org.springframework</groupId>
  35. <artifactId>spring-orm</artifactId>
  36. <version>5.1.2.RELEASE</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework</groupId>
  40. <artifactId>spring-jdbc</artifactId>
  41. <version>5.0.8.RELEASE</version>
  42. </dependency>
  43. <!--aop核心-->
  44. <dependency>
  45. <groupId>org.springframework</groupId>
  46. <artifactId>spring-aspects</artifactId>
  47. <version>5.1.2.RELEASE</version>
  48. </dependency>
  49. <dependency>
  50. <groupId>org.aspectj</groupId>
  51. <artifactId>aspectjweaver</artifactId>
  52. <version>1.8.9</version>
  53. </dependency>
  54. <dependency>
  55. <groupId>aopalliance</groupId>
  56. <artifactId>aopalliance</artifactId>
  57. <version>1.0</version>
  58. </dependency>
  59. <dependency>
  60. <groupId>cglib</groupId>
  61. <artifactId>cglib</artifactId>
  62. <version>3.1</version>
  63. </dependency>
  64. <!--springmvc-->
  65. <dependency>
  66. <groupId>org.springframework</groupId>
  67. <artifactId>spring-webmvc</artifactId>
  68. <version>5.0.8.RELEASE</version>
  69. </dependency>
  70. <!--jstl-->
  71. <dependency>
  72. <groupId>jstl</groupId>
  73. <artifactId>jstl</artifactId>
  74. <version>1.2</version>
  75. </dependency>
  76. <!--文件上传和下载-->
  77. <dependency>
  78. <groupId>commons-io</groupId>
  79. <artifactId>commons-io</artifactId>
  80. <version>2.2</version>
  81. </dependency>
  82. <dependency>
  83. <groupId>commons-fileupload</groupId>
  84. <artifactId>commons-fileupload</artifactId>
  85. <version>1.3.1</version>
  86. </dependency>
  87. <!--Hibernate Validator-->
  88. <dependency>
  89. <groupId>org.hibernate.validator</groupId>
  90. <artifactId>hibernate-validator</artifactId>
  91. <version>6.0.13.Final</version>
  92. </dependency>
  93. <dependency>
  94. <groupId>org.jboss.logging</groupId>
  95. <artifactId>jboss-logging</artifactId>
  96. <version>3.3.2.Final</version>
  97. </dependency>
  98. <dependency>
  99. <groupId>javax.validation</groupId>
  100. <artifactId>validation-api</artifactId>
  101. <version>2.0.1.Final</version>
  102. </dependency>
  103. <dependency>
  104. <groupId>com.fasterxml</groupId>
  105. <artifactId>classmate</artifactId>
  106. <version>1.4.0</version>
  107. </dependency>
  108. <!--json依赖,支持ajax-->
  109. <dependency>
  110. <groupId>com.fasterxml.jackson.core</groupId>
  111. <artifactId>jackson-annotations</artifactId>
  112. <version>2.11.1</version>
  113. </dependency>
  114. <dependency>
  115. <groupId>com.fasterxml.jackson.core</groupId>
  116. <artifactId>jackson-core</artifactId>
  117. <version>2.11.1</version>
  118. </dependency>
  119. <dependency>
  120. <groupId>com.fasterxml.jackson.core</groupId>
  121. <artifactId>jackson-databind</artifactId>
  122. <version>2.11.1</version>
  123. </dependency>
  124. <dependency>
  125. <groupId>javax.servlet</groupId>
  126. <artifactId>servlet-api</artifactId>
  127. <version>2.4</version>
  128. </dependency>
  129. <!--mybatis-->
  130. <dependency>
  131. <groupId>org.mybatis</groupId>
  132. <artifactId>mybatis</artifactId>
  133. <version>3.4.5</version>
  134. </dependency>
  135. <dependency>
  136. <groupId>mysql</groupId>
  137. <artifactId>mysql-connector-java</artifactId>
  138. <version>5.1.47</version>
  139. </dependency>
  140. <dependency>
  141. <groupId>log4j</groupId>
  142. <artifactId>log4j</artifactId>
  143. <version>1.2.12</version>
  144. </dependency>
  145. <dependency>
  146. <groupId>com.mchange</groupId>
  147. <artifactId>c3p0</artifactId>
  148. <version>0.9.5.2</version>
  149. </dependency>
  150. <!--spring-mybatis整合包-->
  151. <dependency>
  152. <groupId>org.mybatis</groupId>
  153. <artifactId>mybatis-spring</artifactId>
  154. <version>2.0.4</version>
  155. </dependency>
  156. </dependencies>
  157. <!--处理静态资源导出的问题-->
  158. <build>
  159. <resources>
  160. <resource>
  161. <directory>src/main/resources</directory>
  162. <includes>
  163. <include>**/*.properties</include>
  164. <include>**/*.xml</include>
  165. </includes>
  166. <filtering>true</filtering>
  167. </resource>
  168. <resource>
  169. <directory>src/main/java</directory>
  170. <includes>
  171. <include>**/*.properties</include>
  172. <include>**/*.xml</include>
  173. </includes>
  174. <filtering>true</filtering>
  175. </resource>
  176. </resources>
  177. </build>
#db.properties
jdbc.username=root
jdbc.password=123456
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8
jdbc.driver=com.mysql.jdbc.Driver
jdbc.maxPoolSize=20
jdbc.minPoolSize=5
<!--log4j.xml-->
<?xml version="1.0" encoding="GB2312" ?>
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">

<log4j:configuration debug="true">

    <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
        <param name="Encoding" value="UTF-8" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m  (%F:%L) \n" />
        </layout>
    </appender>
    <logger name="java.sql">
        <level value="debug" />
    </logger>
    <logger name="org.apache.ibatis">
        <level value="info" />
    </logger>
    <root>
        <level value="debug" />
        <appender-ref ref="STDOUT" />
    </root>
</log4j:configuration>
<!--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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--导入配置文件-->
    <import resource="classpath:spring/spring-mvc.xml"/>
    <import resource="classpath:spring/spring-service.xml"/>
    <import resource="classpath:spring/spring-dao.xml"/>

</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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--1. 包扫描,只扫描controller-->
    <context:component-scan base-package="com.siki.controller"/>

    <!--2. 配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--3. SpringMVC的标配-->
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>

    <!--4. 配置文件上传解析器-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="#{1024 * 1024 * 20}"/>
        <property name="defaultEncoding" value="utf-8"/>
    </bean>

</beans>
<!--spring-service.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!--1. 包扫描-->
    <context:component-scan base-package="com.siki.service"/>

    <!--2. 配置事务管理器-->
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--3. 基于xml配置,操作事务-->
    <aop:config>
        <!--配置切入点表达式-->
        <aop:pointcut id="point" expression="execution(* com.siki.*.*(..))"/>
        <aop:advisor advice-ref="myTx" pointcut-ref="point"/>
    </aop:config>

    <!--4. 配置事务增强-->
    <tx:advice transaction-manager="dataSourceTransactionManager" id="myTx">
        <!--配置事务属性-->
        <tx:attributes>
            <!--所有方法全为事务方法-->
            <tx:method name="*" rollback-for="java.lang.Exception"/>
            <tx:method name="get*" read-only="true"/>
        </tx:attributes>
    </tx:advice>

</beans>
<!--spring-dao-->
<?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"
       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">

    <!--1. 配置数据源-->
    <context:property-placeholder location="classpath:spring/db.properties"/>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="driverClass" value="${jdbc.driver}"/>
        <!--其他属性-->
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>
        <property name="minPoolSize" value="${jdbc.minPoolSize}"/>
    </bean>

    <!--2. 配置mybatis操作数据库,这两步是mybatis整合spring的关键-->
    <!--可以根据SqlSessionFactoryBean获取SqlSessionFactory对象-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--配置mybatis文件位置-->
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
        <property name="dataSource" ref="dataSource"/>
        <!--配置mapper映射文件位置-->
        <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/>
    </bean>

    <!--3. 我们要把每一个dao接口的实现加入到IOC容器中,在使用service的时候,直接@AutoWired自动注入即可-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--注入sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--指定dao接口所在的包-->
        <property name="basePackage" value="com.siki.dao"/>
    </bean>

</beans>
<!--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="4.0">

    <!--1. 配置前端控制器-->
    <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/applicationContext.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>

    <!--2. 配置字符编码过滤器-->
    <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>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>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--3. 配置过滤器,支持Rest风格的PUT和DELETE-->
    <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>

    <!--4. session-->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>

</web-app>
<!--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>

    <!--这里能配置的属性在Spring配置文件中都可以配置,我们只需要在这里写一些setting设置就行-->

</configuration>


<!--EmployeeDao.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="com.siki.dao.EmployeeDao">

    <select id="getEmpById" resultType="com.siki.bean.Employee">
        select * from employee where id = #{id}
    </select>

</mapper>
<!--index.jsp-->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
    <%
      request.setAttribute("path",request.getContextPath());
    %>
    <a href="${path}/getEmp?id=1">查询员工</a>
  </body>
</html>


<!--success.jsp-->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <font color="#98fb98">成功</font><br>
    ${emp}
</body>
</html>
//EmployeeDao
public interface EmployeeDao {

    public Employee getEmpById(Integer id);

}


//EmployeeService
@Service
public class EmployeeService {

    @Autowired
    private EmployeeDao employeeDao;

    public Employee getEmp(Integer id){
        Employee employee = employeeDao.getEmpById(id);
        return employee;
    }

}
//测试
@Controller
public class SSMController {

    @Autowired
    private EmployeeService employeeService;

    @RequestMapping("/getEmp")
    public String test(@RequestParam(value = "id",defaultValue = "1") Integer id, Model model){
        Employee emp = employeeService.getEmp(id);
        model.addAttribute("emp",emp);
        System.out.println(emp);
        return "success";
    }

}