SSM框架搭建

Posted zl-huang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM框架搭建相关的知识,希望对你有一定的参考价值。

一、配置数据库连接参数,用于引入到数据库连接池:

db.properties文件:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:端口号/数据库名?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=数据库连接密码
jdbc.maxActive=10
jdbc.minIdle=5

二、DAO层配置,也就是Mybatis配置:

1、mybatis默认配置文件(sqlMapConfig.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>
//配置在其他地方,这里不做配置
</configuration>

2、mybatis基本配置(applicationContext-dao.xml):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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-4.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 读取db.properties,通过bean的方式创建,spEL方式取值(#{dbConfig.xxx}) 
    <util:properties id="dbConfig" location="classpath:db.properties"></util:properties>
     -->
    <!-- 配置数据库连接池 -->
    <!-- 加载配置文件db.properties数据库连接参数配置 -->
    <context:property-placeholder location="classpath:resource/db.properties" />
    <bean id="dataSource"
        class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="url" value="${jdbc.url}"></property>
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="maxActive" value="${jdbc.maxActive}"></property>
        <property name="minIdle" value="${jdbc.minIdle}"></property>
    </bean>
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- mybatis默认文件在哪里 -->
        <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"></property>
        <!--XML文件在哪里 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
        <!-- 数据源在哪里 -->
        <property name="dataSource" ref="dataSource"></property>
        
    </bean>
    <!-- 配置扫描包,加载mapper的代理对象 -->
        <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="top.hzelin.jingxi.mapper"></property>
        </bean>
</beans>    

三、Service层配置,包括事务配置(applicationContext-service.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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-4.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    <!-- 扫描包, 加载service实现类-->
    <context:component-scan base-package="top.hzelin.jingxi.service" />
    
    <!-- 事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <!-- 通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="create*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>    
        </tx:attributes>
    </tx:advice>
    <!-- 切面,注意需要依赖:

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>

 -->
    <aop:config >
        <aop:advisor advice-ref="txAdvice" 
        pointcut="execution(* top.hzelin.jingxi.service.*.*(..))"/>
    </aop:config>    
</beans>    

四、Controller层配置(springmvc.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:p="http://www.springframework.org/schema/p"
    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/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描包,加载controller实现类 -->
<context:component-scan base-package="top.hzelin.jingxi.controller" />
<!--注解驱动 -->
<mvc:annotation-driven />
<!-- 视图解析器 -->
<bean id="viewResolver"  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="WEB-INF/jsp/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>
<!--资源映射 -->
  <mvc:resources location="/WEB-INF/css/" mapping="/css/**" />
  <mvc:resources location="/WEB-INF/js/" mapping="/js/**" />
</beans>

五、web.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>jingxi-manager-web</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- 加载spring容器 -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/applicationContext-*.xml</param-value>
  </context-param>
  <!-- 配置监听器 -->
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 过滤器处理乱码 -->
  <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>
  <!-- 前端可控制器 -->
  <servlet>
      <servlet-name>SpringMVC</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation,
       springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring/springmvc.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>SpringMVC</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>
  
</web-app>

 

以上是关于SSM框架搭建的主要内容,如果未能解决你的问题,请参考以下文章

SSM 项目整合详细解读

SSM-框架搭建-tank后台学习系统

[转]SSM(Spring+SpringMVC+Mybatis)框架搭建详细教程附源代码Demo

ssm框架的搭建

IDEA 使用IDEA搭建SSM框架项目

使用IDEA搭建SSM框架