SSM整合

基本SSM整合

pom.xml

<properties>
<!-- 自定义版本号 -->
 <spring.version>4.3.8.RELEASE</spring.version>
 <!-- mybatis.jar -->
 <mybatis.version>3.4.6</mybatis.version>
 <mybatis.spring.version>1.3.0</mybatis.spring.version>

 <!-- mysql  -->
 <mysql.version>5.1.42</mysql.version>
 <druid.version>1.1.10</druid.version>
 <!-- 其它依赖jar -->
 <jackson.version>2.8.8</jackson.version>
 <lombok.version>1.16.20</lombok.version>
 <jstl.version>1.2</jstl.version>
 <jstl.standard.version>1.1.2</jstl.standard.version>
 <log4j.version>1.2.17</log4j.version>
 <cglib.nodep.version>3.1</cglib.nodep.version>
 <commons.fileupload.version>1.3.1</commons.fileupload.version>
 <github.pagehelper.version>5.1.10</github.pagehelper.version>
</properties>

<dependencies>
<dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.12</version>
   <scope>test</scope>
 </dependency>


 <!-- Spring jar -->
 <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-test</artifactId>
   <version>${spring.version}</version>
 </dependency>
 <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>${spring.version}</version>
 </dependency>
 <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>${spring.version}</version>
 </dependency>
 <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-beans</artifactId>
   <version>${spring.version}</version>
 </dependency>
 <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-web</artifactId>
   <version>${spring.version}</version>
 </dependency>
 <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>${spring.version}</version>
 </dependency>
 <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-aop</artifactId>
   <version>${spring.version}</version>
 </dependency>
 <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-jdbc</artifactId>
   <version>${spring.version}</version>
 </dependency>
 <dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjrt</artifactId>
   <version>1.8.0</version>
 </dependency>
 <dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjweaver</artifactId>
   <version>1.8.0</version>
 </dependency>
 <!-- SpringMVC文件上传.jar -->
 <dependency>
   <groupId>commons-fileupload</groupId>
   <artifactId>commons-fileupload</artifactId>
   <version>${commons.fileupload.version}</version>
 </dependency>

 <!-- mybatis -->
 <dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis</artifactId>
   <version>${mybatis.version}</version>
 </dependency>
 <dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis-spring</artifactId>
   <version>${mybatis.spring.version}</version>
 </dependency>
 <!-- mybatis 支持延迟加载 cglib-nodep -->
 <dependency>
   <groupId>cglib</groupId>
   <artifactId>cglib-nodep</artifactId>
   <version>${cglib.nodep.version}</version>
 </dependency>

 <!-- Mysql -->
 <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>${mysql.version}</version>
 </dependency>

 <!-- druid连接池 -->
 <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid</artifactId>
   <version>${druid.version}</version>
 </dependency>

 <!-- jackson -->
 <dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-databind</artifactId>
   <version>${jackson.version}</version>
 </dependency>

 <!--JSTL标签 -->
 <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
   <version>${jstl.version}</version>
 </dependency>
 <dependency>
   <groupId>taglibs</groupId>
   <artifactId>standard</artifactId>
   <version>${jstl.standard.version}</version>
 </dependency>

 <!-- lombok -->
 <dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <version>${lombok.version}</version>
   <scope>provided</scope>
 </dependency>

 <!-- log4j -->
 <dependency>
   <groupId>log4j</groupId>
   <artifactId>log4j</artifactId>
   <version>${log4j.version}</version>
 </dependency>

 <!-- 日志需要 -->
 <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-api</artifactId>
   <version>1.7.21</version>
 </dependency>
 <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-log4j12</artifactId>
   <version>1.7.21</version>
 </dependency>


 <!-- mybatis 分页插件 pagehelper -->
 <dependency>
   <groupId>com.github.pagehelper</groupId>
   <artifactId>pagehelper</artifactId>
   <version>${github.pagehelper.version}</version>
 </dependency>
</dependencies>

完整web.xml

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://xmlns.jcp.org/xml/ns/javaee"
      xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
      id="WebApp_ID" version="3.1">


<!-- 加载核心配置文件第一种方式: -->
<listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>classpath*:spring_*.xml</param-value>
</context-param>

<!--SpringMVC中文乱码请求处理 -->
<filter>
 <filter-name>springMVCEncoding</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>forceEncoding</param-name>
   <param-value>true</param-value>
 </init-param>
</filter>
<filter-mapping>
 <filter-name>springMVCEncoding</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping>

<!-- SpringMVC核心拦截器 -->
<servlet>
 <servlet-name>springMVC</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <!-- 配置通用的Spring核心配置文件,默认会从WEB-INF/<servlet-name>-servlet.xml下载 可通过configContextLocation自定义配置文件名称和位置 -->
 <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:spring_mvc.xml</param-value>
 </init-param>

</servlet>
<servlet-mapping>
 <servlet-name>springMVC</servlet-name>
 <url-pattern>/</url-pattern>
</servlet-mapping>

<!-- RestFul风格 当前浏览器只支持GET和POST,需要通过配置让其支持REST风格 PUT DELETE 需要 HiddenHttpMethodFileter -->
<!-- /和 /* 区别: /* 当url-pattern配置成/*的时候,Tomcat会将所有的请求交给对应的Servlet进行处理 / 多数情况下与/*效果一致,但是,当访问的路径正好对应jsp文件时,Tomcat会访问真实的jsp文件而不是把请求交给对应的Servlet处理 -->
<filter>
 <filter-name>httpMethod</filter-name>
 <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
 <filter-name>httpMethod</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>

完整spring_core.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
   http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
   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/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 IOC容器 扫描 base-package   url-pattren=**/*.class
 注解:@Repository @Service @Controller  @Compont
 com.qfjy.project.weixin.servlet    
 作用:SpringIOC容器扫描 创建对象。
 约定大于配置。
-->

<context:component-scan   base-package="com.qfjy"   
   use-default-filters="true">
   <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- AOP配置    -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<context:property-placeholder location="classpath:jdbc.properties" />

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" 
   init-method="init" destroy-method="close">
   <property name="driverClassName" value="${jdbc_driver}" />
   <property name="url" value="${jdbc_url}" />
   <property name="username" value="${jdbc_user}" />
   <property name="password" value="${jdbc_password}" />
   <!-- 配置初始化大小、最小、最大 -->
   <property name="initialSize" value="1" />
   <property name="minIdle" value="1" />
   <property name="maxActive" value="10" />

   <!-- 配置获取连接等待超时的时间 -->
   <property name="maxWait" value="10000" />

   <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
   <property name="timeBetweenEvictionRunsMillis" value="60000" />

   <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
   <property name="minEvictableIdleTimeMillis" value="300000" />

   <property name="testWhileIdle" value="true" />

   <!-- 这里建议配置为TRUE,防止取到的连接不可用 -->
   <property name="testOnBorrow" value="true" />
   <property name="testOnReturn" value="false" />

   <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
   <property name="poolPreparedStatements" value="true" />
   <property name="maxPoolPreparedStatementPerConnectionSize"
      value="20" />
   <!-- 这里配置提交方式,默认就是TRUE,可以不用配置 -->
   <property name="defaultAutoCommit" value="true" />
   <!-- 验证连接有效与否的SQL,不同的数据配置不同 -->
   <property name="validationQuery" value="select 1 " />

   <!-- 配置监控统计拦截的filters -->
   <property name="filters" value="stat" />

</bean>


<!--2 Spring JDBC模版 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
   <property name="dataSource" ref="dataSource"></property>
</bean>


<!--使用MyBatis    spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <!-- mappers 配置文件映射 -->
    <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
    <!-- 别名设置   将该包内所有的类定义了别名,别名就是其类名-->
    <property name="typeAliasesPackage" value="com.qfjy.bean"></property>
    
    <property name="plugins">
    <array>
      <bean class="com.github.pagehelper.PageInterceptor">
        <property name="properties">
          <!--使用下面的方式配置参数,一行配置一个 -->
          <value>
            helperDialect=mysql
          </value>
        </property>
      </bean>
    </array>
  </property>
   
 </bean> 
 <!-- 配置对应的SqlSession -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
       <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
</bean>
 
<!-- MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
   <property name="basePackage" value="com.qfjy.mapper"></property>
   <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>    

 <!-- 配置 事务管理器   -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   <property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 启用声明式注解事务  -->
<tx:annotation-driven transaction-manager="transactionManager" />
</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" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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 
     http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx.xsd
     ">
<!-- base-package:用于扫描的包 resource-pattern:扫描的目录,默认值 **/*.calss 默认是级联,(当前包、和所有子包.) 
   use-default-filters 默认为true @Component, @Repository, @Service, @Controller 
   作用:Spring容器来管理这些对象(对象的创建) -->
<context:component-scan base-package="com.qfjy.web.controller"
use-default-filters="false" >

<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

</context:component-scan>

<mvc:default-servlet-handler />
<mvc:annotation-driven />
</beans>

jdbc.properties

#### key=value
jdbc_url=jdbc:mysql://localhost:3306/mybatis
jdbc_driver=com.mysql.jdbc.Driver
jdbc_user=root
jdbc_password=root

单元测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring_core.xml")
public class UserServiceTest {
 @Autowired
 private UserService userService;
 @Test
 public void t1(){
     System.out.println(userService);
 }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值