spring+spring mvc +mybatis项目搭建
程序员文章站
2022-05-26 11:01:40
...
写给java初学者的SSM框架搭建指南
一.项目结构
二.maven–app项目创建
1.新建maven项目(安装配置maven成功,并在eclipse中配置成功之后)
2.自己选定一个工作空间,next
3.选择maven项目类型为webapp
4.填写项目名称,以及包名
5.项目创建完成之后,查看资源包,必须有以下四个资源包,缺少的自行创建一下
需要手动创建page,common文件夹,位置如下图所示,原因是rest-servlet.xml中设置了位置
三.项目设置
照着下面几张图设置即可
没有设置tomcat的同学设置一下tomcat,版本为7.0
四.配置文件(已经经过测试)
如工程目录上所示的几个配置文件如下配置:
1.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<!-- 声明初始化参数 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>WeddingPhoto.root</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- spring字符集过滤器 关联实现接口 -->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<!-- 给servlet指定初始参数 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:rest-servlet.xml</param-value>
</init-param>
</servlet>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2.applicationContext.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:context="http://www.springframework.org/schema/context"
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">
<context:annotation-config />
<!-- 配置component所在的包,自动加载需要管理的Bean -->
<context:component-scan base-package="com.storm.BeyEducate"></context:component-scan>
<import resource="spring-mybatis.xml" />
</beans>
3.jdbc.properties
#mysql
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url:jdbc:mysql://localhost:3306/beyond_educate
jdbc.username=root
jdbc.password=root
jdbc.maxActive=10
jdbc.maxIdle=10
jdbc.maxWait=5000
jdbc.initialSize=5
jdbc.validationQuery=SELECT 1
jdbc.testWhileIdle=true
jdbc.testOnBorrow=true
jdbc.timeBetweenEvictionRunsMillis=3600000
jdbc.numTestsPerEvictionRun=50
jdbc.minEvictableIdleTimeMillis=120000
jdbc.removeAbandoned=true
jdbc.removeAbandonedTimeout=6000000
4.log4j.properties
log4j.rootLogger=DEBUG,Console,FILE
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
#File Appender
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.File=C:/soft/logs/BeyEducate.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=[%d{HH:mm:ss,SSS}] [%l] [%t] [%-5p] : %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=INFO
log4j.logger.java.sql.Statement=INFO
log4j.logger.java.sql.PreparedStatement=INFO
5.rest-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: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.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.storm.BeyEducate" />
<!-- 资源文件夹,需要手动创建 -->
<mvc:resources location="/common/" mapping="/common/**" />
<mvc:resources location="/pages/" mapping="/pages/**" />
<!--默认的mvc注解映射的支持 -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<ref bean="stringHttpMessageConverter" />
<ref bean="fastJsonHttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="stringHttpMessageConverter"
class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" index="0"></constructor-arg>
<!-- 避免出现乱码 -->
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
<bean id="fastJsonHttpMessageConverter"
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value><!-- 避免IE出现下载JSON文件的情况 -->
</list>
</property>
<property name="features">
<array value-type="com.alibaba.fastjson.serializer.SerializerFeature">
<value>WriteMapNullValue</value>
<value>QuoteFieldNames</value>
<value>DisableCircularReferenceDetect</value>
</array>
</property>
</bean>
<!-- Default ViewResolver对模型视图名称的解析即在模型视图名称添加前后缀 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/pages/" />
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
6.spring-mybatis.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="configProperties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="initialSize" value="${jdbc.initialSize}"></property>
<property name="maxActive" value="${jdbc.maxActive}"/>
<property name="maxIdle" value="${jdbc.maxIdle}"/>
<property name="maxWait" value="${jdbc.maxWait}"/>
<property name="validationQuery" value="${jdbc.validationQuery}"></property>
<property name="testWhileIdle" value="${jdbc.testWhileIdle}"></property>
<property name="testOnBorrow" value="${jdbc.testOnBorrow}"></property>
<property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"></property>
<property name="numTestsPerEvictionRun" value="${jdbc.numTestsPerEvictionRun}"></property>
<property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"></property>
<property name="removeAbandoned" value="${jdbc.removeAbandoned}"/>
<property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}"/>
</bean>
<!-- myBatis文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath*:com/storm/BeyEducate/model/mybatis/*.xml" />
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>dialect=mysql</value>
</property>
</bean>
</array>
</property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.storm.BeyEducate.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务处理面(事务通知) -->
<tx:advice id="appAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 配置事务属性 -->
<!-- 默认值: isolation="DEFAULT" timeout="-1" propagation="REQUIRED" read-only="false" -->
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="batch*" propagation="REQUIRED" />
<tx:method name="read*" propagation="REQUIRED" read-only="true" />
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="count*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 配置AOP事务 -->
<aop:config>
<!-- 配置事务切点 -->
<aop:pointcut expression="execution(* com.storm.BeyEducate.service.*Service.*(..))"
id="appPoint" />
<!-- 结合事务切点与切面 -->
<aop:advisor advice-ref="appAdvice" pointcut-ref="appPoint" />
</aop:config>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="namedTemplate"
class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg index="0" ref="dataSource"></constructor-arg>
</bean>
</beans>
五.maven项目启动操作
(1)更新maven项目 maven–>update maven project
(2)run as –>clean
(3)run as install
(4)maven build
操作完成,则ssm
至此,SSM项目搭建完成
推荐阅读
-
通过Spring Boot + Mybatis + Redis快速搭建现代化Web项目
-
Java框架搭建之Maven、Mybatis、Spring MVC整合搭建(图文)
-
MVC项目结构搭建及单个类的实现学习笔记1
-
Spring+Spring MVC+Mybatis 框架整合开发(半注解半配置文件)
-
MVC项目结构搭建及单个类的实现学习笔记1
-
在eclipse中使用spring,springmvc,mybatis搭建web项目
-
asp.net mvc 简单项目框架的搭建过程(一)对Bll层和Dal层进行充分解耦
-
如何利用IDEA搭建SpringBoot项目整合mybatis实现简单的登录功能
-
搭建springboot+mybatis+freemarker项目
-
从零开始搭建spring mvc + mybatis + memcached+ dubbo\zookper的maven项目