目前java的三大框架整合比较热门的是spring+springMVC+mybatis。今天我使用maven来搭建ssm框架实现简单的增删改查。本人也是小白一枚,算是对自己的一次总结吧。大神们勿喷……
关于maven的使用,可以自行百度~~~~~~~~
创建maven项目以及首次运行项目时暂时发现可能会遇到几个问题,在这里说明一下。
1.创建maven项目后,只有src/main/resource这一个目录,正确的是三个类似这样的目录?
解决办法:右击项目名-->Build Path-->configure Build Path…--->找到Libraries--->然后双击弹出Edit Library 框,选择Alternate JRE -->finish--->ok即可
2.maven项目中的jsp文件报错,出现红叉?
解决办法:右击项目名-->Build Path-->configure Build Path…--->找到Libraries--->选择Add Library…-->选中Server Runtime ---->next ---->Apache Tomcat v7.0 ---finish-->ok 即可
3.将配置文件以及需要的jar包全部放入后,项目名报错,出现红叉?
解决办法:右击项目名-->maven-->Update Project… -->ok此时项目名出现红色感叹号。木事。继续…
此时需要更改三次:
a.右击项目名-->Build Path-->configure Build Path…--->找到Libraries,此时会看到有一处出现红叉,还是选择Alternate JRE-->finish--->Apply。此时先别OK。
b.在这个界面左边找到javaCompiler 并将Compilercompliance level的数值改为1.7(根据自己安装的版本来)-->Apply。
c.再找到ProjectFacets。将里面的java这个后面的值也改为1.7.然后Apply 。最后OK。错误消失
4.首次运行程序,控制台报错:java.lang.ClassNotFoundException:org.springframework.web.context.ContextLoaderListener和java.lang.ClassNotFoundException:org.springframework.web.util.IntrospectorCleanupListener?
解决办法:右击项目名-->Build Path-->configure Build Path…--->然后再左边的版块里找到Deployment Assembly这一项。选中,然后再右侧点击Add,弹出对话框选中Java Build Path Entries -->选中MavenDependencies-->finish-->Apply-->ok即可。重新运行,成功!
------------------可爱的分割线--------------------
OK,现在开始搭建环境吧!首先我们需要的是jar包。maven项目下,jar包都是存放在pom.xml文件下。里面都是jar包依赖。推荐大家一个网址:http://mvnrepository.com/tags/maven。以后大家需要什么jar包直接进去搜就OK啦。
-------------------1.pom.xml ---->jar包已经备注,缺什么要什么
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.stuSys</groupId>
<artifactId>stuSys</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>stuSys Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<!-- spring版本号 -->
<spring.version>4.0.2.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.2.6</mybatis.version>
<!-- log4j日志文件管理包版本 -->
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-juli</artifactId>
<version>8.5.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<!-- 表示开发的时候引入,发布的时候不会加载此包 -->
<scope>test</scope>
</dependency>
<!-- spring核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- mybatis/spring包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- 导入Mysql数据库链接jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<!-- JSTL标签类 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 日志文件管理包 -->
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0.20100224</version>
</dependency>
<!-- 格式化对象,方便输出日志 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log end -->
<!-- 映入JSON -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- 上传组件包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<finalName>stuSys</finalName>
</build>
</project>
------------------------------------------------------------
&&&&&前面已经把需要的jar包依赖放到了pom.xml文件里。既然是SSM框架整合,那么所需要的配置文件肯定少不了。这些配置文件可以放在src/main/source目录下。web.xml是放在web-inf下。
-----------------------------------------------------------------------------------------
-----jdbc.properties:
-----这个文件是连接数据库的驱动以及用户名和密码。类似于定义变量名。方便spring配置文件里的引用。比较省事。
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/users?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull
username=root
password=bdqn
initialSize=0
maxActive=20
maxIdle=20
minIdle=1
maxWait=60000
----------------------------------------------------------------------------
----log4j.properties:
----这个文件是log日志文件,方便在控制台上打印输出。
#定义LOG输出级别
log4j.rootLogger=INFO,Console,File
#定义日志输出目的地为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以灵活地指定日志输出格式,下面一行是指定具体的格式
log4j.appender.Console.layout =org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c]- %m%n
#文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.File =org.apache.log4j.RollingFileAppender
#指定输出目录
log4j.appender.File.File = logs/ssm.log
#定义文件最大大小
log4j.appender.File.MaxFileSize = 10MB
# 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
---------------------------------------------------------------------------
--------springMVC配置文件
------这个文件是springMVC的配置文件,里面配置了视图解析器等springMVC的相关配置信息。
<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
<context:component-scanbase-package="com.topsun.controller" />
<!--避免IE执行AJAX时,返回JSON出现下载文件 -->
<beanid="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<propertyname="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<propertyname="messageConverters">
<list>
<refbean="mappingJacksonHttpMessageConverter" /> <!--JSON转换器 -->
</list>
</property>
</bean>
<!-- 定义跳转的文件的前后缀 ,视图模式配置-->
<beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<propertyname="prefix" value="/WEB-INF/jsp/" />
<propertyname="suffix" value=".jsp"/>
</bean>
<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<!--<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> -->
<!-- 默认编码 -->
<!--<propertyname="defaultEncoding" value="utf-8" /> -->
<!-- 文件大小最大值 -->
<!--<property name="maxUploadSize" value="10485760000" /> -->
<!-- 内存中的最大值 -->
<!--<property name="maxInMemorySize"value="40960" />
</bean> -->
</beans>
----------------------------------------------------------------------------------------
--------spring-mybatis.xml
------这个是spring和mybatis整合的配置文件。
<?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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自动扫描 -->
<context:component-scanbase-package="com.topsun" />
<!-- 引入配置文件 -->
<beanid="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<propertyname="location" value="classpath:jdbc.properties" />
</bean>
<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<propertyname="driverClassName" value="${driver}" />
<propertyname="url" value="${url}" />
<propertyname="username" value="${username}" />
<propertyname="password" value="${password}" />
<!-- 初始化连接大小 -->
<propertyname="initialSize"value="${initialSize}"></property>
<!-- 连接池最大数量 -->
<propertyname="maxActive" value="${maxActive}"></property>
<!-- 连接池最大空闲 -->
<propertyname="maxIdle" value="${maxIdle}"></property>
<!-- 连接池最小空闲 -->
<propertyname="minIdle" value="${minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<propertyname="maxWait" value="${maxWait}"></property>
</bean>
<!--spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<propertyname="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<propertyname="mapperLocations"value="classpath*:com/topsun/mapping/*.xml"></property>
</bean>
<!--DAO接口所在包名,Spring会自动查找其下的类 -->
<beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
<propertyname="basePackage" value="com.topsun.dao" />
<propertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"></property>
</bean>
<!--(事务管理)transactionmanager, use JtaTransactionManager for global tx -->
<beanid="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource" ref="dataSource" />
</bean>
</beans>
------------------------------------------------------------------------------
-----web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>Archetype Created Web Application</display-name>
<!-- Spring和mybatis的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</context-param>
<!-- 编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止Spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- Spring MVC servlet -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
----------------------------------------------------------------------------------
&&&&&&&以上是ssm框架整合所需要的所有配置文件。里面都有注释,这里就不去赘述了。
**********************未完待续……
来源:oschina
链接:https://my.oschina.net/u/4341499/blog/4341759