一个简单的项目(click+spring+eclipselink jpa)

一、框架介绍

1.Apache Click 是一个先进的Java企业版的Web应用程序框架,提供一个基于浏览器的富客户端编程模型。

主要的特点包括:

  • 易学
  • 面向组件和页面的设计
  • 基于事件的编程模型
  • 性能非常好
  • 自动的表单生成以及客户端、服务器端数据验证
  • 支持 Velocity、JSP、FreeMarker 等模板引擎
  • 采用 Apache 开源协议
  • 支持 JDK 1.4 以及以后的版本

2.eclipselink jpa是一个类似于hibernate的持久化框架,通过注解的方式实现简单映射。

3.spring就不多介绍,数据库使用mysql,项目管理用maven

 

二、编写框架对应的配置文件

1.通过maven生成一个工程,如工程名为click,然后为工程新建两个maven module,一个为web工程click-web,另一个为普通java工程click-core。

2.为每个工程的pom文件中加入对应的依赖包:

click的pom:

<artifactId>click</artifactId>
    <groupId>com.xxx</groupId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
	<modules>
		<module>click-core</module>
		<module>click-web</module>
	</modules>
	<dependencies>
                  <!--加入一些公用包-->
		<dependency>
		  <groupId>junit</groupId>
		  <artifactId>junit</artifactId>
		  <version>4.11</version>
	</dependency>
	<dependency>
	  <groupId>commons-lang</groupId>
	  <artifactId>commons-lang</artifactId>
	  <version>2.6</version>
	</dependency>
	</dependencies>

 3.click-web的pom:(加入click框架所需包)

<parent>
    <artifactId>click</artifactId>
    <groupId>com.xxx</groupId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>com.xxx</groupId>
  <artifactId>click-web</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>click-web Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
  	<javax.servlet.version>2.5</javax.servlet.version>
  	<click.version>2.3.0</click.version>
  </properties>
  <dependencies>
  	<dependency>
  		<groupId>com.yaolinnan</groupId>
  		<artifactId>click-core</artifactId>
  		<version>0.0.1-SNAPSHOT</version>
  	</dependency>
    <dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<scope>provided</scope>
			<version>${javax.servlet.version}</version>
		</dependency>
		
	<dependency>
	    <groupId>org.apache.click</groupId>
	    <artifactId>click-nodeps</artifactId>
	    <version>${click.version}</version>
	    <scope>compile</scope>
	</dependency>

	<dependency>
	    <groupId>org.apache.click</groupId>
	    <artifactId>click-extras</artifactId>
	    <version>${click.version}</version>
	    <scope>compile</scope>
	</dependency>
	
  </dependencies>
  <build>
    <finalName>click-web</finalName>
  </build>

 4.click-core的pom:(加入spring和eclipselink包)

<parent>
    <artifactId>click</artifactId>
    <groupId>com.xxx</groupId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>com.xxx</groupId>
  <artifactId>click-core</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>click-core</name>
  <packaging>jar</packaging>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <jpa.version>2.0.0</jpa.version>
    <spring.version>3.0.6.RELEASE</spring.version>
  </properties>
  <dependencies>
   <dependency>  
        <groupId>org.eclipse.persistence</groupId>  
        <artifactId>javax.persistence</artifactId>  
        <version>${jpa.version}</version>  
    </dependency>  
    <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.4.1</version>
        </dependency>

        <dependency>
			<groupId>commons-pool</groupId>
			<artifactId>commons-pool</artifactId>
			<version>1.5.4</version>
		</dependency>
		<dependency>
			<groupId>commons-beanutils</groupId>
			<artifactId>commons-beanutils-core</artifactId>
			<version>1.8.3</version>
		</dependency>
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>2.6</version>
		</dependency>
      <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>jsr250-api</artifactId>
            <version>1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.9</version>
		</dependency>
		<dependency>  
	       <groupId> org.aspectj</groupId >  
	       <artifactId> aspectjweaver</artifactId >  
	      <version> 1.6.11</version >  
		</dependency>  
		<dependency>
	  <groupId>commons-dbcp</groupId>
	  <artifactId>commons-dbcp</artifactId>
	  <version>1.4</version>
	</dependency>
		
  </dependencies>

 5.在click-web工程下的src/main/resources目录下生成click.xml,编写click框架的配置信息:

<click-app charset="UTF-8">

  <pages package="com.xxx.pages" automapping="true"/>

  <mode value="debug"/>

</click-app>

 web.xml:

<web-app>
  <display-name>click-web</display-name>
  <servlet>
    <servlet-name>ClickServlet</servlet-name>
    <servlet-class>org.apache.click.extras.spring.SpringClickServlet</servlet-class>
    <init-param>
			<param-name>inject-page-beans</param-name>
			<param-value>true</param-value>
	</init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>ClickServlet</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
  	<welcome-file>hello.htm</welcome-file>
  </welcome-file-list>
  
  
  <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>			
			classpath*:spring/*.xml
		</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
</web-app>

 

6.在click-core的src/main/resources目录下编写eclipselink框架的配置信息--persistence.xml

<persistence version="2.0" 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">
  <persistence-unit name="springjpaPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <!-- 需要持久化的实体 -->
<class>com.xxx.core.model.User</class>
    <properties>
        <property name="eclipselink.ddl-generation.output-mode" value="both" />
    </properties>

  </persistence-unit>
  
</persistence>

 在这个目录下新建spring/springcontext.xml,编写spring框架的配置信息

<context:annotation-config/>
    <context:component-scan base-package="com.xxx.core"/>
    <tx:annotation-driven transaction-manager="transactionManager"/>
	
	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>  
    
	  <!-- Spring transaction manager -->
	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" p:entityManagerFactory-ref="entityManagerFactory" />
	
	<!-- JPA shared entity manager -->
	<bean id="entityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean" p:entityManagerFactory-ref="entityManagerFactory" />

  
  
	<bean id="entityManagerFactory"
       class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"  >
        <property name="loadTimeWeaver">
            <bean
             class="com.xxx.core.util.NewInstrumentationLoadTimeWeaver"/>
		
        </property>
        <property name="dataSource" ref="mydataSource" />
        <property name="JpaVendorAdapter" ref="jpaAdapter"/>
        <property name="persistenceUnitName" value="springjpaPU"></property>
        <property name="persistenceXmlLocation" value="classpath*:persistence.xml"/>  
        
    </bean>

    <bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
        <property name="databasePlatform" value="org.eclipse.persistence.platform.database.MySQLPlatform" />
        <property name="showSql" value="true" />
    </bean>  
  <!--数据源配置-->
    <bean id="mydataSource" 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="maxIdle" value="10"/>  
        <property name="maxActive" value="100"/>  
        <property name="maxWait" value="10000"/>  
        <property name="testOnBorrow" value="true"/>  
        <property name="testWhileIdle" value="true"/>  
        <property name="timeBetweenEvictionRunsMillis" value="1200000"/>  
        <property name="minEvictableIdleTimeMillis" value="1800000"/>  
        <property name="numTestsPerEvictionRun" value="5"/>  
        <property name="defaultAutoCommit" value="true"/>  
    </bean>  
  
    <context:property-placeholder location="classpath*:spring/config.properties"/>  
	<!--DAO和Service注入-->
	<bean id="userDAO" class="com.yaolinnan.core.dao.UserDAOImpl">
	<property name="entityManager" ref="entityManager"></property>
	</bean>
	<bean id="userService" class="com.yaolinnan.core.service.UserServiceImpl"></bean>

 

新建config.properties,编写数据库配置信息

# MySql
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://xxx
jdbc.username=xxx
jdbc.password=xxx
jdbc.initialSize=1
jdbc.maxIdle=2
jdbc.minIdle=-1
jdbc.maxWait=10000
jdbc.maxActive=10
jdbc.testOnBorrow=true
jdbc.testOnReturn=true
jdbc.testWhileIdle=true
jdbc.logAbandoned=true
jdbc.removeAbandoned=true
jdbc.removeAbandonedTimeout=50
jdbc.validationQuery=select count(*) from dual
jdbc.timeBetweenEvictionRunsMillis=300000
jdbc.minEvictableIdleTimeMillis=300000

 三、程序编写

1.click-core编写实体类,对应数据库中的表

@Entity
@Table(name="xxx")
public class User{
	
	@Id
	private int id;
	
	private String userName;
	
	private String loginName;
	
	private String password;
	
	private String email;
	
	@Temporal(TemporalType.DATE)
	private Date createDate;
         
                public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
.........加入属性对应的set和get方法
}

 2.编写DAO和Service(简单写下)

public class UserDAOImpl implements UserDAO{
	
	
	private EntityManager entityManager;

	@Override
	public List<User> getUsers() {
		Query q=entityManager.createQuery("SELECT t FROM User t ORDER BY t.id ASC");
		return q.getResultList();
	}

	@Override
	public User getUserbyId(int id) {
		return entityManager.find(User.class, id);
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}
	
	@PersistenceContext
	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	@Override
	public void saveUser(User user) {
		entityManager.persist(user);
	}

}

 

3.编写一个继承自InstrumentationLoadTimeWeaver的类,这个类避免了工程启动时去jvm配置-javaagent:spring-agent.jar这个步骤,对应springcontext.xml中<property name="loadTimeWeaver">属性。

public class NewInstrumentationLoadTimeWeaver extends InstrumentationLoadTimeWeaver{

	 @Override
	    public void addTransformer(ClassFileTransformer transformer) {
	       try {
	           super.addTransformer(transformer);
	       } catch (Exception e) {}
	    }
}

 4.在click-web编写page和页面来测试。

public class Hello extends Page{

	
	private static final long serialVersionUID = 1L;
	
	private UserService userService;
	
	private Table table=new Table("userTable");
	
	private ActionLink delete=new ActionLink("delete",this,"onDelete");

	@Override
	public void onInit() {
		this.addControl(table);
		this.addControl(delete);
		table.setClass(Table.CLASS_SIMPLE);//设置表格显示的样式
        table.setPageSize(10);//设置一页显示的数量
        table.setShowBanner(true);
        table.setSortable(true);//设置表格排序
        table.addColumn(new Column("userName","名称"));//第一列显示的标题和对应model的值
        Column column = new Column("email","邮箱");
        column.setAutolink(true);//设置自动关联url或者email地址
        column.setTitleProperty("loginName");//设置指向当前列显示的值
        
        table.addColumn(column);
        
        delete.setImageSrc("images/u86.png");//设置ActionLink显示的图片
        delete.setTitle("删除");//设置ActionLink显示的名称
        delete.setAttribute("onclick", "return window.confirm('确定要删除当前用户吗?');");//设置ActionLink的css对应的值
        
        column = new Column("操作");
        column.setTextAlign("center");
        AbstractLink[] links = new AbstractLink[] { delete};//为当前列加上ActionLink(可多个)
        column.setDecorator(new LinkDecorator(table, links, "loginName"));//添加的ActionLink操作对应的值
        column.setSortable(false);
        table.addColumn(column);
        List<User> rowList=userService.getUsers();
       
        table.setRowList(rowList);//为表格添加数据
        this.addModel("list", rowList);
	}
	
	public Boolean onDelete(){
		String name=delete.getValue();//得到用户点击删除操作时所对应的值
		this.addModel("name", name);
		return true;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	

}

 

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>click table</title>
</head>
<body>
	 $userTable
#if($name)
	<p>delete user name:$name</p>
#end

#foreach($user in $list)

	<div>$user.userName</div>
#end
</body>
</html>

 

四、启动tomcat,并访问。

你可能感兴趣的:(eclipse)