您的位置:首页 > 编程语言 > Java开发

java ssm框架学习——三大框架整合

2017-08-17 11:38 288 查看

1.  导入所有ssm需要jar包

2.  创建config资源文件夹

 


3.  整合MyBatis

3.1. 导入db.properties

db.driver=com.mysql.jdbc.Driver

db.url=jdbc:mysql://localhost:3306/monthtest?useUnicode=true&characterEncoding=utf8

db.username=root

db.password=root


 

3.2. 导入log4j.properties

# Global logging configuration

log4j.rootLogger=DEBUG, stdout

# Console output...

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n


 

3.3. 配置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>

        <!-- 
批量设置别名 -->

        <typeAliases>

            <package name="com.jiyun.ssm.pojo" />

        </typeAliases>

    </configuration>


 

3.4. 配置dao层配置文件  applicationContext-dao.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"

    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-3.2.xsd 

        http://www.springframework.org/schema/mvc 

        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 

        http://www.springframework.org/schema/context 

        http://www.springframework.org/schema/context/spring-context-3.2.xsd 

        http://www.springframework.org/schema/aop 

        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 

        http://www.springframework.org/schema/tx 

        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

    <!-- 
加载java配置文件 -->

    <context:property-placeholder location="classpath:db.properties" />

    <!-- 
配置数据源 -->

    <!-- 
配置数据源 -->

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"

        destroy-method="close">

        <property name="driverClass" value="${db.driver}" />

        <property name="jdbcUrl" value="${db.url}" />

        <property name="user" value="${db.user}" />

        <property name="password" value="${db.password}" />

    </bean>

    <!-- 
配置sqlSessionFactory -->

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

        <!-
f0c1
- 
配置mybatis的全局配置文件 -->

        <property name="configLocation" value="classpath:SqlMapConfig.xml" />

        <!-- 
配置数据源 -->

        <property name="dataSource" ref="dataSource"></property>

    </bean>

    <!-- 
扫描需要生产代理对象的mapper -->

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

        <!-- 
需要扫描的mapper的包 -->

        <property name="basePackage" value="com.jiyun.ssm.dao" />

    </bean>

</beans>


3.5. 配置Service层文件

    <?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"

    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-3.2.xsd 

        http://www.springframework.org/schema/mvc 

        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 

        http://www.springframework.org/schema/context 

        http://www.springframework.org/schema/context/spring-context-3.2.xsd 

        http://www.springframework.org/schema/aop 

        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 

        http://www.springframework.org/schema/tx 

        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

 

    <!-- 
通过扫描组件,扫描servcie的bean包 -->

    <context:component-scan base-package="com.jiyun.ssm.service" />

 

    <!-- 
事务管理器 -->

    <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="*" propagation="REQUIRED" />

            <tx:method name="find*" read-only="true" />

            <tx:method name="query*" read-only="true" />

            <tx:method name="select*" read-only="true" />

            <tx:method name="get*" read-only="true" />

        </tx:attributes>

    </tx:advice>

    <!-- 
配置AOP -->

    <aop:config>

        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.jiyun.ssm.service.*.*(..))"/>

    </aop:config>

</beans>


4.  SpringMVC整合

4.1. 创建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:mvc="http://www.springframework.org/schema/mvc"

    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-3.2.xsd 

        http://www.springframework.org/schema/mvc 

        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 

        http://www.springframework.org/schema/context 

        http://www.springframework.org/schema/context/spring-context-3.2.xsd 

        http://www.springframework.org/schema/aop 

        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 

        http://www.springframework.org/schema/tx 

        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

    </beans>


 

4.2. 配置springmvc.xml

4.2.1.1.         组件扫描器扫描controller的bean所在包

<!-- 
组件扫描器扫描controller的bean所在包 -->

    <context:component-scan base-package="com.jiyun.ssm.controller" />


4.2.1.2.         配置注解的处理器映射器和适配器

<mvc:annotation-driven />


4.2.1.3.         视图解析器

 <bean

        class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix" value="/WEB-INF/jsp/" />

        <property name="suffix" value=".jsp" />

    </bean>


5.  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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"[/code] 
    id="WebApp_ID" version="2.5">

    <!-- 
配置springmvc的配置文件路径 -->
[/code]
    <context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>classpath:applicationContext-*.xml</param-value>

    </context-param>

 

    <!-- 
配置监听器 -->

    <listener>

        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

 

    <!-- 
配置前端控制器 -->

    <servlet>

        <servlet-name>springmvc</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <!-- 
配置springmvc的配置文件路径 -->

        <init-param>

            <param-name>contextConfigLocation</param-name>

            <param-value>classpath:springmvc.xml</param-value>

        </init-param>

    </servlet>

    <servlet-mapping>

        <servlet-name>springmvc</servlet-name>

        <!-- 
支持restful -->

        <url-pattern>*.action</url-pattern>

    </servlet-mapping>

</web-app>


 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息