SSM框架搭建
来源:互联网 发布:apache负载均衡 编辑:程序博客网 时间:2024/06/14 16:28
第一步:创建maven项目 <dependencies> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- spring --> <!-- spring面向切面编程相关的jar包 --> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.10</version> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.10</version> <!-- springmvc --> <!-- springmvc 所需架包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.5.RELEASE</version> <!-- springmvc 返回数据是json的架包 --> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.7</version> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.4</version> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> <!-- io --> <!-- 文件上传需要的jar --> <!-- log4j依賴 --> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.6.2</version> </dependency> </dependencies> xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 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/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 1、读取jdbc.properties配置文件--> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations" value="classpath:jdbc.properties"/> <tx:method name="get*" read-only="true"/> <tx:method name="select*" read-only="true"/> <!-- 除了上面定义的方法之外,其他方法都要通过事务处理 --> PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <setting name="mapUnderscoreToCamelCase" value="true"/> <setting name="cacheEnabled" value="true"/> <setting name="lazyLoadingEnabled" value="true"/> <setting name="multipleResultSetsEnabled" value="true"/> <setting name="useColumnLabel" value="true"/> <setting name="useGeneratedKeys" value="false"/> <setting name="autoMappingBehavior" value="PARTIAL"/> <setting name="autoMappingUnknownColumnBehavior" value="WARNING"/> <setting name="defaultExecutorType" value="SIMPLE"/> <setting name="defaultStatementTimeout" value="25"/> <setting name="defaultFetchSize" value="100"/> <setting name="localCacheScope" value="SESSION"/> <setting name="safeRowBoundsEnabled" value="false"/> <setting name="jdbcTypeForNull" value="OTHER"/> <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/> <typeAliases> <package name="com.hnqy.entity"/> <!-- 根据mybatis定义不同的数据库厂商标识 --> <!-- 映射的sql文件 -->
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 支持注解 --> <mvc:annotation-driven></mvc:annotation-driven> <!--定义注解要扫描的包 --> <context:component-scan base-package="com.hnqy.controller"></context:component-scan> <!-- 对静态资源的处理 --> <mvc:default-servlet-handler/> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <bean id="multipartResolver" </beans> <!-- 应用启动加载spring配置文件 指定配置文件位置 -->
第二步:更改jdk及compile版本
第三步:添加dynamic web model 支持
第四步:添加架包(pom.xml)
所需架包:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<!-- spring jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
</dependency>
<dependency>
</dependency>
</dependency>
<dependency>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.31</version>
</dependency>
</dependency>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.8</version>
</dependency>
<!-- jstl表达式相关的jar包 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
第五步: beans.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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
</bean>
<!-- 2、配置 jdbc 数据源 dataSource-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driverClassName}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- 3、配置mybatis的sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 引入mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!-- 4、配置mybatis自动扫描sql映射文件/接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hnqy.mapper"></property>
</bean>
<!-- 5、定义事务所在的bean -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 6、支持注解驱动的事务管理,指定事务管理器 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 7、aspectj支持自动代理实现AOP功能 -->
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
<!-- 8、定义事务的切面和切入点信息 -->
<!-- 定义事务的属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- read-only="true" 事务隔离级别最低,性能最高 -->
<tx:method name="list*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice>
<!-- 定义事务所使用的切入点 -->
<aop:config>
<aop:pointcut expression="execution(* com.hnqy.service.impl.*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config>
<!-- 定义spring扫描的包 -->
<context:component-scan base-package="com.hnqy.service.impl"></context:component-scan>
<context:component-scan base-package="com.hnqy.mapper"></context:component-scan>
</beans>
第六步:mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
<configuration>
<!-- properties属性配置文件的引入 -->
<!-- setting设置,调整mybatis的运行行为 -->
<settings>
<!-- 是否开启自动驼峰命名规则(camel case)映射 -->
</settings>
<!-- 类的别名 -->
</typeAliases>
<!-- 设置要使用的数据库环境 -->
</configuration>
第七步: springmvc-servlet.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:xsi="http://www.w3.org/2001/XMLSchema-instance"
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 文件上传 -->
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为1MB -->
<property name="maxUploadSize">
<value>1048576</value>
</property>
</bean>
第八步:修改web.xml,设置应用启动加载spring、springmvc
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 配置spring的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 针对post请求,防止取数据乱码 -->
<filter>
<filter-name>characterEncoding</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>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- springmvc 的核心分发器DispatcherServlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化 DispatcherServlet 的时候,同时也会初始化spring容器,默认配置文件路径为/WEB-INF/<servlet-name>-servlet.xml。 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- “/” 为tomcat默认的请求处理器,所有请求都交给该servlet来处理。 默认为tomcat/conf/web.xml中配置的org.apache.catalina.servlets.DefaultServlet -->
<url-pattern>/</url-pattern>
</servlet-mapping>
第九步:创建相对应的包和类
第十步:测试
阅读全文
0 0
- SSM框架的搭建
- SSM框架搭建
- SSM框架搭建
- SSM框架搭建
- SSM框架搭建
- SSM框架搭建
- SSM框架搭建
- 初学搭建SSM框架
- MyEclipse2014搭建SSM框架
- ssm框架搭建
- Maven搭建SSM框架
- SSM框架搭建
- SSM 框架 搭建
- SSM框架快速搭建
- ssm框架的搭建
- SSM 框架 搭建
- 搭建SSM框架
- 搭建ssm框架
- POJ1007算法解析
- TensorFlow分布式全套(原理,部署,实例)
- UITableView的基本使用方法
- B
- 数据库主从
- SSM框架搭建
- Android Studio配置svn时提示authentication required(需要证书)的解决方法
- Unity和Android之间的交互
- 你知道成功的研发团队应具备哪这9大属性吗?
- opencv球体追踪
- javaweb动态导出指定格式文件,并写入内容动态生成
- C++ string自定义辅助方法汇总
- 第二章 第二节:操作数据库
- 我的java生涯