版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37776094/article/details/84305413
1.Controller
import com.scala.domain.MetaTable
import com.scala.service.MetaTableService
import com.scala.utils.ResultVOUtil
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation._
/**
* Created by ckl on 2018/11/20.
* Scala Controller
* 基于RestFul 链接记得加上"/"
* eg: http://localhost:8888/scala-hello/meta/table/
*/
@RestController
@RequestMapping(Array("/meta/table")) //与java不同之处 都得放到Array里面
class MetaTableController @Autowired()(metaTableService: MetaTableService){
@RequestMapping(value = Array("/"), method = Array(RequestMethod.GET)) //参数放到Array里面
@ResponseBody
def find() = {
ResultVOUtil.success(metaTableService.findall())
}
@RequestMapping(value = Array("/"), method = Array(RequestMethod.POST)) //参数放到Array里面
@ResponseBody
def save(@ModelAttribute metaTable:MetaTable) = {
metaTableService.save(metaTable)
ResultVOUtil.success() //Scala 调用 java
}
}
2.Service
import com.scala.domain.MetaTable
import com.scala.repository.MetaTableRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
/**
* Created by ckl on 2018/11/20.
*/
@Service
class MetaTableService @Autowired()(metaTableRepository: MetaTableRepository){
def findall() = {
metaTableRepository.findAll()
}
@Transactional
def save(metaTable: MetaTable) = {
this.metaTableRepository.save(metaTable)
}
}
3.orm
import com.scala.domain.MetaTable
import org.springframework.data.repository.CrudRepository
/**
* Created by ckl on 2018/11/20.
* Scala 接口 trait
*/
trait MetaTableRepository extends CrudRepository[MetaTable, Integer]{
}
4.model
import scala.beans.BeanProperty
/**
* Created by ckl on 2018/11/20.
*/
@Entity
@Table
class MetaTable {
@Id
@GeneratedValue
@BeanProperty
var id:Integer = _
@BeanProperty
var name:String = _
@BeanProperty
var tableType:String = _
@BeanProperty
var dbId:Integer = _
}
完整代码下载地址:https://download.csdn.net/download/m0_37776094/10796561