网上这样的例子真的是不计其数了,好多好多,这篇文章也是参考了网上的一个例子,这篇文章的目的,就是为了给那些不知如何下手兄弟们多一篇参考的文章。
博客参考地址:https://blog.csdn.net/u011422624/article/details/72778773
废话不多说了,开始我们的搭建之旅。 (看图说话)
项目目录展示:
首先我们创建project new Project ——> next
有个地方需要注意,选择gradle后,什么也不要勾选,(默认会选择java) 然后next
next ——> finish
ok项目创建完成。
接下来开始创建我们自己需要开发的模块,
点击 项目名右键——>new model ——> gradle (勾选java) ——>效果:
第一个模块创建完成。
其他模块也是这样创建, web模块需要多说一下,在创建web模块时,——> gradle(勾选java,web)。
最终的结果:
到此,细心的朋友应该知道我们所需的模块都创建出来了。
接下来了解下这几个模块是如何依赖的,在上面的图片中有个 settings.gradle的文件,我们查看下这个文件的内容:
是不是非常的简单,这里面的内容是自动添加进来的,所以完全不需要考虑是如何依赖的啦,因为gradle帮我们做好了。
下面是重点了:搭建 SSM框架。
首先我们把所有依赖的架包添加到项目中: 找到项目中最顶层的 build.gradle 里面的代码内容:
group 'com.xk'
version '1.0-SNAPSHOT'
subprojects {
//添加插件
apply plugin: 'java'
//编码环境jdk版本
sourceCompatibility = 1.8
//编译时jdk版本
targetCompatibility = 1.8
repositories {
maven{
url "http://maven.aliyun.com/nexus/content/repositories/central/"
}
//maven中央仓库
mavenCentral()
}
//配置外部属性
ext {
spring_version = "4.3.6.RELEASE"
}
//依赖的配置
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
//servlet依赖
compile "javax.servlet:servlet-api:3.0-alpha-1"
compile "javax:javaee-api:7.0"
//jstl标签库
compile "taglibs:standard:1.1.2"
compile "javax.servlet:jstl:1.2"
//公共资源包
compile "commons-logging:commons-logging:1.2"
compile "commons-lang:commons-lang:2.6"
compile "org.apache.commons:commons-collections4:4.0"
compile "commons-beanutils:commons-beanutils:1.8.3"
compile "commons-dbcp:commons-dbcp:1.4"
compile "commons-pool:commons-pool:1.6"
//文件上传
compile "commons-fileupload:commons-fileupload:1.3.1"
compile "commons-io:commons-io:2.4"
//AspectJ(切点表达式)
compile "org.aspectj:aspectjrt:1.7.4"
compile "org.aspectj:aspectjweaver:1.7.4"
//springmvc + Spring Configuration
compile "org.springframework:spring-web:$spring_version"
compile "org.springframework:spring-webmvc:$spring_version"
compile "org.springframework:spring-aop:$spring_version"
compile "org.springframework:spring-aspects:$spring_version"
compile "org.springframework:spring-beans:$spring_version"
compile "org.springframework:spring-context:$spring_version"
compile "org.springframework:spring-context-support:$spring_version"
compile "org.springframework:spring-core:$spring_version"
compile "org.springframework:spring-expression:$spring_version"
compile "org.springframework:spring-jdbc:$spring_version"
compile "org.springframework:spring-messaging:$spring_version"
compile "org.springframework:spring-orm:$spring_version"
compile "org.springframework:spring-tx:$spring_version"
compile "org.springframework:spring-test:$spring_version"
//mybatis及依赖包
compile group: 'org.mybatis', name: 'mybatis', version: '3.4.2'
//spring 整合 mybatis
compile group: 'org.mybatis', name: 'mybatis-spring', version: '1.3.0'
//mysql数据库连接驱动
compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.5'
//dbcp 连接池配置数据库
compile group: 'commons-dbcp', name: 'commons-dbcp', version: '1.4'
//日志
compile group: 'log4j', name: 'log4j', version: '1.2.17'
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.24'
compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.24'
//gson
compile group: 'com.google.code.gson', name: 'gson', version: '2.7'
//fastjson
compile group: 'com.alibaba', name: 'fastjson', version: '1.2.33'
}
}
然后我们查看下web目录,在webapp 下创建两个目录 resources 、WEB-INF、 一个jsp页面 index.jsp
web端的最终的目录我们先来看一下:
上面的目录就是这个项目的 核心的东西了,在下面把里面的代码都贴出来,供大家参考。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- 使用监听器加载applicationContext文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 解决post乱码问题 -->
<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>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--默认访问页面-->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
spring-mvc.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">
<!--扫描包,自动注入bean-->
<context:component-scan base-package="com.oa"/>
<!--使用注解开发spring mvc-->
<mvc:annotation-driven/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--上传下载-->
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
<!--登录拦截器 这里注意一下,如果使用这段代码请配置好登录拦截器,不要只是新建一个LogInterceptor类,要把内容写全,要么注释掉下段代码-->
<!--<mvc:interceptors>-->
<!--<mvc:interceptor>-->
<!--<mvc:mapping path="/**"/>-->
<!--<mvc:exclude-mapping path="/bootstrap/**/*"/>-->
<!--<mvc:exclude-mapping path="/css/**/*"/>-->
<!--<mvc:exclude-mapping path="/js/**/*"/>-->
<!--<mvc:exclude-mapping path="/img/**/*"/>-->
<!--<bean class="com.lmandy.interceptor.LogInterceptor"/>-->
<!--</mvc:interceptor>-->
<!--</mvc:interceptors>-->
<!--过滤掉静态资源文件-->
<mvc:resources mapping="/bootstrap/**/*" location="/resources/bootstrap/"/>
<mvc:resources mapping="/css/**/*" location="/resources/css/"/>
<mvc:resources mapping="/js/**/*" location="/resources/js/"/>
<mvc:resources mapping="/img/**/*" location="/resources/img/"/>
</beans>
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"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.oa"/>
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
</bean>
<!--sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:com/oa/dao/**/*Mapper.xml"/>
</bean>
<!--mapper 扫描器,配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.oa.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!--声明式事物管理器-->
<!-- 配置spring的PlatformTransactionManager,名字为默认值 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置基于注解的声明式事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
#?useUnicode=true & characterEncoding=utf8 &autoReconnect=true
jdbc.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC & useUnicode=true & characterEncoding=utf8 &autoReconnect=true
jdbc.username=root
jdbc.password=root
log4j.properties
log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
以上我们的配置文件内容基本完成了,如果还其他需求大家可以自定义配置了。
下面把整合的代码写一下大家参考下:
controller
service
dao