一个spring data jpa的有关问题
一个spring data jpa的问题
实体类:
Product.java
Dao类:
ProductDao.java
Service接口:
ProductService.java
ServiceImpl类:
ProductServiceImpl.java
测试类:
Test.java
jpa配置文件:
persistence.xml
spring配置文件:
applicationContext.xml
实体类:
Product.java
package com.original.springdata.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "product")
public class Product {
private Integer id;
private String name;
private Float price;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
}
Dao类:
ProductDao.java
package com.original.springdata.dao;
import org.springframework.data.repository.Repository;
import com.original.springdata.entity.Product;
public interface ProductDao extends Repository<Product,Integer>{
public Product save(Product product);
}
Service接口:
ProductService.java
package com.original.springdata.service;
import com.original.springdata.entity.Product;
public interface ProductService {
public Product createNewProduct(String name,float price);
}
ServiceImpl类:
ProductServiceImpl.java
package com.original.springdata.service;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.original.springdata.dao.ProductDao;
import com.original.springdata.entity.Product;
@Service("productService")
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductDao productDao;
@Override
@Transactional
public Product createNewProduct(String name, float price) {
Product product = new Product();
product.setName(name);
product.setPrice(price);
return productDao.save(product);
}
}
测试类:
Test.java
package com.original.springdata.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.original.springdata.service.ProductService;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
ProductService productService = ctx.getBean("productService", ProductService.class);
productService.createNewProduct("calculator", 100);
ctx.close();
}
}
jpa配置文件:
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="springdata">
<class>com.original.springdata.entity.Product</class>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/springdata"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="root"/>
<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>
spring配置文件:
applicationContext.xml
<?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"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
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-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">