diff --git a/README.md b/README.md index b3cd1f0..2de9aea 100644 --- a/README.md +++ b/README.md @@ -44,4 +44,506 @@ https://github.com/universsky/ylazy --- + + + + + + +## Springboot plus Scala 开发简明教程 + +### 一 使用maven作为项目构建工具 + +直接参照pom.xml + +```xml + + + 4.0.0 + + com.light.sword + ylazy + 1.0-SNAPSHOT + + + 2.11.8 + 1.8 + 1.8 + UTF-8 + 2.11 + UTF-8 + 1.8 + 2.0.3.RELEASE + 1.0.0-SNAPSHOT + 1.3.5.RELEASE + + + + + org.springframework.boot + spring-boot-starter-parent + 1.3.5.RELEASE + + + + + + org.scala-lang + scala-library + ${scala.version} + + + + com.typesafe.scala-logging + scala-logging-slf4j_2.11 + 2.1.2 + + + + + junit + junit + test + + + org.specs2 + specs2-core_${scala.compat.version} + 2.4.16 + test + + + org.scalatest + scalatest_${scala.compat.version} + 2.2.4 + test + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + org.springframework.boot + spring-boot-starter-velocity + + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework + spring-test + + + + com.alibaba + fastjson + 1.2.7 + + + mysql + mysql-connector-java + + + javax.mail + mail + 1.4.7 + + + com.squareup.okhttp3 + okhttp + 3.2.0 + + + + + + + + + + + + + + src/main/scala + src/test/scala + + + org.scala-tools + maven-scala-plugin + + + + compile + testCompile + + + + + incremental + ${scala.version} + + + app + com.light.sword.ylazy.LightSwordApplication + + -deprecation + + + -Xms256m + -Xmx2048m + + + + + + + + org.springframework + springloaded + 1.2.6.RELEASE + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + package + + copy-dependencies + + + + + system + + + + org.apache.maven.plugins + maven-eclipse-plugin + + true + + ch.epfl.lamp.sdt.core.scalabuilder + + + ch.epfl.lamp.sdt.core.scalanature + + + org.eclipse.jdt.launching.JRE_CONTAINER + ch.epfl.lamp.sdt.launching.SCALA_CONTAINER + + + + + + + + + org.scala-tools + maven-scala-plugin + + ${scala.version} + + + + + + + + scala-tools.org + Scala-Tools Maven2 Repository + http://scala-tools.org/repo-releases + + + + + + scala-tools.org + Scala-Tools Maven2 Repository + http://scala-tools.org/repo-releases + + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + io.spring.platform + platform-bom + ${platform-bom.version} + pom + import + + + + + + +``` + + +### 二 Scala plus springboot配置 +### 三 SpringBoot plus JPA的持久层框架 + +####领域类写法 + +```scala +package com.light.sword.ylazy.entity + +import java.util.Date +import javax.persistence.{Entity, GeneratedValue, GenerationType, Id} + +import scala.beans.BeanProperty +import scala.language.implicitConversions + +@Entity +class LazyTask { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @BeanProperty + var id: Integer = _ + + @BeanProperty + var url: String = _ + @BeanProperty + var name: String = _ + + //用例状态: -1未执行 0失败 1成功 + @BeanProperty + var state: Integer = _ + + @BeanProperty + var owner: String = _ + + @BeanProperty + var resultJson: String = _ + + @BeanProperty + var executeTimes: Integer = _ + + @BeanProperty + var executor: Integer = _ + + @BeanProperty + var gmtCreate: Date = _ + + @BeanProperty + var gmtModify: Date = _ + + + +} +``` + +####Dao层代码写法 + +```scala +package com.light.sword.ylazy.dao + +import java.util.List + +import com.light.sword.ylazy.entity.LazyTask +import org.springframework.data.jpa.repository.Query +import org.springframework.data.repository.CrudRepository + +// JavaConversions +import scala.language.implicitConversions + +trait LazyTaskDao extends CrudRepository[LazyTask, Integer] { + def findAll(): List[LazyTask] + + def save(t: LazyTask): LazyTask + + def findOne(id: Integer): LazyTask + + @Query(value = "SELECT * FROM lazy_task where url like '%?1%'", nativeQuery = true) + def listByUrl(url: String): List[LazyTask] + + @Query(value = "SELECT * FROM lazy_task where name like '%?1%'", nativeQuery = true) + def listByName(name: String): List[LazyTask] + + +} + +``` + + +### 四 使用Scala调用Spring MVC框架API + +####Controller写法实例 + +```scala +package com.light.sword.ylazy.controller + +import java.util.Date + +import com.light.sword.ylazy.config.DomainConfig +import com.light.sword.ylazy.dao.LazyTaskDao +import com.light.sword.ylazy.engine.PhantomjsExecutor +import com.light.sword.ylazy.entity.LazyTask +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.ui.Model +import org.springframework.web.bind.annotation._ +import org.springframework.web.servlet.ModelAndView + +@RestController +class LazyTaskController @Autowired()(val lazyTaskDao: LazyTaskDao, + val phantomjsExecutor: PhantomjsExecutor, + val domainConfig: DomainConfig) { + + @RequestMapping(value = { + Array("/newTask.do") + }) + def newTask_do() = { + new ModelAndView("ylazy/newTask") + } + + @RequestMapping(value = { + Array("/ylazy/newTask") + }, method = Array(RequestMethod.POST)) + @ResponseBody + def newTask(@ModelAttribute lazyTask: LazyTask) = { + lazyTask.gmtCreate = new Date + lazyTask.gmtModify = new Date + lazyTask.executeTimes = 0 + lazyTask.state = -1 + lazyTaskDao.save(lazyTask) + } + + @RequestMapping(value = { + Array("/list.do") + }) + def list_do(model: Model) = { + model.addAttribute("lazyTaskList", lazyTaskDao.findAll()) + model.addAttribute("domainName", domainConfig.getDomainName) + model.addAttribute("port", domainConfig.getPort) + + new ModelAndView("ylazy/list") + } + + + /** + * 获取一条任务记录 + * + * @param id + * @return + */ + @RequestMapping(value = { + Array("/lazytask") + }, method = Array(RequestMethod.GET)) + @ResponseBody + def findOne(@RequestParam(value = "id") id: Integer) = lazyTaskDao.findOne(id) + + /** + * 获取一条任务记录的返回json + * + * @param id + * @return + */ + @RequestMapping(value = { + Array("/result/{id}") + }, method = Array(RequestMethod.GET)) + @ResponseBody + def findResultJson(@PathVariable(value = "id") id: Integer) = lazyTaskDao.findOne(id).resultJson + + + /** + * 执行任务 + * @param id + * @return + */ + @RequestMapping(value = { + Array("/ylazy/runTask") + }, method = Array(RequestMethod.GET)) + @ResponseBody + def runTask(@RequestParam(value = "id") id: Integer) = { + phantomjsExecutor.ylazyById(id) + } + + + @RequestMapping(value = { + Array("/ylazy") + }, method = Array(RequestMethod.GET)) + @ResponseBody + def ylazy(@RequestParam(value = "url") url: String) = phantomjsExecutor.ylazy(url) + + +} + +``` + + + +### 五 Scala with Java混合编程 +由于Scala代码最终也是编译成为JVM上的ByteCode,所以与所有运行在JVM上的语言无缝集成。 + + +参看LazyTaskController.scala对PhantomjsExecutor.java类的调用。 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +