jpa spring

Main:

public static void main(String[] args) {
// TODO Auto-generated method stub
//new ProductServiceImpl().createNewProduct(9, "iphoneX", 8.88);
double a = 3.0;
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext("spring-demo-cfg.xml");
// ProductService productService = ctx.getBean("productService", ProductService.class);
// // productService.createNewProduct(20,"mac3", a);
// System.out.println(productService.findProductById(16).getDescription());
ProductDao productDao = ctx.getBean("productDao", ProductDao.class);
// Product p = new Product();
// p.setId(16);
// p.setDescription("HTC");
// p.setPrice(8.88);
System.out.print(productDao.findById(18));
productDao.updateById(26, 8);
}

productDao:

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

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
public interface ProductDao extends JpaRepository<Product, Long> {
public Product save(Product product);

@Query("select a from Product a where a.id = ?1")
public Product findById(int id);

@Transactional
@Modifying(clearAutomatically=true)
@Query("update Product a set a.id = ?1 where a.id = ?2")
public void updateById(int afterid, int beforeid);
}


//public interface ProductDao {
//
// public Product saveProduct(Product prod);
//
// public Product save(Product prod);
//}

 

spring-demo-cfg.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package=""/>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="entityManagerFactory" class=
"org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
</bean>
<jpa:repositories base-package=""
entity-manager-factory-ref="entityManagerFactory"
transaction-manager-ref="transactionManager"/>
</beans>

persisitence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="SimplePU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.driver_class"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="hibernate.connection.url"
value="jdbc:sqlserver://munsqldev1:2647;databaseName=CCC"/>
<property name="hibernate.connection.username" value=""/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.dialect"
value="org.hibernate.dialect.SQLServerDialect"/>
<property name="hibernate.show_sql" value="false"/>
<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>

 

Product:

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="products")
public class Product implements Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;

@Id
private int id;
private String description;
private Double price;

public void setId(int i) {
id = i;
}

public int getId() {
return id;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public Double getPrice() {
return price;
}

public void setPrice(Double price) {
this.price = price;
}

public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append("Description: " + description + ";");
buffer.append("Price: " + price);
return buffer.toString();
}
}

posted on 2017-09-21 21:15  白衣胜雪  阅读(134)  评论(0编辑  收藏  举报