Spring Data Jpa开发学习笔记(一)

时间:2022-05-15 15:13:45

虽然一直在用Spring Data Jpa,但是只是在用,也没有从零自己配置过,所有打算借这次机会,自己研究研究。

目录结构

Spring Data Jpa开发学习笔记(一)

pom.xml

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.10.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.1.0.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>

persistence.xml

<!-- 注意:persistence.xml要放在resources/META-INF目录下 -->
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="jpaUnit" transaction-type="RESOURCE_LOCAL">
<!-- HibernatePersistence不提供的话,可以换成HibernatePersistenceProvider -->
<!--<provider>org.hibernate.ejb.HibernatePersistence</provider>-->
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.xyc.chat.pojo.User</class>
<properties>
<property name="hibernate.connection.driver_class"
value="com.mysql.jdbc.Driver"/>

<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/jpa"/>

<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="123456"/>
<property name="hibernate.dialect"
value="org.hibernate.dialect.MySQL5Dialect"/>

<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.use_sql_comments" value="false"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>

User

package com.xyc.jpa.pojo;

import javax.persistence.*;

/**
* Created by xyc on 2016/8/28 0028.
*/

@Entity
@Table(name = "t_user")
public class User {
@Id
@Column(name = "id", nullable = false)
@GeneratedValue
private Long id;
@Column(name = "username", nullable = true)
private String username;
@Column(name = "password", nullable = true)
private String password;
@Column(name = "age", nullable = true)
private Integer age;

public User() {
}

public User(String username, String password, Integer age) {
this.username = username;
this.password = password;
this.age = age;
}

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 Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}
}

UserDao

package com.xyc.jpa.dao;

import com.xyc.jpa.pojo.User;

/**
* Created by xyc on 2016/8/28 0028.
*/

public interface UserDao {
User save(User user);
}

UserDaoImpl

package com.xyc.jpa.dao.impl;

import com.xyc.jpa.dao.UserDao;
import com.xyc.jpa.pojo.User;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
* Created by xyc on 2016/8/28 0028.
*/

public class UserDaoImpl implements UserDao {
public User save(User user) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpaUnit");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(user);
em.getTransaction().commit();
emf.close();
return user;
}
}

UserService

package com.xyc.jpa.service;

import com.xyc.jpa.pojo.User;

/**
* Created by xyc on 2016/8/28 0028.
*/

public interface UserService {
User create(String username,String password,Integer age);
}

UserServiceImpl

package com.xyc.jpa.service.impl;

import com.xyc.jpa.dao.UserDao;
import com.xyc.jpa.dao.impl.UserDaoImpl;
import com.xyc.jpa.pojo.User;
import com.xyc.jpa.service.UserService;

/**
* Created by xyc on 2016/8/28 0028.
*/

public class UserServiceImpl implements UserService {
private UserDao userDao = new UserDaoImpl(); ;

public User create(String username, String password, Integer age) {
// 封装域对象
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.setAge(age);
// 调用持久层,完成数据的保存
return userDao.save(user);
}
}

UserServiceTest

package com.xyc.jpa.service;

import com.xyc.jpa.service.impl.UserServiceImpl;

/**
* Created by xyc on 2016/8/28 0028.
*/

public class UserServiceTest {
public static void main(String[] args) {
new UserServiceImpl().create("haha", "123456", 18);
}
}

运行main方法,你的数据库中就会插入一条数据。
以上Spring Data Jpa的最基本实现,未完待续!