├── README.md ├── src ├── main │ ├── resources │ │ ├── static │ │ │ ├── subpage │ │ │ │ └── newpage.html │ │ │ └── index.html │ │ └── application.properties │ └── java_scala │ │ └── org │ │ └── tashaxing │ │ └── SpringbootScalaDemo │ │ ├── SpringbootScalaDemoApplication.scala │ │ ├── repository │ │ ├── ScalaModelQuery.scala │ │ ├── ScalaModelRepository.scala │ │ └── BaseQuery.scala │ │ ├── controller │ │ ├── PageController.scala │ │ ├── JavaTestController.java │ │ └── ScalaTestController.scala │ │ ├── config │ │ └── OriginConfig.scala │ │ └── model │ │ ├── ScalaModel.scala │ │ └── SlickModel.scala └── test │ └── java │ └── org │ └── tashaxing │ └── SpringbootScalaDemo │ └── SpringbootScalaDemoApplicationTests.java ├── frontend ├── src │ ├── apitest.html │ └── apitest.js ├── .gitattributes ├── README.md ├── package.json └── webpack.config.js ├── .gitattributes ├── .gitignore ├── pom.xml ├── mvnw.cmd └── mvnw /README.md: -------------------------------------------------------------------------------- 1 | # SpringbootScalaDemo 2 | springboot demo combined with scala and java 3 | 4 | # blog address 5 | http://blog.csdn.net/u012234115/article/details/78650622 6 | -------------------------------------------------------------------------------- /src/main/resources/static/subpage/newpage.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | newpage 6 | 7 | 8 |

the subpage

9 | 10 | -------------------------------------------------------------------------------- /src/main/resources/static/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | scala test index page 6 | 7 | 8 | demo rootpage for scala springboot 9 | 10 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | #datasource 3 | spring.jpa.hibernate.ddl-auto=update 4 | spring.datasource.url=jdbc:mysql://localhost:3306/db_example?useUnicode=true&characterEncoding=UTF-8&useSSL=false 5 | spring.datasource.username=springuser 6 | spring.datasource.password=ThePassword 7 | 8 | # Server port 9 | server.port=7700 -------------------------------------------------------------------------------- /frontend/src/apitest.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | apitest 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /frontend/.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /src/test/java/org/tashaxing/SpringbootScalaDemo/SpringbootScalaDemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package org.tashaxing.SpringbootScalaDemo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringbootScalaDemoApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java_scala/org/tashaxing/SpringbootScalaDemo/SpringbootScalaDemoApplication.scala: -------------------------------------------------------------------------------- 1 | package org.tashaxing.SpringbootScalaDemo 2 | 3 | import org.springframework.boot.SpringApplication 4 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration 5 | import org.springframework.boot.autoconfigure.{EnableAutoConfiguration, SpringBootApplication} 6 | 7 | @SpringBootApplication 8 | class BootConfig 9 | 10 | object SpringbootScalaDemoApplication extends App { 11 | 12 | SpringApplication.run(classOf[BootConfig]) 13 | } -------------------------------------------------------------------------------- /frontend/README.md: -------------------------------------------------------------------------------- 1 | # react_multipage_demo 2 | A react multipage demo with webpack 3 | 4 | # code structure 5 | ![](https://github.com/tashaxing/react_multipage_demo/raw/master/src/asset/struct.PNG)
6 | 7 | # blog address 8 | http://blog.csdn.net/u012234115/article/details/78640077 9 | 10 | # screenshots 11 | ![](https://github.com/tashaxing/react_multipage_demo/raw/master/src/asset/home.png)
12 | ![](https://github.com/tashaxing/react_multipage_demo/raw/master/src/asset/page1.png)
13 | ![](https://github.com/tashaxing/react_multipage_demo/raw/master/src/asset/page2.png)
14 | -------------------------------------------------------------------------------- /src/main/java_scala/org/tashaxing/SpringbootScalaDemo/repository/ScalaModelQuery.scala: -------------------------------------------------------------------------------- 1 | package org.tashaxing.SpringbootScalaDemo.repository 2 | 3 | import org.springframework.beans.factory.annotation.Autowired 4 | import org.springframework.stereotype.Service 5 | import org.tashaxing.SpringbootScalaDemo.model.ScalaModel 6 | import java.util.List 7 | 8 | @Service 9 | class ScalaModelQuery extends BaseQuery[ScalaModel] 10 | { 11 | @Autowired val scalaModelRepository: ScalaModelRepository = null 12 | 13 | def findByName(name: String): List[ScalaModel] = scalaModelRepository.findByName(name) 14 | 15 | // def findNameOfAge(): List[Object] = scalaModelRepository.findNameOfAge() 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java_scala/org/tashaxing/SpringbootScalaDemo/repository/ScalaModelRepository.scala: -------------------------------------------------------------------------------- 1 | package org.tashaxing.SpringbootScalaDemo.repository 2 | 3 | import org.springframework.data.jpa.repository.{JpaRepository, Query} 4 | import org.tashaxing.SpringbootScalaDemo.model.ScalaModel 5 | 6 | // remember to use java List and Long here 7 | import java.util.List 8 | import java.lang.Long 9 | 10 | trait ScalaModelRepository extends JpaRepository[ScalaModel, Long] 11 | { 12 | // normal find function from db 13 | def findByName(name: String): List[ScalaModel] 14 | 15 | // self defined function from db by sql query 16 | // @Query(value = "select name from scala_model where age < 22") 17 | // def findNameOfAge(): List[Object] 18 | } -------------------------------------------------------------------------------- /src/main/java_scala/org/tashaxing/SpringbootScalaDemo/controller/PageController.scala: -------------------------------------------------------------------------------- 1 | package org.tashaxing.SpringbootScalaDemo.controller 2 | 3 | import org.springframework.stereotype.Controller 4 | import org.springframework.web.bind.annotation.RequestMapping 5 | 6 | @Controller 7 | class PageController 8 | { 9 | // root page 10 | @RequestMapping(Array("/")) 11 | def index(): String = "redirect:/index.html" 12 | 13 | // an other page 14 | // use forward instead of redirect will not show xxx.html in browser address bar 15 | @RequestMapping(Array("/subpage")) 16 | def sub(): String = "forward:/subpage/newpage.html" 17 | 18 | // no need to redirect apitest 19 | // @RequestMapping(Array("/apitest")) 20 | // def apitest(): String = "redirect:/apitest.html" 21 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear on external disk 35 | .Spotlight-V100 36 | .Trashes 37 | 38 | # Directories potentially created on remote AFP share 39 | .AppleDB 40 | .AppleDesktop 41 | Network Trash Folder 42 | Temporary Items 43 | .apdisk 44 | /.idea 45 | target 46 | node_modules 47 | -------------------------------------------------------------------------------- /frontend/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react_multipage_demo", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "antd": "^2.12.8", 7 | "axios": "^0.17.1", 8 | "react": "^15.6.1", 9 | "react-dom": "^15.6.1" 10 | }, 11 | "scripts": { 12 | "start": "webpack-dev-server", 13 | "build": "webpack" 14 | }, 15 | "devDependencies": { 16 | "babel-core": "^6.26.0", 17 | "babel-loader": "^7.1.2", 18 | "babel-preset-env": "^1.6.1", 19 | "babel-preset-es2015": "^6.24.1", 20 | "babel-preset-react": "^6.24.1", 21 | "copy-webpack-plugin": "^4.2.3", 22 | "css-loader": "^0.28.7", 23 | "less": "^2.7.3", 24 | "less-loader": "^4.0.5", 25 | "style-loader": "^0.19.0", 26 | "webpack": "^3.8.1", 27 | "webpack-dev-server": "^2.9.4" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java_scala/org/tashaxing/SpringbootScalaDemo/config/OriginConfig.scala: -------------------------------------------------------------------------------- 1 | package org.tashaxing.SpringbootScalaDemo.config 2 | 3 | import org.springframework.context.annotation.Configuration 4 | import org.springframework.web.servlet.config.annotation.{CorsRegistry, WebMvcConfigurerAdapter} 5 | 6 | @Configuration 7 | class OriginConfig extends WebMvcConfigurerAdapter 8 | { 9 | override def addCorsMappings(registry: CorsRegistry): Unit = 10 | { 11 | // cors setting to allow origin access 12 | registry.addMapping("/**") // allow any path 13 | .allowedOrigins("http://192.168.1.97") // allow exact ip /** to map any ip 14 | .allowedMethods("GET", "POST") // allow methods 15 | .allowedHeaders("*") // allow any header 16 | } 17 | 18 | } -------------------------------------------------------------------------------- /src/main/java_scala/org/tashaxing/SpringbootScalaDemo/model/ScalaModel.scala: -------------------------------------------------------------------------------- 1 | package org.tashaxing.SpringbootScalaDemo.model 2 | 3 | import java.lang.Long 4 | import javax.persistence.{Entity, GeneratedValue, Id, Table} 5 | import javax.validation.constraints.NotNull 6 | 7 | import scala.beans.BeanProperty 8 | import org.hibernate.validator.constraints.{NotBlank, NotEmpty} 9 | 10 | import scala.annotation.meta.field 11 | 12 | //@Table(name = "scala_model") // define mysql table name 13 | //@Entity 14 | //case class ScalaModel( 15 | // @(Id @field) @(GeneratedValue @field) @BeanProperty var id: Long, 16 | // @BeanProperty @(NotEmpty @field) var name: String, // @field is a must 17 | // @BeanProperty @(NotEmpty @field) var age: Int 18 | //) 19 | 20 | @Table(name = "scala_model") // define mysql table name 21 | @Entity 22 | class ScalaModel 23 | { 24 | @Id 25 | @GeneratedValue 26 | @BeanProperty 27 | var id: Long = _ 28 | 29 | @BeanProperty 30 | @NotBlank // we can make sure it not empty 31 | var name: String = _ 32 | 33 | @BeanProperty 34 | @NotNull 35 | var age: Int = 18 // we can define default value here 36 | } 37 | 38 | 39 | -------------------------------------------------------------------------------- /src/main/java_scala/org/tashaxing/SpringbootScalaDemo/controller/JavaTestController.java: -------------------------------------------------------------------------------- 1 | package org.tashaxing.SpringbootScalaDemo.controller; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | import org.springframework.web.bind.annotation.GetMapping; 9 | import org.springframework.web.bind.annotation.RequestMapping; 10 | import org.springframework.web.bind.annotation.RequestParam; 11 | import org.springframework.web.bind.annotation.RestController; 12 | import org.tashaxing.SpringbootScalaDemo.model.ScalaModel; 13 | 14 | @RestController 15 | @RequestMapping("/javatest") 16 | public class JavaTestController { 17 | 18 | @RequestMapping 19 | public String root() { 20 | return "java test results"; 21 | } 22 | 23 | @GetMapping("/info") 24 | public Map getInfo(@RequestParam String name) { 25 | // java map can be returned without extra code 26 | Map map = new HashMap<>(); 27 | map.put("name", name); 28 | return map; 29 | } 30 | 31 | @RequestMapping("/list") 32 | public List> getList() { 33 | List> list = new ArrayList<>(); 34 | Map map = null; 35 | for (int i = 1; i <= 5; i++) { 36 | map = new HashMap<>(); 37 | map.put("name", "hello-" + i); 38 | list.add(map); 39 | } 40 | return list; 41 | } 42 | 43 | // test model serialize 44 | @GetMapping("/model") 45 | public ScalaModel getModel() { 46 | ScalaModel scalaModel = new ScalaModel(); 47 | scalaModel.setId(10L); 48 | scalaModel.setName("Ethan"); 49 | scalaModel.setAge(23); 50 | 51 | return scalaModel; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java_scala/org/tashaxing/SpringbootScalaDemo/model/SlickModel.scala: -------------------------------------------------------------------------------- 1 | package org.tashaxing.SpringbootScalaDemo.model 2 | 3 | import javax.persistence.{Entity, GeneratedValue, Id} 4 | import javax.validation.constraints.NotNull 5 | 6 | import org.hibernate.validator.constraints.{NotBlank, NotEmpty} 7 | 8 | import scala.annotation.meta.field 9 | import scala.beans.BeanProperty 10 | import slick.jdbc.MySQLProfile.api._ 11 | import slick.lifted.TableQuery 12 | 13 | object SlickDB 14 | { 15 | // @Entity 16 | // in springboot, we must add some annotation for case class for slick 17 | // @Entity 18 | // case class UserInfo( 19 | // @(Id @field) @(GeneratedValue @field) @BeanProperty var id: Long, 20 | // @BeanProperty @(NotBlank @field) var name: String, // @field is a must 21 | // @BeanProperty @(NotNull @field) var age: Int 22 | // ) 23 | case class UserInfo(id: Long, name: String, age: Int) 24 | 25 | // define an entity class with contructor used for spring serialize 26 | @Entity 27 | class UserInfoObject 28 | { 29 | @Id 30 | @GeneratedValue 31 | @BeanProperty 32 | var id: Long = _ 33 | 34 | @BeanProperty 35 | @NotBlank // we can make sure it not empty 36 | var name: String = _ 37 | 38 | @BeanProperty 39 | @NotNull 40 | var age: Int = 18 // we can define default value here 41 | } 42 | 43 | class SlickModelTable(tag: Tag) extends Table[UserInfo](tag, "scala_model") 44 | { 45 | def id = column[Long]("id", O.PrimaryKey) 46 | def name = column[String]("name") 47 | def age = column[Int]("age") 48 | def * = (id, name, age) <> (UserInfo.tupled, UserInfo.unapply) 49 | } 50 | def slick_table = TableQuery[SlickModelTable] 51 | } 52 | 53 | 54 | -------------------------------------------------------------------------------- /frontend/webpack.config.js: -------------------------------------------------------------------------------- 1 | var path = require('path'); 2 | var webpack = require('webpack') 3 | var CopyWebpackPlugin = require('copy-webpack-plugin'); 4 | 5 | module.exports = { 6 | entry: { 7 | apitest: __dirname + "/src/apitest.js" 8 | }, 9 | output: { 10 | path: path.resolve(__dirname, '../src/main/resources/static'), // build to springboot static page dir 11 | filename: 'build/[name].bundle.js', 12 | chunkFilename: 'build/[id].chunk.js' 13 | }, 14 | // devtool: 'eval-source-map', 15 | devServer: { 16 | inline: true, // hot load 17 | contentBase: './src', 18 | port: 3000, // dev server listen port 19 | }, 20 | module: { 21 | rules: [ 22 | { 23 | test: /\.(js|jsx)$/, 24 | exclude: /node_modules/, 25 | use: { 26 | loader: 'babel-loader', 27 | options: { 28 | presets: ['react', 'es2015'] 29 | } 30 | }, 31 | }, 32 | { 33 | test: /\.css$/, 34 | loader: 'style-loader!css-loader' 35 | }, 36 | { 37 | test: /\.less$/, 38 | loader: 'style-loader!css-loader!less-loader' 39 | }, 40 | { 41 | test: /\.(png|jpg|gif)$/, 42 | loader: 'url?limit=819200' 43 | } 44 | ] 45 | }, 46 | plugins: [ 47 | // new HtmlWebpackPlugin({ 48 | // title: "myapp for test" 49 | // }) 50 | new webpack.optimize.CommonsChunkPlugin({ 51 | name: 'vendor', 52 | filename: 'build/vendor.bundle.js' 53 | }), 54 | // new webpack.optimize.UglifyJsPlugin({ 55 | // compress: { 56 | // warnings: false 57 | // } 58 | // }), // there are promblem working with webpack v3 59 | new CopyWebpackPlugin([ 60 | { 61 | from: __dirname + '/src', ignore: '*.js' // copy all the files except js 62 | } 63 | ]) 64 | ] 65 | } -------------------------------------------------------------------------------- /src/main/java_scala/org/tashaxing/SpringbootScalaDemo/repository/BaseQuery.scala: -------------------------------------------------------------------------------- 1 | package org.tashaxing.SpringbootScalaDemo.repository 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository 4 | import org.springframework.beans.factory.annotation.Autowired 5 | import scala.reflect.ClassTag 6 | import java.lang.Long 7 | import org.springframework.data.domain.Page 8 | import java.util.List 9 | import org.springframework.stereotype.Service 10 | import javax.transaction.Transactional 11 | import java.lang.Boolean 12 | import org.springframework.data.domain.PageRequest 13 | 14 | // base query helper for all datatype and table 15 | @Service 16 | abstract class BaseQuery[T: ClassTag] { 17 | 18 | /** spring data jpa dao */ 19 | @Autowired val jpaRepository: JpaRepository[T, Long] = null 20 | 21 | /** 22 | * add record 23 | * 24 | * @param s 25 | * @return T 26 | */ 27 | def save[S <: T](s: S): T = jpaRepository.save(s) 28 | 29 | /** 30 | * delete record by id 31 | * 32 | * @param id 数据Id 33 | * @return Unit 34 | */ 35 | @Transactional 36 | def delete(id: Long): Unit = jpaRepository.delete(id) 37 | 38 | /** 39 | * delete by batch 40 | * 41 | * @param lists 42 | * @return Unit 43 | */ 44 | @Transactional 45 | def delete(lists: List[T]): Unit = jpaRepository.delete(lists); 46 | 47 | /** 48 | * update recorde by id 49 | * 50 | * @param s 51 | * @return T 52 | */ 53 | @Transactional 54 | def update[S <: T](s: S) = jpaRepository.save(s) 55 | 56 | /** 57 | * find recorde by id 58 | * 59 | * @param id 数据Id 60 | * @return T 61 | */ 62 | def find[S <: T](id: Long): T = jpaRepository.findOne(id) 63 | 64 | /** 65 | * query all 66 | * 67 | * @return List[T] 68 | */ 69 | def findAll[S <: T]: List[T] = jpaRepository.findAll 70 | 71 | /** 72 | * query by id batch 73 | * 74 | * @return List[T] 75 | */ 76 | def findAll[S <: T](ids: List[Long]): List[T] = jpaRepository.findAll(ids) 77 | 78 | /** 79 | * count 80 | * 81 | * @return Long 82 | */ 83 | def count: Long = jpaRepository.count 84 | 85 | /** 86 | * check recorde exits 87 | * 88 | * @param id 89 | * @return Boolean 90 | */ 91 | def exist(id: Long): Boolean = jpaRepository.exists(id) 92 | 93 | /** 94 | * query page 95 | * 96 | * @param page start page 97 | * @param pageSize page number 98 | * @return Page[T] 99 | */ 100 | def page[S <: T](page: Int, pageSize: Int): Page[T] = { 101 | var rpage = if (page < 1) 1 else page; 102 | var rpageSize = if (pageSize < 1) 5 else pageSize; 103 | jpaRepository.findAll(new PageRequest(rpage - 1, pageSize)) 104 | } 105 | 106 | } 107 | -------------------------------------------------------------------------------- /frontend/src/apitest.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import 'antd/dist/antd.css'; 4 | import {Button} from 'antd'; 5 | import axios from 'axios'; 6 | 7 | export default class ApiTest extends React.Component 8 | { 9 | onGetClick() 10 | { 11 | console.log("click get"); 12 | // get record 13 | axios.get("/scalatest/findid/2") 14 | .then(function (response) { 15 | console.log(response.data); 16 | }) 17 | .catch(function (error) { 18 | console.log(error); 19 | }); 20 | } 21 | 22 | onSaveCick() 23 | { 24 | console.log("click save"); 25 | // save one record 26 | axios({ 27 | method: 'post', 28 | url: '/scalatest/slickadd', 29 | data: { 30 | id: 0, 31 | name: "github", 32 | age: 25 33 | } 34 | }) 35 | .then(function (response) { 36 | console.log(response.data); 37 | }) 38 | .catch(function (error) { 39 | console.log(error); 40 | }); 41 | 42 | } 43 | 44 | onUpdateClick() 45 | { 46 | console.log("click update"); 47 | // update one record 48 | axios({ 49 | method: 'post', 50 | url: '/scalatest/slickupdate', 51 | data: { 52 | id: 0, 53 | name: "tashaxing", 54 | age: 25 55 | } 56 | }) 57 | .then(function (response) { 58 | console.log(response.data); 59 | }) 60 | .catch(function (error) { 61 | console.log(error); 62 | }); 63 | } 64 | 65 | onDeleteClick() 66 | { 67 | console.log("click delete"); 68 | // save one record 69 | axios.post("/scalatest/slickdelete/6") 70 | .then(function (response) { 71 | console.log(response.data); 72 | }) 73 | .catch(function (error) { 74 | console.log(error); 75 | }); 76 | } 77 | 78 | render() 79 | { 80 | return ( 81 |
82 |

test restapi for springboot scala

83 |

84 |

85 |

86 |

87 |
88 | ); 89 | } 90 | } 91 | 92 | ReactDOM.render(, document.getElementById('apitest')); 93 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.tashaxing 7 | SpringbootScalaDemo 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | SpringbootScalaDemo 12 | Demo project for Spring Boot with scala and java 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.5.7.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 2.12.3 27 | 2.12 28 | 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-web 38 | 39 | 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-starter-data-jpa 44 | 45 | 46 | 47 | 48 | com.typesafe.slick 49 | slick_2.12 50 | 3.2.1 51 | 52 | 53 | 54 | 55 | mysql 56 | mysql-connector-java 57 | 58 | 59 | 60 | 61 | org.scala-lang 62 | scala-library 63 | ${scala.version} 64 | 65 | 66 | org.scala-lang 67 | scala-compiler 68 | ${scala.version} 69 | 70 | 71 | 72 | org.springframework.boot 73 | spring-boot-starter-test 74 | test 75 | 76 | 77 | 78 | 79 | 80 | src/main/java_scala 81 | 82 | 83 | 84 | org.springframework.boot 85 | spring-boot-maven-plugin 86 | 87 | 88 | 89 | 90 | net.alchim31.maven 91 | scala-maven-plugin 92 | 3.2.1 93 | 94 | 95 | compile-scala 96 | compile 97 | 98 | add-source 99 | compile 100 | 101 | 102 | 103 | test-compile-scala 104 | test-compile 105 | 106 | add-source 107 | testCompile 108 | 109 | 110 | 111 | 112 | incremental 113 | ${scala.version} 114 | 115 | -deprecation 116 | 117 | 118 | -Xms64m 119 | -Xmx1024m 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM http://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven2 Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' 39 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 40 | 41 | @REM set %HOME% to equivalent of $HOME 42 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 43 | 44 | @REM Execute a user defined script before this one 45 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 46 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 47 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" 48 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" 49 | :skipRcPre 50 | 51 | @setlocal 52 | 53 | set ERROR_CODE=0 54 | 55 | @REM To isolate internal variables from possible post scripts, we use another setlocal 56 | @setlocal 57 | 58 | @REM ==== START VALIDATION ==== 59 | if not "%JAVA_HOME%" == "" goto OkJHome 60 | 61 | echo. 62 | echo Error: JAVA_HOME not found in your environment. >&2 63 | echo Please set the JAVA_HOME variable in your environment to match the >&2 64 | echo location of your Java installation. >&2 65 | echo. 66 | goto error 67 | 68 | :OkJHome 69 | if exist "%JAVA_HOME%\bin\java.exe" goto init 70 | 71 | echo. 72 | echo Error: JAVA_HOME is set to an invalid directory. >&2 73 | echo JAVA_HOME = "%JAVA_HOME%" >&2 74 | echo Please set the JAVA_HOME variable in your environment to match the >&2 75 | echo location of your Java installation. >&2 76 | echo. 77 | goto error 78 | 79 | @REM ==== END VALIDATION ==== 80 | 81 | :init 82 | 83 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 84 | @REM Fallback to current working directory if not found. 85 | 86 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 87 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 88 | 89 | set EXEC_DIR=%CD% 90 | set WDIR=%EXEC_DIR% 91 | :findBaseDir 92 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 93 | cd .. 94 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 95 | set WDIR=%CD% 96 | goto findBaseDir 97 | 98 | :baseDirFound 99 | set MAVEN_PROJECTBASEDIR=%WDIR% 100 | cd "%EXEC_DIR%" 101 | goto endDetectBaseDir 102 | 103 | :baseDirNotFound 104 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 105 | cd "%EXEC_DIR%" 106 | 107 | :endDetectBaseDir 108 | 109 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 110 | 111 | @setlocal EnableExtensions EnableDelayedExpansion 112 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 113 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 114 | 115 | :endReadAdditionalConfig 116 | 117 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 118 | 119 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 120 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 121 | 122 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 123 | if ERRORLEVEL 1 goto error 124 | goto end 125 | 126 | :error 127 | set ERROR_CODE=1 128 | 129 | :end 130 | @endlocal & set ERROR_CODE=%ERROR_CODE% 131 | 132 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost 133 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 134 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" 135 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" 136 | :skipRcPost 137 | 138 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 139 | if "%MAVEN_BATCH_PAUSE%" == "on" pause 140 | 141 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% 142 | 143 | exit /B %ERROR_CODE% 144 | -------------------------------------------------------------------------------- /mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven2 Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /etc/mavenrc ] ; then 40 | . /etc/mavenrc 41 | fi 42 | 43 | if [ -f "$HOME/.mavenrc" ] ; then 44 | . "$HOME/.mavenrc" 45 | fi 46 | 47 | fi 48 | 49 | # OS specific support. $var _must_ be set to either true or false. 50 | cygwin=false; 51 | darwin=false; 52 | mingw=false 53 | case "`uname`" in 54 | CYGWIN*) cygwin=true ;; 55 | MINGW*) mingw=true;; 56 | Darwin*) darwin=true 57 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home 58 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html 59 | if [ -z "$JAVA_HOME" ]; then 60 | if [ -x "/usr/libexec/java_home" ]; then 61 | export JAVA_HOME="`/usr/libexec/java_home`" 62 | else 63 | export JAVA_HOME="/Library/Java/Home" 64 | fi 65 | fi 66 | ;; 67 | esac 68 | 69 | if [ -z "$JAVA_HOME" ] ; then 70 | if [ -r /etc/gentoo-release ] ; then 71 | JAVA_HOME=`java-config --jre-home` 72 | fi 73 | fi 74 | 75 | if [ -z "$M2_HOME" ] ; then 76 | ## resolve links - $0 may be a link to maven's home 77 | PRG="$0" 78 | 79 | # need this for relative symlinks 80 | while [ -h "$PRG" ] ; do 81 | ls=`ls -ld "$PRG"` 82 | link=`expr "$ls" : '.*-> \(.*\)$'` 83 | if expr "$link" : '/.*' > /dev/null; then 84 | PRG="$link" 85 | else 86 | PRG="`dirname "$PRG"`/$link" 87 | fi 88 | done 89 | 90 | saveddir=`pwd` 91 | 92 | M2_HOME=`dirname "$PRG"`/.. 93 | 94 | # make it fully qualified 95 | M2_HOME=`cd "$M2_HOME" && pwd` 96 | 97 | cd "$saveddir" 98 | # echo Using m2 at $M2_HOME 99 | fi 100 | 101 | # For Cygwin, ensure paths are in UNIX format before anything is touched 102 | if $cygwin ; then 103 | [ -n "$M2_HOME" ] && 104 | M2_HOME=`cygpath --unix "$M2_HOME"` 105 | [ -n "$JAVA_HOME" ] && 106 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 107 | [ -n "$CLASSPATH" ] && 108 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 109 | fi 110 | 111 | # For Migwn, ensure paths are in UNIX format before anything is touched 112 | if $mingw ; then 113 | [ -n "$M2_HOME" ] && 114 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 115 | [ -n "$JAVA_HOME" ] && 116 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 117 | # TODO classpath? 118 | fi 119 | 120 | if [ -z "$JAVA_HOME" ]; then 121 | javaExecutable="`which javac`" 122 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 123 | # readlink(1) is not available as standard on Solaris 10. 124 | readLink=`which readlink` 125 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 126 | if $darwin ; then 127 | javaHome="`dirname \"$javaExecutable\"`" 128 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 129 | else 130 | javaExecutable="`readlink -f \"$javaExecutable\"`" 131 | fi 132 | javaHome="`dirname \"$javaExecutable\"`" 133 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 134 | JAVA_HOME="$javaHome" 135 | export JAVA_HOME 136 | fi 137 | fi 138 | fi 139 | 140 | if [ -z "$JAVACMD" ] ; then 141 | if [ -n "$JAVA_HOME" ] ; then 142 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 143 | # IBM's JDK on AIX uses strange locations for the executables 144 | JAVACMD="$JAVA_HOME/jre/sh/java" 145 | else 146 | JAVACMD="$JAVA_HOME/bin/java" 147 | fi 148 | else 149 | JAVACMD="`which java`" 150 | fi 151 | fi 152 | 153 | if [ ! -x "$JAVACMD" ] ; then 154 | echo "Error: JAVA_HOME is not defined correctly." >&2 155 | echo " We cannot execute $JAVACMD" >&2 156 | exit 1 157 | fi 158 | 159 | if [ -z "$JAVA_HOME" ] ; then 160 | echo "Warning: JAVA_HOME environment variable is not set." 161 | fi 162 | 163 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 164 | 165 | # traverses directory structure from process work directory to filesystem root 166 | # first directory with .mvn subdirectory is considered project base directory 167 | find_maven_basedir() { 168 | 169 | if [ -z "$1" ] 170 | then 171 | echo "Path not specified to find_maven_basedir" 172 | return 1 173 | fi 174 | 175 | basedir="$1" 176 | wdir="$1" 177 | while [ "$wdir" != '/' ] ; do 178 | if [ -d "$wdir"/.mvn ] ; then 179 | basedir=$wdir 180 | break 181 | fi 182 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 183 | if [ -d "${wdir}" ]; then 184 | wdir=`cd "$wdir/.."; pwd` 185 | fi 186 | # end of workaround 187 | done 188 | echo "${basedir}" 189 | } 190 | 191 | # concatenates all lines of a file 192 | concat_lines() { 193 | if [ -f "$1" ]; then 194 | echo "$(tr -s '\n' ' ' < "$1")" 195 | fi 196 | } 197 | 198 | BASE_DIR=`find_maven_basedir "$(pwd)"` 199 | if [ -z "$BASE_DIR" ]; then 200 | exit 1; 201 | fi 202 | 203 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} 204 | echo $MAVEN_PROJECTBASEDIR 205 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 206 | 207 | # For Cygwin, switch paths to Windows format before running java 208 | if $cygwin; then 209 | [ -n "$M2_HOME" ] && 210 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 211 | [ -n "$JAVA_HOME" ] && 212 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 213 | [ -n "$CLASSPATH" ] && 214 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 215 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 216 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` 217 | fi 218 | 219 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 220 | 221 | exec "$JAVACMD" \ 222 | $MAVEN_OPTS \ 223 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 224 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 225 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 226 | -------------------------------------------------------------------------------- /src/main/java_scala/org/tashaxing/SpringbootScalaDemo/controller/ScalaTestController.scala: -------------------------------------------------------------------------------- 1 | package org.tashaxing.SpringbootScalaDemo.controller 2 | 3 | import org.springframework.beans.factory.annotation.Autowired 4 | import org.springframework.web.bind.annotation._ 5 | import org.tashaxing.SpringbootScalaDemo.model.ScalaModel 6 | import org.tashaxing.SpringbootScalaDemo.repository.ScalaModelQuery 7 | 8 | import slick.jdbc.MySQLProfile.api._ 9 | import org.tashaxing.SpringbootScalaDemo.model.SlickDB._ 10 | import scala.concurrent.Await 11 | import scala.concurrent.duration.Duration 12 | 13 | import scala.collection.mutable 14 | import scala.util.Random 15 | import java.util.List 16 | import javax.validation.Valid 17 | 18 | import org.springframework.validation.BindingResult 19 | 20 | @RestController // here must be restcontroller 21 | @RequestMapping(Array("/scalatest")) 22 | class ScalaTestController @Autowired()(private val scalaModelQuery: ScalaModelQuery) 23 | { 24 | // slick db config, usually it is in a config file 25 | val db = Database.forURL( 26 | url = "jdbc:mysql://localhost:3306/db_example?useUnicode=true&characterEncoding=UTF-8&useSSL=false", 27 | driver = "com.mysql.jdbc.Driver", 28 | user = "springuser", 29 | password = "ThePassword") 30 | 31 | // ---- normal operation 32 | // root test 33 | @GetMapping 34 | def root = "hello scala springboot" 35 | 36 | // get test with short mapping 37 | @GetMapping(Array("/string1")) 38 | def getTest1(): String = { 39 | return "scala test1 results" 40 | } 41 | 42 | // get test with whole mapping 43 | @RequestMapping(value = Array("/string2"), method = Array(RequestMethod.GET)) 44 | def getTest2(): String = { 45 | return "scala test2 results" 46 | } 47 | 48 | // get int list 49 | @GetMapping(Array("/list")) 50 | def getIntList(): Array[Int] = { 51 | return Array(1, 2, 3, 4) 52 | } 53 | 54 | // get object(frontend will get json structure) 55 | @GetMapping(Array("/object")) 56 | def getObject(): ScalaModel = { 57 | val model = new ScalaModel // if it is defined with class 58 | model.id = 3L 59 | model.name = "lucy" 60 | model.age = 21 61 | 62 | // val model = ScalaModel(5L, "lily", 23) // if it is defined with abstract class 63 | return model 64 | } 65 | 66 | // // get object 67 | // @GetMapping(Array("/slickobject")) 68 | // def getSlickObject(): UserInfo = { 69 | // val userInfo = UserInfo(5L, "alice", 17) 70 | // return userInfo 71 | // } 72 | 73 | // get object with param 74 | @GetMapping(Array("/object/{id}")) 75 | def getObject(@PathVariable("id") id: Long): ScalaModel = { 76 | val random_age = Random.nextInt(10) 77 | val model = new ScalaModel 78 | model.id = id 79 | model.name = "tom" 80 | model.age = 20 + random_age 81 | return model 82 | } 83 | 84 | // get list of object 85 | @GetMapping(Array("/objectlist")) 86 | def getObjectList(): Array[ScalaModel] = 87 | { 88 | val model_list = mutable.ArrayBuffer[ScalaModel]() 89 | for (i <- 0 to 9) 90 | { 91 | val model = new ScalaModel 92 | model.id = i.toLong 93 | model.name = "lily" 94 | model.age = 20 + i 95 | 96 | model_list += model 97 | } 98 | return model_list.toArray 99 | } 100 | 101 | // post single param 102 | @PostMapping(Array("/upload")) 103 | def postInfo(@PathVariable("name") name: String): String = 104 | { 105 | return "post param $name got" 106 | } 107 | 108 | // ---- database operation 109 | 110 | /*jpa*/ 111 | // get all recorde(remember to use java List not scala List) 112 | @GetMapping(Array("/listmodel")) 113 | def listmodel(): List[ScalaModel] = 114 | { 115 | val res = scalaModelQuery.findAll 116 | 117 | // do sth here 118 | 119 | return res 120 | } 121 | 122 | // get 123 | @GetMapping(value = Array("/findid/{id}")) 124 | def findById(@PathVariable(value = "id") id: Long): ScalaModel = 125 | { 126 | return scalaModelQuery.find(id) 127 | } 128 | 129 | @GetMapping(Array("/findname/{name}")) 130 | def findByName(@PathVariable("name") name: String): List[ScalaModel] = 131 | { 132 | return scalaModelQuery.findByName(name) 133 | } 134 | 135 | // post 136 | @PostMapping(Array("/delete/{id}")) 137 | def deleteById(@PathVariable("id") id: Long): String = 138 | { 139 | val res = scalaModelQuery.delete(id) 140 | return "delete success" 141 | } 142 | 143 | @RequestMapping(value = Array("/add"), method = Array(RequestMethod.POST)) 144 | def save(@RequestBody scalaModel: ScalaModel): ScalaModel = 145 | { 146 | // notice here use RequestBody 147 | val res = scalaModelQuery.save(scalaModel) 148 | return res 149 | } 150 | 151 | @PostMapping(Array("/update")) 152 | def update(@RequestBody scalaModel: ScalaModel, bindingResult: BindingResult): ScalaModel = 153 | { 154 | val res = scalaModelQuery.update(scalaModel) 155 | return res 156 | } 157 | 158 | /*slick*/ 159 | @GetMapping(Array("/getslick/{name}")) 160 | def getSlickModel(@PathVariable("name") name: String): UserInfoObject = 161 | { 162 | val slickres = Await.result(db.run(slick_table.filter(_.name === name).result), Duration.Inf) 163 | 164 | val userInfoObject = new UserInfoObject 165 | userInfoObject.id = slickres(0).id 166 | userInfoObject.name = slickres(0).name 167 | userInfoObject.age = slickres(0).age 168 | 169 | return userInfoObject 170 | } 171 | 172 | @GetMapping(Array("/listslick")) 173 | def listSlickModel(): Array[UserInfoObject] = 174 | { 175 | val slickres = Await.result(db.run(slick_table.result), Duration.Inf) 176 | // val slickres = Await.result(db.run(slick_table.filter(_.age < 22).result), Duration.Inf) 177 | val res = mutable.ArrayBuffer[UserInfoObject]() 178 | slickres.map( 179 | record => { 180 | val userInfoObject = new UserInfoObject 181 | userInfoObject.id = record.id 182 | userInfoObject.name = record.name 183 | userInfoObject.age = record.age 184 | res += userInfoObject 185 | }) 186 | return res.toArray 187 | } 188 | 189 | @PostMapping(Array("/slickadd")) 190 | def slicksave(@RequestBody userInfoObject: UserInfoObject): String = 191 | { 192 | val userInfo = UserInfo(userInfoObject.id, userInfoObject.name, userInfoObject.age) 193 | val userArray = Array[UserInfo](userInfo) 194 | Await.result(db.run(slick_table ++= userArray), Duration.Inf) 195 | 196 | return "save success" 197 | } 198 | 199 | @PostMapping(Array("/slickupdate")) 200 | def slickupdate(@RequestBody userInfoObject: UserInfoObject): String = 201 | { 202 | val userInfo = UserInfo(userInfoObject.id, userInfoObject.name, userInfoObject.age) 203 | val userArray = Array[UserInfo](userInfo) 204 | Await.result(db.run(slick_table.filter(_.name === userInfo.name).delete), Duration.Inf) 205 | Await.result(db.run(slick_table ++= userArray), Duration.Inf) 206 | 207 | return "update success" 208 | } 209 | 210 | @PostMapping(Array("/slickdelete/{id}")) 211 | def slickdelete(@PathVariable("id") id: Long): String = 212 | { 213 | Await.result(db.run(slick_table.filter(_.id === id).delete), Duration.Inf) 214 | 215 | return "delete success" 216 | } 217 | 218 | } 219 | --------------------------------------------------------------------------------