Maven-SSM框架整合
Posted 隐语者
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Maven-SSM框架整合相关的知识,希望对你有一定的参考价值。
1、创建Maven项目
配置pom.xml依赖
<!-- 允许创建jsp页面 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <!-- jsp页面标签 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- jsp API --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> <!-- servlet Api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <!-- SSM依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-instrument</artifactId> <version>4.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-instrument-tomcat</artifactId> <version>4.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>4.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-messaging</artifactId> <version>4.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>4.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc-portlet</artifactId> <version>4.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-websocket</artifactId> <version>4.2.1.RELEASE</version> </dependency> <!-- mybatis核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> </dependency> <!-- jackjson,支持接口返回输出json格式 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.2.1</version> </dependency> <!--打印日志 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- 文件上传处理 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency>
<build>
<finalName>SSM-project</finalName>
<plugins>
<!--JDK版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
<!-- Tomcat7插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<!-- tomcat7的插件, 不同tomcat版本这个也不一样 -->
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<!-- 通过maven tomcat7:run运行项目时,访问项目的端口号 -->
<port>8080</port>
<!-- 项目访问路径 本例:localhost:9090, 如果配置的aa, 则访问路径为localhost:9090/aa -->
<path>/SpringMVC_Maven</path>
<uriEncoding>UTF-8</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>
2、导入db.properties
dataSource.driverName=com.mysql.jdbc.Driver dataSource.jdbcUrl=jdbc:mysql://{主机IP}/{数据库名称} dataSource.user={数据库用户名} dataSource.password={数据库密码}
导入日志:log4j.properties
log4j.rootLogger=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=../log/error.log log4j.appender.File.MaxFileSize=10MB 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,SSS}][%c]%m%n
3、配置springMVC配置文件:src/main/resources/springmvc/springmvc-config.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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" 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"> <!-- 扫描器,指定DispatcherServlet去扫描当前包名下的所有类, base-package属性不能精确到类级别,只能到包级别 --> <context:component-scan base-package="com.maven.controller" /> <!-- exception --> <context:component-scan base-package="com.maven.exception" /> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 解决@ResponseBody中文乱码 --> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8" /> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 文件上传配置 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- defaultEncoding:请求的编码格式必须和用户JSP的编码一致,以便正确读取表单中的内容。 uploadTempDir:文件上传过程中的临时目录,上传完成后,临时文件会自动删除 maxUploadSize:设置文件上传大小上限(单位为字节)-1为无限制 --> <property name="defaultEncoding" value="UTF-8" /> <property name="maxUploadSize" value="102400000" /> <!-- uploadTempDir可以不做设置,有默认的路径,上传完毕会临时文件会自动被清理掉 --> <property name="uploadTempDir" value="/upload/"></property> </bean> <!-- 处理器映射器 --> <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> --> <!-- 处理器适配器 --> <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" /> --> <!-- 配置注解驱动,配置之后就不用配置处理器映射器和处理器适配器了 --> <mvc:annotation-driven /> <!-- 静态资源不拦截 --> <mvc:resources location="/staitcSources/" mapping="/staitcSources/**" /> <!-- 配置拦截器,可以配置多个拦截器,按顺序执行 --> <!-- <mvc:interceptors> 第一个拦截器 <mvc:interceptor> <mvc:mapping path="/**" /> 所有请求都拦截 <mvc:exclude-mapping path="/staticRescoures/**"/> 静态资源不拦截 <bean class="org.sprigmvc.Interceptor.MyInterceptor1" /> </mvc:interceptor> 第二个拦截器 <mvc:interceptor> <mvc:mapping path="/**" /> 所有请求都拦截 <mvc:exclude-mapping path="/staticRescoures/**"/> 静态资源不拦截 <bean class="org.sprigmvc.Interceptor.MyInterceptor2" /> </mvc:interceptor> </mvc:interceptors> --> </beans>
4、配置mybaits配置文件:src/main/resources/mybaits/mybatis-config.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>
<typeAlias type="com.maven.pojo.animalPojo" alias="Animal"/>
<typeAlias type="com.maven.pojo.cttablePojo" alias="Cttable"/>
</typeAliases>
</configuration>
5、配置spring配置文件:/resources/spring/applicationContent-dao.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <!-- 数据源 --> <context:property-placeholder location="classpath:db.properties" ignore-unresolvable="true" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${dataSource.driverName}" /> <property name="url" value="${dataSource.jdbcUrl}" /> <property name="username" value="${dataSource.user}" /> <property name="password" value="${dataSource.password}" /> </bean> <!-- 会话工厂 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 载入mybaits配置文件 --> <property name="configLocation" value="classpath:mybaits/mybatis-config.xml"></property> <!-- 载入SQL映射文件 --> <property name="mapperLocations" value="classpath:com/maven/dao/*.xml"></property> </bean> <!-- 事务管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 事务注解驱动,注解@Transactional的类和方法将具有事务性 <tx:annotation-driven transaction-manager="txManager" />--> <!-- 配置事务增强处理Bean,指定事务管理器 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <!-- 用于配置详细的事务传播、隔离、是否只读等属性, 这些属性会封装到底层事务代码中的TransactionDefinition中 --> <tx:attributes> <!-- 所有以\'get\'开头的方法是read-only的 事务的隔离性isolation="DEFAULT" 事务的传播行为 propagation="REQUIRED" REQUIRED 支持当前的事务,如果不存在就创建一个新的。这是最常用的选择。 SUPPORTS 支持当前的事务,如果不存在就不使用事务。 MANDATORY 支持当前的事务,如果不存在就抛出异常。 REQUIRES_NEW 创建一个新的事务,并暂停当前的事务(如果存在)。 NOT_SUPPORTED 不使用事务,并暂停当前的事务(如果存在)。 NEVER 不使用事务,如果当前存在事务就抛出异常。 NESTED 如果当前存在事务就作为嵌入事务执行,否则与REQUIRED类似。 事务的超时 timeout="-1" -1为永不超时,8代表8秒超时 --> <tx:method name="get*,login*" read-only="true" isolation="DEFAULT" propagation="REQUIRED" timeout="-1" /> <!-- 其他方法使用默认的事务设置 --> <tx:method name="*" /> </tx:attributes> </tx:advice> <aop:config> <!-- 配置一个切入点,org.fkjava.service以及子包下面的所有类需要做事务 --> <aop:pointcut id="pointcut" expression="execution(* com.maven.service..*ServiceImpl.*(..))" /> <!-- 指定在pointcut切入点应用txAdvice事务增强处理 aop:advisor的作用是在已有的advice前提下,通过该元素指定将advice织入哪些位置 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" /> </aop:config> </beans>
配置spring-service层:/resources/spring/applicationContent-service.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <beans> <bean id="userdao" class="com.maven.service.impl.UserServiceImpl" p:sqlSessionFactory-ref="sqlSessionFactory"></bean> </beans> </beans>
6、配置web.xml
<!-- log4j配置 --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener </listener-class> </listener> <!-- Spring容器加载所有的配置文件的路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:spring/applicationContent-*.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 配置SpringMVC核心控制器 --> <servlet> <servlet-name>MVC</servlet-name> <!-- SpringMVC的中央处理器:DispathcherServlet有,其成为分派器, 把所有的请求url分派给其他的处理器 --> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc/springmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <!-- 加载的时候就启动 --> </servlet> <servlet-mapping> <servlet-name>MVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 解决中文乱码 --> <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> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 404页面 --> <error-page> <error-code>404</error-code> <location>/404.jsp</location> </error-page>
自此,最基本的配置就完成了,用的时候注意改相对应的配置文件地址
7、最简单的测试
UserLoginController:
package com.maven.controller; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.stereotype.Repository; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.portlet.ModelAndView; import com.maven.pojo.animalPojo; import com.maven.service.UserService; @Controller public class UserLoginController { @Resource(name="userdao") private UserService userservice; @RequestMapping(value="/home") @ResponseBody public ModelAndView login(ModelAndView mv) { List<animalPojo> list=userservice.getAllDate(); mv.setViewName("home"); mv.addObject("json", list); System.out.println(list); return mv; } }
animalMapper,java
package com.maven.dao; import java.util.List; import com.maven.pojo.animalPojo; public interface animalMapper { List<animalPojo> selectAllData(); }
animalMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.maven.dao.animalMapper"> <resultMap type="Animal" id="AnimalResultMap"> <id column="id" property="id"/> <result column="Species_id" property="SpeciesId"/> <result column="animal" property="animalName"/> </resultMap> <select id="selectAllData" resultMap="AnimalResultMap"> select *from animal </select> </mapper>
animalPojo.java
package com.maven.pojo; public class animalPojo { private int id; private String SpeciesId; private String animalName; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getSpeciesId() { return SpeciesId; } public void setSpeciesId(String speciesId) { SpeciesId = speciesId; } public String getAnimalName() { return animalName; } public void setAnimalName(String animalName) { this.animalName = animalName; } @Override public String toString() { return "animalPojo [id=" + id + ", SpeciesId=" + SpeciesId + ", animalName=" + animalName + "]"; } public animalPojo() { super(); // TODO Auto-generated constructor stub } public animalPojo(int id, String speciesId, String animalName) { super(); this.id = id; SpeciesId = speciesId; this.animalName = animalName; } }
cttablePojo.java
package com.maven.pojo; public class cttablePojo { private Integer id; private String Species; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getSpecies() { return Species; } public void setSpecies(String species) { Species = species; } @Override public String toString() { return "cttablePojo [id=" + id + ", Species=" + Species + "]"; } public cttablePojo() { super(); // TODO Auto-generated constructor stub } public cttablePojo(Integer id, String species) { super(); this.id = id; Species = species; } }
UserService.java
package com.maven.service; import java.util.List; import com.maven.pojo.animalPojo; public interface UserService { /** * 获取animal所有的数据 * */ List<animalPojo> getAllDate(); }
UserServiceImpl.java
package com.maven.service.impl; import java.util.List; import javax.annotation.Resource; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.springframework.stereotype.Repository; import com.maven.dao.animalMapper; import com.maven.pojo.animalPojo; import com.maven.service.UserService; public class UserServiceImpl implements UserService { private SqlSessionFactory sqlSessionFactory; public SqlSessionFactory getSqlSessionFactory() { return sqlSessionFactory; } public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { this.sqlSessionFactory = sqlSessionFactory; } public UserServiceImpl() {} @Override public List<animalPojo> getAllDate() { // TODO Auto-generated method stub SqlSession session=sqlSessionFactory.openSession(); animalMapper am=session.getMapper(animalMapper.class); return am.selectAllData(); } }
结果:
以上是关于Maven-SSM框架整合的主要内容,如果未能解决你的问题,请参考以下文章
java:maven(maven-ssm(聚合,分包开发))
全栈编程系列SpringBoot整合Shiro(含KickoutSessionControlFilter并发在线人数控制以及不生效问题配置启动异常No SecurityManager...)(代码片段