linux下的Java开发环境折腾完了。在实际的项目开发过程中对JPA一直是情有独钟,今下午在家在linux环境下折腾了下[color=green][size=x-large]Spring3+Hibernate3.6(Jpa)[/size][/color]的整合,做了一个小DEMO。其实跟在XP环境下开发是大同小异,冇的什么神奇的地方。
[size=x-large][color=red]1.环境的搭建[/color][/size]
直接上图吧。有图有真相,来的更是在
首先是简单项目图结构,注意项目包的命名规范,清晰简单明了易懂。
[img]http://dl.iteye.com/upload/attachment/535572/f7fde634-ea41-3ba8-ba3a-d17586118345.png[/img]
Sping+Jpa 所需要的jar,这是一般开发者比较好奇的地方。刚学框架都一大堆的jar都直接往工程中放,也不知道一个所以然,老师说能用让工程能跑起来就行。对初学着当然是可行,但搞明白,心里更踏实 :o :o :o
在写这个DEMO的时候都是从官网上来下的jar,一个个来整的做到jar的最简化。这里有不再折腾来,不是很清楚就度娘谷姐了。其实我是的。网络资源共享,浩瀚无边。
[img]http://dl.iteye.com/upload/attachment/535575/949a752e-015e-3dbb-8cf3-6fbb393c8011.png[/img]
[img]http://dl.iteye.com/upload/attachment/535577/f75f21ef-a13c-3c62-a43d-07f6f98a1f5b.png[/img]
[size=x-large][color=red]2.数据库实体类[/color][/size]
所谓Spring+JPA整合,数据库实体当然是使用JPA的注解来玩来。
[size=x-large][color=green]3.配置文件[/color][/size]
[size=large]beans.xml[/size]
[size=large]web.xml[/size]
[size=x-large][color=red]4.测试文件[/color]
[/size]
直接上图:
[img]http://dl.iteye.com/upload/attachment/535579/10598974-741b-33bc-abea-28cf01f94dec.png[/img]
[color=darkred][size=large]5.注意
在linux中终端是分大小写的。所以在建数据库的时候注意大小写的区分。因为也是才刚开始来玩linux,不是很在行,也是在测试的时候出错来才有所了解与收获。
DEMO有想要的可以来call。
[/size][/color]
[size=x-large][color=red]1.环境的搭建[/color][/size]
直接上图吧。有图有真相,来的更是在
首先是简单项目图结构,注意项目包的命名规范,清晰简单明了易懂。
[img]http://dl.iteye.com/upload/attachment/535572/f7fde634-ea41-3ba8-ba3a-d17586118345.png[/img]
Sping+Jpa 所需要的jar,这是一般开发者比较好奇的地方。刚学框架都一大堆的jar都直接往工程中放,也不知道一个所以然,老师说能用让工程能跑起来就行。对初学着当然是可行,但搞明白,心里更踏实 :o :o :o
在写这个DEMO的时候都是从官网上来下的jar,一个个来整的做到jar的最简化。这里有不再折腾来,不是很清楚就度娘谷姐了。其实我是的。网络资源共享,浩瀚无边。
[img]http://dl.iteye.com/upload/attachment/535575/949a752e-015e-3dbb-8cf3-6fbb393c8011.png[/img]
[img]http://dl.iteye.com/upload/attachment/535577/f75f21ef-a13c-3c62-a43d-07f6f98a1f5b.png[/img]
[size=x-large][color=red]2.数据库实体类[/color][/size]
所谓Spring+JPA整合,数据库实体当然是使用JPA的注解来玩来。
package com.tq.bean.user;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@NamedQueries( {
// 查询所以数据项
@NamedQuery(name = "Buyer.list", query = "SELECT b FROM Buyer b"),
// 查询数据项总数
@NamedQuery(name = "Buyer.count", query = "SELECT count(b.id) FROM Buyer b"),
// 查询此数据项是否存在
@NamedQuery(name = "Buyer.exists", query = "SELECT count(b.id) FROM Buyer b WHERE b.username = :username") })
public class Buyer implements Serializable {
private static final long serialVersionUID = 8394979715028899027L;
public static final String QUERY_LIST = "Buyer.list";
public static final String QUERY_COUNT = "Buyer.count";
public static final String QUERY_EXISTS = "Buyer.exists";
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
/** 用户名 **/
@Column(length = 18)
private String username;// 只允许字母/数字/下划线
/** 密码 **/
@Column(length = 32, nullable = false)
private String password;
/** 真实姓名 **/
@Column(length = 8)
private String realname;
/** 邮箱 **/
@Column(length = 50, nullable = false)
private String email;
/** 是否启动 **/
@Column(nullable = false)
private Boolean visible = true;
/** 注册时间 **/
@Temporal(TemporalType.TIMESTAMP)
@Column(nullable = false)
private Date regTime = new Date();
public Buyer() {
}
public Buyer(String username) {
this.username = username;
}
public Buyer(String username, String password) {
this.username = username;
this.password = password;
}
public Buyer(String username, String password, String email) {
this.username = username;
this.password = password;
this.email = email;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getRegTime() {
return regTime;
}
public void setRegTime(Date regTime) {
this.regTime = regTime;
}
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 String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void setVisible(Boolean visible) {
this.visible = visible;
}
}
[size=x-large][color=green]3.配置文件[/color][/size]
[size=large]beans.xml[/size]
<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.tq" />
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="100" />
<property name="maxWait" value="1000" />
<property name="poolPreparedStatements" value="true" />
<property name="defaultAutoCommit" value="true" />
</bean>
<!-- 类工厂由spring管理 -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="persistenceUnitName" value="springJpa" />
<!-- 注入数据源bean到实体管理工厂bean -->
<property name="dataSource" ref="dataSource" />
<property name="loadTimeWeaver">
<!-- 运行时植入 -->
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
</bean>
<!-- 事务由spring管理 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory">
<ref bean="entityManagerFactory" />
</property>
</bean>
<!-- 事务声明方式是注解 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 自动装载EntityManager -->
<context:annotation-config />
</beans>
[size=large]web.xml[/size]
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>springJpa</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- log4j 必须要在最前面 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webApp.root</param-value>
</context-param>
<!-- Spring初始化 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- <param-value> /WEB-INF/classes/**/applicationContext*.xml </param-value> -->
<param-value>/WEB-INF/beans.xml</param-value>
</context-param>
<!-- Spring的监听 对Spring容器进行实例化 ,并把實例化存放在application的屬性中 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--过滤器 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
<init-param>
<param-name>entityManagerFactoryBeanName</param-name>
<param-value>entityManagerFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
[size=x-large][color=red]4.测试文件[/color]
[/size]
/**
* @Title: BuyerTest.java
* @Package: cn.itcast.junit.test
* @Description: TODO(用一句话描述该文件做什么)
* @author: 何枫
* @date: 2011-8-11 下午07:57:31
* @version: V1.0
*/
package com.tq.junit.test;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tq.bean.user.Buyer;
import com.tq.service.buyer.BuyerService;
/**
* @Title: BuyerTest.java
* @Package: cn.itcast.junit.test
* @Description: TODO(用一句话描述该文件做什么)
* @author: 何枫
* @date: 2011-8-11 下午07:57:31
* @version: V1.0
*/
public class BuyerTest {
public BuyerTest() {
}
@BeforeClass
public static void setUpClass() throws Exception {
}
@AfterClass
public static void tearDownClass() throws Exception {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
@Test
public void save() {
System.out.println("测试Spring3.05+JPA(Hibernate3.6)环境");
ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
BuyerService buyerService = (BuyerService) cxt.getBean("buyerServiceBean");
for (int i = 0; i < 4; i++) {
buyerService.save(new Buyer("ASAPX5" + i, "1987",
"xiangyunzkf.net163@.com"));
}
}
}
直接上图:
[img]http://dl.iteye.com/upload/attachment/535579/10598974-741b-33bc-abea-28cf01f94dec.png[/img]
[color=darkred][size=large]5.注意
在linux中终端是分大小写的。所以在建数据库的时候注意大小写的区分。因为也是才刚开始来玩linux,不是很在行,也是在测试的时候出错来才有所了解与收获。
DEMO有想要的可以来call。
[/size][/color]