这是我用来解决依赖关系的maven构建项目的pom.xml。
的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.raj</groupId>
<artifactId>hSQLdb1</artifactId>
<version>0.1.0</version>
<properties>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
以下是我的示例应用程序的属性文件,我在使用applicationproperties和application-hsqldb.properties
application.properties
spring.profiles.active=hsqldb
# Hibernate
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.jadira.usertype.autoRegisterUserTypes=true
**application-hsqldb.properties**
# Hibernate
spring.jpa.hibernate.ddl-auto=validate
# Initialization
spring.datasource.schema=classpath:/data/hsqldb/schema.sql
spring.datasource.data=classpath:/data/hsqldb/data.sql
data.sql
INSERT INTO EMP(f_name, l_name) VALUES('Rajendar','Kumar');
INSERT INTO EMP(f_name, l_name) VALUES('Ramesh','Kumar');
schema.sql文件
DROP TABLE EMP IF EXISTS;
CREATE TABLE emp (
id BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1) NOT NULL,
f_name VARCHAR(100) NOT NULL,
l_name VARCHAR(100) NOT NULL,
PRIMARY KEY (id)
);
以下是我的休息服务的控制器,它有两个用于问候和从数据库读取数据的服务
import com.raj.hSQLdb.pojo.Employee;
import com.raj.hSQLdb.pojo.Greating;
import com.raj.hSQLdb.service.impl.ProcessEmpDataService;
@RestController
@RequestMapping("/app")
public class Controller{
@Autowired
private ProcessEmpDataService empDataService;
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
@GetMapping("/greeting")
public String sayHello(@RequestParam(value="name", defaultValue="Rajendar") String name) {
return new Greating(String.valueOf(counter.incrementAndGet()), String.format(template, name)).toString();
}
@RequestMapping(value = {"/read"}, method = RequestMethod.GET)
public ResponseEntity<?> read(Long id) {
Employee response = empDataService.findOne(id);
return new ResponseEntity<Employee>(response,HttpStatus.OK);
}
}
实体
package com.raj.hSQLdb.pojo;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="emp")
public class Employee implements Serializable{
private static final long serialVersionUID = 5667869092245806231L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "f_name")
private String firstName;
@Column(name = "l_name")
private String lastName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
}
}
** Greating **
package com.raj.hSQLdb.pojo;
public class Greating{
public Greating(String code, String message) {
this.code=code;
this.message=message;
}
protected String code;
private String message;
public String getCode() {
return code;
}
public String getMessage() {
return message;
}
}
存储库
package com.raj.hSQLdb.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.raj.hSQLdb.pojo.Employee;
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long>{
}
服务
package com.raj.hSQLdb.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.raj.hSQLdb.pojo.Employee;
import com.raj.hSQLdb.repository.EmployeeRepository;
@Component
public class ProcessEmpDataService {
@Autowired
private EmployeeRepository repository;
public Employee findOne(Long id) {
Employee emp = repository.findOne(id);
return emp;
}
}
运行应用程序的主类
package com.raj.hSQLdb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableTransactionManagement
public class ServletInitializer {
/**
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(ServletInitializer.class, args);
}
}