spring2.5集成jpa
程序员文章站
2022-03-02 14:37:19
...
所需jar包:
spring2.5:
aspectjrt.jar
aspectjweaver.jar
cglib-nodep-2.1_3.jar
common-annotations.jar
commons-logging.jar
log4j-1.2.15.jar
spring.jar
spring-webmvc-struts.jar
hibernate3.3:
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
ehcache-1.2.3.jar
ejb3-persistence.jar
hibernate3.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
javassist-3.9.0.GA.jar
jta-1.1.jar
slf4j-api-1.5.8.jar
slf4j-log4j12-1.5.8.jar
c3p0-0.9.1.2.jar
mysql-connector-java-5.1.7-bin.jar
数据库使用的是mysql5,脚本:
/*
MySQL Data Transfer
Source Host: localhost
Source Database: ssjdb
Target Host: localhost
Target Database: ssjdb
Date: 2010-3-30 18:47:37
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for student_
-- ----------------------------
DROP TABLE IF EXISTS `student_`;
CREATE TABLE `student_` (
`id_` int(11) NOT NULL AUTO_INCREMENT,
`name_` varchar(20) DEFAULT NULL,
`sex_` varchar(20) DEFAULT NULL,
`age_` int(11) DEFAULT NULL,
`grade_` varchar(60) DEFAULT NULL,
`class_` varchar(60) DEFAULT NULL,
PRIMARY KEY (`id_`)
) ENGINE=MyISAM AUTO_INCREMENT=35 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records
-- ----------------------------
INSERT INTO `student_` VALUES ('2', 'student2', '男', '12', '一年级', '二班');
INSERT INTO `student_` VALUES ('3', 'student3', '男', '13', '一年级', '二班');
INSERT INTO `student_` VALUES ('4', 'student4', '女', '14', '一年级', '二班');
INSERT INTO `student_` VALUES ('5', 'student5', '女', '15', '一年级', '二班');
INSERT INTO `student_` VALUES ('6', 'student6', '男', '16', '二年级', '一班');
INSERT INTO `student_` VALUES ('7', 'student7', '男', '17', '二年级', '一班');
INSERT INTO `student_` VALUES ('8', 'student8', '女', '18', '一年级', '一班');
INSERT INTO `student_` VALUES ('9', 'story1', '男', '19', '一年级', '二班');
INSERT INTO `student_` VALUES ('10', 'story2', '女', '11', '一年级', '一班');
INSERT INTO `student_` VALUES ('11', 'story3', '男', '12', '二年级', '一班');
INSERT INTO `student_` VALUES ('12', 'story4', '女', '13', '一年级', '二班');
INSERT INTO `student_` VALUES ('13', 'story5', '男', '14', '一年级', '一班');
INSERT INTO `student_` VALUES ('14', 'story6', '男', '15', '一年级', '一班');
INSERT INTO `student_` VALUES ('15', 'story7', '女', '16', '一年级', '二班');
INSERT INTO `student_` VALUES ('16', 'story8', '男', '17', '一年级', '二班');
INSERT INTO `student_` VALUES ('17', 'story9', '男', '18', '一年级', '二班');
INSERT INTO `student_` VALUES ('18', 'story8', '男', '15', '一年级', '二班');
INSERT INTO `student_` VALUES ('19', 'story8', '男', '16', '一年级', '二班');
实体类:
/**
* 学生信息类
*/
@Entity
@Table(name = "student_")
public class Student implements java.io.Serializable {
private static final long serialVersionUID = 2345820270183923971L;
private Integer id;//主键
private String name;//名字
private String sex;//性别
private Integer age;//年龄
private String grade;//年级
private String class_;//班级
public Student() {
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id_", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name_", length = 20)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "sex_", length = 20)
public String getSex() {
return this.sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Column(name = "age_")
public Integer getAge() {
return this.age;
}
public void setAge(Integer age) {
this.age = age;
}
@Column(name = "grade_", length = 60)
public String getGrade() {
return this.grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
@Column(name = "class_", length = 60)
public String getClass_() {
return this.class_;
}
public void setClass_(String class_) {
this.class_ = class_;
}
}
persistence.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_1_0.xsd" version="1.0">
<persistence-unit name="ssjPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>org.forever.xxxx.entities.Student</class>
<properties>
<property name="hibernate.connection.driver_class"
value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/paginationdb" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="root" />
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
spring配置文件:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 1.配置数据源 --> <context:annotation-config /> <context:component-scan base-package="org.forever.xxxx" /> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass"> <value>com.mysql.jdbc.Driver</value> </property> <property name="jdbcUrl"> <value>jdbc:mysql://localhost:3306/ssjdb</value> </property> <property name="user"> <value>root</value> </property> <property name="password"> <value>root</value> </property> </bean> <!-- 2.配置实体管理工厂 --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceUnitName" value="ssjPU" /> <property name="dataSource" ref="dataSource"/> </bean> <!-- 3.配置jpa模板工具 --> <bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate"> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <!-- 4.配置事务环境 --> <bean id="myTxManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <!-- 5.事务aop配置 --> <aop:config> <aop:pointcut id="bizMethod" expression="execution(* org.forever.xxxx.biz..*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="bizMethod" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="myTxManager"> <tx:attributes> <tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED" /> <tx:method name="find*" read-only="true" propagation="NOT_SUPPORTED" /> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> </beans>
测试类:
package org.forever.xxxx.test;
import java.util.List;
import org.forever.xxxx.entities.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.jpa.JpaTemplate;
public class BeanTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
JpaTemplate jpaTemplate = (JpaTemplate) context.getBean("jpaTemplate");
List<?> list = jpaTemplate.find("select student from Student student");
for (Object object : list) {
Student student = (Student)object;
System.out.println(student.getName());
}
}
}
上一篇: C++中一些常见的方法
下一篇: python中一些常见的数学操作