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

原创 2016年08月30日 23:08:35

虽然一直在用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的最基本实现,未完待续!

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Spring Data JPA 入门学习笔记

根据 Spring Data JAP Guide 入门 Sping JPA 过程记录 环境准备 项目要去 jdk1.8 + ,Application.java 中使用了 jdk1.8 + 的lamb...

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

在上一篇博客中我们实现了最基本的Spring Data Jpa,在本篇博客中我们将进一步整合Spring。 修改pom.xml org.springframework spring-...

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

本次在http://blog.csdn.net/xyc_csdn/article/details/52564149的基础上添加了Controller层并添加了显示页面用于输出列表。 修改pom.xml...

spring-data-jpa 学习笔记一(转)

基础学习笔记. http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/  1. Spring...

spring-data的学习笔记

  • 2017年11月06日 18:03
  • 129KB
  • 下载

巴巴运动网学习笔记二之spring3.1+jpa的环境配置

a.导入spring所用jar包 b.新建src/beans.xml文件  

spring boot + jpa学习笔记

pom.xml:
  • sunhuwh
  • sunhuwh
  • 2016年08月05日 11:38
  • 1800

MongoDB学习笔记(三)使用Spring Data操作MongoDB

1.所需的包: 2.代码: (1)配置文件一:bootstrap.xml //代码开始     xmlns:xsi="http://www.w3.org/2001/XMLSchema-in...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Spring Data Jpa开发学习笔记(一)
举报原因:
原因补充:

(最多只允许输入30个字)