git clone https://github.com/spring-guides/gs-accessing-data-jpa.git
└── src
└── main
└── java
└── hello
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'gs-spring-boot'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
// tag::jetty[]
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: "spring-boot-starter-tomcat"
}
compile("org.springframework.boot:spring-boot-starter-jetty")
// end::jetty[]
// tag::actuator[]
compile("org.springframework.boot:spring-boot-starter-actuator")
// end::actuator[]
compile('org.springframework.boot:spring-boot-starter-data-jpa:1.3.3.RELEASE')
compile('mysql:mysql-connector-java:5.1.13')
testCompile("junit:junit")
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
spring.datasource.url = jdbc:mysql://localhost:3306/test spring.datasource.username = root spring.datasource.password = root #spring.datasource.driverClassName = com.mysql.jdbc.Driver # Specify the DBMS spring.jpa.database = MYSQL # Keep the connection alive if idle for a long time (needed in production) spring.datasource.testWhileIdle = true spring.datasource.validationQuery = SELECT 1 # Show or not log for each sql query spring.jpa.show-sql = true # Hibernate ddl auto (create, create-drop, update) spring.jpa.hibernate.ddl-auto = update # Naming strategy spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy # Use spring.jpa.properties.* for Hibernate native properties (the prefix is # stripped before adding them to the entity manager) # The SQL dialect makes Hibernate generate better SQL for the chosen database spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
package hello.Entities
import javax.validation.constraints.NotNull
import java.io.Serializable;
import javax.persistence.*;
/**
* Created by Bruce on 2016/3/9.
*/
@Entity
@Table(name = "user")
data class User(@Id @GeneratedValue(strategy = GenerationType.AUTO) var id: Long? = 0,
@Column(nullable = false) var name: String? = null,
@Column(nullable = false) var email: String? = null) : Serializable {
protected constructor() : this(id = null, name = null, email = null) {
}
}
package hello.Entities
import org.springframework.data.repository.CrudRepository
import org.springframework.transaction.annotation.Transactional
@Transactional
interface UserDao : CrudRepository<User, Long> {
fun findByEmail(email: String): User?
}
package hello.Controllers
import hello.Entities.User
import hello.Entities.UserDao
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseBody
/**
* Created by Bruce on 2016/3/9.
*/
@RestController
class UserController {
@Autowired
private var userDao: UserDao? = null
@RequestMapping("/create")
@ResponseBody
public fun create(name: String, email: String): User? {
try {
var newUser = User(name = name, email = email)
userDao?.save(newUser)
return newUser
} catch(e: Exception) {
return null
}
}
@RequestMapping("/delete")
@ResponseBody
public fun delete(id: Long): String {
try {
var user = User(id)
userDao?.delete(user)
return id.toString() + "deleted"
} catch(e: Exception) {
return "delete error " + e.message.toString()
}
}
@RequestMapping("/get-by-email")
@ResponseBody
public fun getByEmail(email: String): User? {
try {
var user = userDao?.findByEmail(email)
if (user != null) {
return user
} else {
return null
}
} catch(e: Exception) {
return null
}
}
@RequestMapping("/update")
@ResponseBody
public fun updateUser(id: Long, name: String, email: String): User? {
try {
var user: User? = userDao?.findOne(id) ?: return null
user?.name = name
user?.email = email
userDao?.save(user)
return user
} catch(e: Exception) {
return null
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有