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

时间:2023-01-27 16:06:40

在上一篇博客中我们实现了最基本的Spring Data Jpa,在本篇博客中我们将进一步整合Spring。

修改pom.xml

<!-- 添加spring-webmvc,web.xmlContextLoaderListener监听器需要 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>

添加web.xml

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>JPA</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

添加applicationContext.xml

<!-- 放在classpath下,即resources目录下 -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"
>


<!-- 开启spring注解 -->
<context:annotation-config/>
<!-- 在指定的package下扫描以及注册javabean -->
<context:component-scan base-package="com.xyc.jpa"/>

</beans>

修改UserDaoImpl

package com.xyc.jpa.dao.impl;

import com.xyc.jpa.dao.UserDao;
import com.xyc.jpa.pojo.User;
import org.springframework.stereotype.Repository;

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

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

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

修改UserServiceImpl

package com.xyc.jpa.service.impl;

import com.xyc.jpa.dao.UserDao;
import com.xyc.jpa.pojo.User;
import com.xyc.jpa.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

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

@Service("userService")
public class UserServiceImpl implements UserService {
/**
* private UserDao userDao = new UserDaoImpl();
*/

/**
* 改为注解
*/

@Resource
private UserDao userDao;

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 org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

public class UserServiceTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) ctx.getBean("userService");
userService.create("haha", "123456", 18);
}
}

运行UserServiceTest,数据库会插入一条记录。