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> |
相关文章推荐
- java 从零开始,学习笔记之基础入门<三大框架整合>(四十二)
- JavaWeb学习笔记之SSM三大框架整合详细教程
- 17年2月24日Java后台学习记录,Spring Maven更换版本报错,SSM框架整合,Mysql语句整理。
- Spring学习笔记----三大框架(Spring+SpringMVC+MyBatis)整合详细教程
- java三大框架学习之路---Struts2_HelloWorld
- SSM框架 SSM项目源码 SSM源码 下载 java框架整合Springmvc+mybatis+s
- java 三大框架整合
- java三大框架整合做权限管理操作
- Java新手如何学习Spring、Struts、Hibernate三大框架?
- java三大框架之Hibernate的个人学习
- 为什么学习Java三大框架SSH与MVC的设计模式
- java三大框架之Hibernate的个人学习
- Java新手如何学习Spring、Struts、Hibernate三大框架?
- java web ssh三大框架整合简单入门
- java三大框架学习总结(1)
- 【SSM框架 SSM项目源码 SSM源码 下载】java框架整合Springmvc+mybatis+shiro+bootstrap
- 学习java三大框架的总结
- Java新手如何学习Spring、Struts、Hibernate三大框架?(转)
- ssm框架学习03--springmvc和mybatis整合
- JAVA_WEB项目之Struts2、Spring和Hibernate三大框架的整合