IntelliJ-IDEA教程
gradle官网
spring下载
使用IDEA和gradle搭建Spring MVC和MyBatis开发环境
Spring MVC 环境搭建
intellij+gradle spring环境搭建
第一次可以参考上面的环境搭建文章,写的很好。
这里新建一个project:
1.png 2.png
GroupID是项目组织唯一的标识符,实际对应[Java]的包结构,就是main目录里java的目录结构。一般GroupID就是填com.company.appname,这样子。
ArtifactID就是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称。例如(zhifubao,weixin当然用英文更好)
3.png选择gradle的来源,可以用自己下载的gradle,也可以使用IDEA内建的gradle版本:
4.png选择项目存放的目录:
5.png 6.png添加jar包进lib中,这里我比较懒,直接都加进去,自己可以根据需要添加,如图
7.pnggradle配置
group 'com.lq.play'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'war'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile group: 'junit', name: 'junit', version: '4.12'
def springVersion = "4.3.9.RELEASE"
def mybatisVersion = "3.2.6"
def log4jVersion = "1.2.17"
// Spring依赖
compile("org.springframework:spring-aop:$springVersion",
"org.springframework:spring-orm:$springVersion",
"org.springframework:spring-jdbc:$springVersion",
"org.springframework:spring-jms:$springVersion",
"org.springframework:spring-messaging:$springVersion",
"org.springframework:spring-instrument-tomcat:$springVersion",
"org.springframework:spring-instrument:$springVersion",
"org.springframework:spring-framework-bom:$springVersion",
"org.springframework:spring-expression:$springVersion",
"org.springframework:spring-core:$springVersion",
"org.springframework:spring-context-support:$springVersion",
"org.springframework:spring-context:$springVersion",
"org.springframework:spring-beans:$springVersion",
"org.springframework:spring-aspects:$springVersion",
"org.springframework:spring-test:$springVersion",
"org.springframework:spring-tx:$springVersion",
"org.springframework:spring-web:$springVersion",
"org.springframework:spring-webmvc:$springVersion",
"org.springframework:spring-webmvc-portlet:$springVersion")
// https://mvnrepository.com/artifact/commons-logging/commons-logging
compile group: 'commons-logging', name: 'commons-logging', version: '1.2'
//mybaits
compile group: 'org.mybatis', name: 'mybatis', version: mybatisVersion
//mybaits-spring
compile group: 'org.mybatis', name: 'mybatis-spring', version: '1.2.2'
//mysql
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.6'
compile group: 'commons-dbcp', name: 'commons-dbcp', version: '1.4'
//日志
compile("log4j:log4j:$log4jVersion",
"org.slf4j:slf4j-api:1.7.21",
"org.slf4j:slf4j-log4j12:1.7.21")
}
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"
>
<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>
<!-- Log4J 配置 Start -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>6000</param-value>
</context-param>
<!-- 防止Spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<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:config/spring/spring-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>
<welcome-file-list>
<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: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-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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自动扫描控制器 -->
<context:component-scan base-package="com.lq.play.controller" use-default-filters="false">
<!--<context:component-scan base-package="">-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 视图解析器 -->
<bean id="internalResourceViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 控制器映射器和控制器适配器 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!--<mvc:default-servlet-handler/>-->
<!-- 静态资源映射器 -->
<mvc:resources mapping="/**" location="/WEB-INF/js/" />
<mvc:resources mapping="/**" location="/WEB-INF/css/" />
</beans>
IndexController.java
@Controller
public class IndexController {
@RequestMapping("/")
public String index(){
return "/index";
}
}
配置tomcat:
点击右边箭头->Edit configuration
tomcat1.png tomcat2.png
点击tomcat server的local选项,点击右上角的configure..., 在对话框中选中下载的Tomcat根目录:
tomcat3.png tomcat4.png然后将配置好的tomcat添加到运行库当中:
tomcat5.png点击右上角的绿色向右箭头,启动
tomcat6.png
运行成功,spring的世界可以开始玩耍了
image.png
网友评论