快速搭建ssm框架
Posted fengzhaokang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了快速搭建ssm框架相关的知识,希望对你有一定的参考价值。
快速搭建SSM框架
因为最近有很多朋友问我自己的项目搭建的不够完善,并且经常出现一些小问题,那么今天我又整理了一下文档教大家如何快速搭建SSM框架我是用 eclipse搭建的,如果想用idear的话我过段时间再出一个
第一步:创建web项目
第二步:使用小黑鸟工具生成实体类,mapper接口,以及mapper.xml 不需要接口实现类(需要装插件)
第三步:创建service接口,创建service接口实现类,在类上添加@Service(需要提前引入SSM所需lib架包)
然后:调用***mapper dao 并且需要添加@注解共有两种 1:@Autowried 2:@Resource 一种按类型一种按名称
第四步:创建controller 类名上添加@Controller注解 声明这个是个控制器可以根据需求加@RequestMapping
第五步:添加配置文件 applicationContext database.propert mybatis-config springmvc-servlet
给大家附上我的配置文件,可能每个人和每个人的都不一样仅供参考哦
applicationContext.xml内容
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 9 http://www.springframework.org/schema/aop 10 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 11 http://www.springframework.org/schema/tx 12 http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 13 http://www.springframework.org/schema/context 14 http://www.springframework.org/schema/context/spring-context-3.2.xsd"> 15 <!-- 开启自动扫包 --> 16 <context:component-scan base-package="com.mai.dao,com.mai.Service" /> 17 <!-- 读取数据库配置文件 --> 18 <context:property-placeholder location="classpath:database.properties"/> 19 20 <!-- JNDI获取数据源(使用dbcp连接池) --> 21 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton"> 22 <property name="driverClassName" value="${driver}" /> 23 <property name="url" value="${url}" /> 24 <property name="username" value="${name}" /> 25 <property name="password" value="${pwd}" /> 26 </bean> 27 28 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 29 <property name="dataSource" ref="dataSource"></property> 30 <property name="configLocation" value="classpath:mybatis-config.xml"></property> 31 </bean> 32 33 <!-- 自动扫描所有的Mapper接口与文件 --> 34 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 35 <property name="basePackage" value="com.mai.dao"></property> 36 </bean> 37 38 <!-- 配置事务管理器 --> 39 <bean id="txManager" 40 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 41 <property name="dataSource" ref="dataSource"></property> 42 </bean> 43 44 <!-- 定义个通知,指定事务管理器 --> 45 <tx:advice id="txAdvice" transaction-manager="txManager"> 46 <tx:attributes> 47 <tx:method name="delete*" propagation="REQUIRED" /> 48 <tx:method name="add*" propagation="REQUIRED" /> 49 <tx:method name="update*" propagation="REQUIRED" /> 50 <tx:method name="get*" propagation="SUPPORTS" read-only="true"/> 51 <tx:method name="*" propagation="SUPPORTS" /> 52 </tx:attributes> 53 </tx:advice> 54 <aop:config> 55 <!-- 配置一个切入点 --> 56 <aop:pointcut id="serviceMethods" expression="execution(* com.mai.Service..*(..))" /> 57 <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" /> 58 </aop:config> 59 60 </beans>
mybatis-config.xmlneirong
<?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> <settings> <!-- changes from the defaults --> <setting name="lazyLoadingEnabled" value="false" /> </settings> <typeAliases> <!--这里给实体类取别名,方便在mapper配置文件中使用--> <package name="com.mai.entity"/> </typeAliases> </configuration>
springmvc-servlet.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.mai.controller"/> <mvc:annotation-driven conversion-service="myConvertService"> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json</value> </list> </property> <property name="features"> <list> <!-- Date的日期转换器 --> <value>WriteDateUseDateFormat</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> </bean> <mvc:resources mapping="/statics/**" location="/statics/" /> <!-- 完成视图的对应 --> <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 --> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="favorParameter" value="true"></property> <property name="mediaTypes"> <map> <entry key="html" value="text/html;charset=UTF-8"></entry> <entry key="json" value="application/json;charset=UTF-8"></entry> </map> </property> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </list> </property> </bean> <!-- 配置MultipartResolver,用于上传文件,使用spring的CommonsMultipartResolver --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="5000000"/> <property name="defaultEncoding" value="UTF-8"/> </bean> </beans>
第六步:配置完成后在web-inf 下创建一个jsp文件夹 里面放我们的jsp页面
配置web.xml 告诉tomcat我们先扫描那个再扫描哪个 编码集什么的
第七部: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_3_0.xsd" version="3.0"> <display-name>TestS</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <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>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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> </web-app>
然后 taomcat debug运行 然后 ping路径 localhost:端口/项目名/哪个控制器的/那个方法。buling~页面出来了 也就是配置成功了
可以试试看
以上是关于快速搭建ssm框架的主要内容,如果未能解决你的问题,请参考以下文章