Groovy简单语法
打印输出
println("hello");
println("world") //省略;
println "hello" //省略;和()
定义变量
-
普通类型
def a = 10 //定义变量a println a //打印输出a
-
集合类型
def list = ["a","b","c"] list << "d" //添加元素 println list[3] //打印下标为3的元素
def map = ["k1":"v1","k2":"v2"] map.k4 = "v4"; println map.k4
-
闭包
def a = { //定义闭包(闭包其实就是代码块) println "hello" } def b(Closure closure){//定义方法,参数是闭包Closure closure(); } b(a) //调用方法,传递参数
def a = { v -> println "hello ${v}" //接收参数 } def b(Closure closure){ closure("world") //传递参数 } b(a) //调用方法传递参数
配置Gradle本地仓库
电脑–》属性–》高级系统设置–》环境变量–》新建
第一个Gradle简单项目
-
build.gradle
plugins { id 'java' } group 'mycode' version '1.0-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenLocal() //本地仓库 mavenCentral() } dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' compile group: 'org.springframework', name: 'spring-context', version: '5.2.5.RELEASE' }
-
Dao
public interface CustomerDao { public void findAll(); }
public class CustomerDaoImpl implements CustomerDao { @Override public void findAll() { System.out.println("成功"); } }
-
spring.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean name="customerDao" class="CustomerDaoImpl"/> </beans>
-
测试是否可运行
public class TestBean { @Test public void te(){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); CustomerDao customerDao = (CustomerDao) context.getBean("customerDao"); customerDao.findAll(); } }
-
输出乱码,解决,在build.gradle中添加
tasks.withType(JavaCompile){ options.encoding = "utf-8" //解决输出中文乱码 }
第一个Gradle简单Web项目
-
新建一个Gradle项目,在src/main下创建一个包,名为webapp,下文件夹下新建WEB-INF包,在WEB-INF下新建web.xml文件
-
build.gradle
plugins { id 'java' id 'war' //打包为war,webapp才能被表示为一个web资源包 } group 'mycode' version '1.0-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenLocal() mavenCentral() } dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' compile group: 'org.springframework', name: 'spring-context', version: '5.2.5.RELEASE' compile group: 'org.springframework', name: 'spring-web', version: '5.2.5.RELEASE' compile group: 'org.springframework', name: 'spring-webmvc', version: '5.2.5.RELEASE' providedCompile group: 'javax.servlet', name: 'jsp-api', version: '2.0' providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0' }
-
web.xml
<?xml version='1.0' encoding='UTF-8'?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet><!--SpringMVC前端控制器--> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name><!--SpringMVC配置文件的位置--> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
-
springmvc.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: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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.controller"/> <mvc:annotation-driven/> <mvc:default-servlet-handler/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
-
controller层
@Controller public class MyController { @RequestMapping("/toList") public String toList(){ return "list"; } }
-
index.jsp
<jsp:forward page="/toList"/>
-
添加本地Tomcat,运行
第一个Gradle拆分项目
-
父项目的配置
allprojects {//父项目的配置,所有子项目都可用 apply plugin: 'java' //不能写plugins{id 'java'},这么写会报错 group 'mycode' version '1.0-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenLocal() mavenCentral() } dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' compile group: 'org.springframework', name: 'spring-context', version: '5.2.5.RELEASE' compile group: 'org.springframework', name: 'spring-web', version: '5.2.5.RELEASE' compile group: 'org.springframework', name: 'spring-webmvc', version: '5.2.5.RELEASE' compile group: 'javax.servlet', name: 'jsp-api', version: '2.0' compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0' } }
-
子项目的配置
dependencies{//service层使用dao层 compile project(":gradle-dao") }
apply plugin: 'war'//web层,打包方式为war dependencies{//web层使用service层 compile project(":gradle-service") }