使用spring data jpa + maven开发服务端接口

整体目录结构如下:

使用spring data jpa + maven开发服务端接口_第1张图片


pom.xml 内容:

<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.lala</groupId>
	<artifactId>myapi</artifactId>
	<version>1.0.0</version>
	<packaging>jar</packaging>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.2.0.RELEASE</version>
	</parent>

	<name>myapi</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-jpa</artifactId>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
		</dependency>

		<dependency>
			<groupId>org.postgresql</groupId>
			<artifactId>postgresql</artifactId>
			<version>9.3-1102-jdbc41</version>
		</dependency>

		<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>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<verbose>true</verbose>
				</configuration>
			</plugin>

			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-eclipse-plugin</artifactId>
				<configuration>
					<sourceExcludes>
						<sourceExclude>**/.svn/</sourceExclude>
					</sourceExcludes>
					<downloadSources>true</downloadSources>
					<ajdtVersion>none</ajdtVersion>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>


application.properties 内容:

pp.name=myapi
app.description=${reader.name} is a Spring Boot application
app.version=1.0.0
server.port=9060

spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://127.0.0.1/db_cloud
spring.datasource.username=xrkadmin
spring.datasource.password=admin

spring.jpa.show-sql=true
spring.jpa.hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect
spring.jpa.hibernate.ddl-auto= none


User.java 内容 :

package com.lala.myapi;

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
 * Created by admin on 14-10-21
 */
@Entity
@Table(name = "users")
public class User{
	@Id
    @Column
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer uid;//自增长的id(userId)
	@Column
	private String uname;
	@Column
	private String nick;
	@Column
	private String email;
	@Column
	private Date ctime;
	@Column
	private Integer status;
	public Integer getUid() {
		return uid;
	}
	public void setUid(Integer uid) {
		this.uid = uid;
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public String getNick() {
		return nick;
	}
	public void setNick(String nick) {
		this.nick = nick;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Date getCtime() {
		return ctime;
	}
	public void setCtime(Date ctime) {
		this.ctime = ctime;
	}
	public Integer getStatus() {
		return status;
	}
	public void setStatus(Integer status) {
		this.status = status;
	}
}

UserRepository.java 内容 :

package com.lala.myapi;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends CrudRepository<User, Integer> 
{

}

AppController.java 内容为:

package com.lala.myapi;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/api")
public class AppController 
{
	@Autowired
	UserRepository userRepository;

	@ResponseBody
    @RequestMapping(value = "/v1/find_by_id", method = RequestMethod.GET)
    public Object find()
    {
		return userRepository.findOne(1);
    }
}


App.java 内容为:


package com.lala.myapi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

/**
 * Hello world!
 *
 */
@ComponentScan("com")
@EnableJpaRepositories("com")
@Configuration
@EnableAutoConfiguration
public class App 
{
    public static void main( String[] args )
    {
    	SpringApplication.run(App.class, args);
    }
}


最后,运行App文件,启动成功之后,在浏览器中输入 http://127.0.0.1:9060/api/v1/find_by_id 即可看到效果

你可能感兴趣的:(使用spring data jpa + maven开发服务端接口)