构建第一个SpringData JPA项目

1.JPA简介
JPA是Java Persistence API的简称, 中文名为Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系, 并将运行期的实体对象持久化到数据库中。
Sun引入新的JPA ORM规范出于两个原因: 其一, 简化现有JavaEE和Java SE应用开发工作; 其二, Sun希望整合ORM技术, 实现天下归一。
2.JPA核心内容
一套API标准。 在javax.persistence的包下面, 用来操作实体对象, 执行CRUD操作, 框架在后台替代我们完成所有的事情, 开发者从烦琐的JDBC和SQL代码中解脱出来。
面向对象的查询语言: Java Persistence QueryLanguage(JPQL) 。 这是持久化操作中很重要的一个方面, 通过面向对象而非面向数据库的查询语言查询数据, 避免程序的SQL语句紧密耦合。
ORM(object/relational metadata) 元数据的映射。 JPA支持XML和JDK5.0注解两种元数据的形式, 元数据描述对象和表之间的映射关系, 框架据此将实体对象持久化到数据库表中。

构建第一个SpringData JPA项目

1.创建maven项目并添加POM依赖


		org.springframework.boot
		spring-boot-starter-parent
		1.5.2.RELEASE
		 
	


	
		org.springframework.boot
		spring-boot-starter-data-jpa
	

	
		org.springframework.boot
		spring-boot-starter-web
	

	
		mysql
		mysql-connector-java
	

	
		org.springframework.boot
		spring-boot-starter-test
		test
	

	
		com.alibaba
		druid
		1.0.29
	




	
		
			org.apache.maven.plugins
			maven-compiler-plugin
			
				1.8
				1.8
			
		
		
			org.springframework.boot
			spring-boot-maven-plugin
		
	

代码目录结构
构建第一个SpringData JPA项目_第1张图片
2.实体类代码如下

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="sysusers")
public class User implements Serializable {

	private static final long serialVersionUID = -2585926647461040314L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)    
	private long id;

	@Column(name = "userName")
	private String userName;

	@Column(name = "passWord")
	private String passWord;

	@Column(name = "role")
	private String role;

	@Column(name = "yn")
	private String yn;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassWord() {
		return passWord;
	}

	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public String getYn() {
		return yn;
	}

	public void setYn(String yn) {
		this.yn = yn;
	}

}

3.创建Dao层接口

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.hzyc.springdata.jpa.domain.User;

@Repository
public interface UserJpaRepository 
	extends JpaRepository{

}

4.创建Service层
实现类

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hzyc.springdata.jpa.dao.UserJpaRepository;
import com.hzyc.springdata.jpa.domain.User;
import com.hzyc.springdata.jpa.service.IUserService;

@Service
public class UserServiceImpl implements IUserService {

	@Resource
	private UserJpaRepository userJpaRepository;
	
	@Override
	public boolean addUser(User user) {
		long id = userJpaRepository.save(user).getId();
		System.out.println("--------------"+id);
		return id>0;
	}

	@Override
	public User get(long id) {
		return userJpaRepository.findOne(id);
	}

}

接口

import com.hzyc.springdata.jpa.domain.User;

public interface IUserService {

	public boolean addUser(User user);
	
	public User get(long id);
}

5.创建控制器Controller

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hzyc.springdata.jpa.domain.User;
import com.hzyc.springdata.jpa.service.IUserService;

@Controller
public class UserController {

	@Resource
	private IUserService userService;
	
	@RequestMapping("/getUser")
	@ResponseBody
	public User getUser(long id){
		return userService.get(id);
	}
}

6.Application

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan("com.hzyc.springdata.jpa")
@EnableAutoConfiguration
public class Application {

	public static void main(String[] args) throws Exception {
		SpringApplication.run(Application.class, args);
	}
}

7.application.yml代码

spring:
 datasource:
  driverClassName: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost:3306/tf68?useUnicode=true&characterEncoding=utf8
  username: root
  password: root
 jpa:
  showSql: true #显示sql语句
  hibernate:
   naming: #忽略驼峰命题替换为下划线
    physicalStrategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

你可能感兴趣的:(SpringData,JPA)