By Spring boot
A : 大概最少要
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.2.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
@Entity
@Table(name = "HRUSER")
public class HrUser implements Serializable {
private static final long serialVersionUID = 1L;
@Size(max = 10)
@Column(name = "CPNYID")
private String cpnyid;
@Id
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 20)
@Column(name = "EMPID")
private String empId;
@Size(max = 7)
@Column(name = "DEPT_NO")
private String deptNo;
@Size(max = 10)
@Column(name = "POSSIE")
private String possie;
/*
* ....以下省略
*/
}
@RepositoryRestResource(collectionResourceRel = "user", path = "user")
public interface HrUserRepository
extends PagingAndSortingRepository<HrUser, String> {
HrUser findByLoginId(@Param("loginId")String loginId);
HrUser findByEmpId(@Param("empId") String empId);
}
tip: @RepositoryRestResource()可以不加 , but spring boot 會很親切的幫你加上複數在path , 例如 user 變成 users
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
# EMBEDDED SERVER CONFIGURATION (ServerProperties)
server.port=9876
# DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url=jdbc:sqlserver://
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.SQLServer2008Dialect
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
spring.jpa.show-sql=false
console > mvn clean package
console > java -jar target/rest-data.jar
public class App{
public static void main(String[] args) throws Exception {
System.getProperties()
.setProperty("spring.config.location",
ConfigPath.getDefaultPath()+"anno-rest.properties");
SpringApplication.run(App.class, args);
}
}
# EMBEDDED SERVER CONFIGURATION (ServerProperties)
server.port=
server.context-path=/anno-rest
# DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url=jdbc:mariadb://____
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.testOnBorrow=true
spring.datasource.validationQuery=SELECT 1
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
spring.jpa.show-sql=false