`
yulon
  • 浏览: 116953 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

跟我一起写大虾网(第1天)

阅读更多

一、先整合spring2.5+jpa1.0,使用扫描加注解的方式加快开发速度,SessionFactory创建所需要的時間较长,使用单例
模式.事务也交给spring管理.

 

1.1 、导入项目相关jar包,先集成spring2.5+jpa1.0,在src目录下建立META-INF子目录,在这个目录下建立一个名为persistence.xml,这个模板文件可以在hibernate-entitymanager-3.3.1.GA\test-resources下的子包找到。

<?xml version="1.0"?>
<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">
<!-- 配置一个持久化单元,名为mydaxia -->
  <persistence-unit name="mydaxia" transaction-type="RESOURCE_LOCAL">
  	<!-- 使用的是Hibernate提供的jpa支持 -->
  	<provider>org.hibernate.ejb.HibernatePersistence</provider>
	<properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
         <property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver"/>
         <property name="hibernate.connection.username" value="root"/>
         <property name="hibernate.connection.password" value="root"/>
         <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/mydaxia?useUnicode=true&amp;characterEncoding=UTF-8"/>
 
         <property name="hibernate.max_fetch_depth" value="3"/>
         <!-- 开发阶段建议用update -->
         <property name="hibernate.hbm2ddl.auto" value="update"/>
	     <property name="hibernate.jdbc.fetch_size" value="18"/>
	     <property name="hibernate.jdbc.batch_size" value="10"/>
	     <property name="hibernate.show_sql" value="false"/>
	     <property name="hibernate.format_sql" value="false"/>     
      </properties>
  </persistence-unit>
</persistence>

 

1.2、在src目录下建一个名为beans.xml文件,其实编译完后会放到WebRoot\WEB-INF\classes目录下.

<?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">
   <!--注册bean扫描器,启用注解能力 -->
   <context:component-scan base-package="cn.daxia"/>
   <!-- 注册实体管理器工厂 -->
   <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
 		<property name="persistenceUnitName" value="mydaxia"/>
   </bean>	
   <!-- 注册事务管理器 ,spring容器管理事务-->   
   <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
   </bean>
   <!-- 开启事务注解能力 -->
   <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

 

1.3、新建一个用户实体bean: cn.daxia.bean.user.User

@Entity 
public class User implements Serializable{

	private static final long serialVersionUID = -3476050387771606955L;
	private String username;
	
	private String password;
	
	public User() {}
	public User(String username, String password) {
		this.username = username;
		this.password = password;
	}
	
	@Id @Column(length=80)
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Column(length=50)
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}

 

 知识提示: @Entity 标识这个类为实体类,要与数据库表进行映射的,默认名与类的简单名相同,即User; @Id 是这个实体类的标识属性,与表对应的是主键. @Column 表示要与表字段映射的属性.

1.4、新建一个用户业务接口:cn.daxia.service.user.UserService,定义一个保存方法

public interface UserService {

	public void save(User user);
}

 

       新建一个用户业务接口实现类:cn.daxia.service.user.impl.UserServiceBean

@Service("userServiceBean") @Transactional
public class UserServiceBean implements UserService {

	@PersistenceContext(unitName="mydaxia") 
	protected EntityManager em;
	
	public void save(User user) {
		em.persist(user);
		System.out.println("执行了UserServiceBean.save()方法");
	}

}

 

 知识提示: @Service("userServiceBean") 标识这个类要交给spring管理,id为userServiceBean,默认也是这个id;

@Transactional 标识这个类的所有业务方法都要开启事务; @PersistenceContext(unitName="mydaxia") 从容器里面获取一个持久化单元,名为mydaxia.

 

1.5、新建一个测试类:UserServiceTest

 

public class UserServiceTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@Test
	public void testSpringContext(){
		ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
		UserService userService = (UserService)act.getBean("userServiceBean");
		
		userService.save(new User("yulon","123456"));
	}
}

 

知识提示: 测试库采用的是junit4,测试通过!

 

二、集成struts2,该项目使用的struts版本是2.1.8

 

2.1、新建一个Action: cn.daxia.bean.web.action.user.UserRegAction

@Controller("userRegAction")
public class UserRegAction extends ActionSupport{
	
	private static final long serialVersionUID = 1159518406043666180L;
	
	public String registerUI()throws Exception{
System.out.println("调用了UserRegAction.registerUI()方法");
		return null;
	}
	public String register() throws Exception {
System.out.println("调用了UserRegAction.register()方法");		
		return null;
	}
}

 

 知识提示: @Controller("userRegAction"),也是标识这个类交给spring管理,与之前@Service("userServiceBean") 不同的是,@Controller标识的是控制层的类. 但目前这两个注解没什么区别,以后的版本可能会有所不同!

 

1.2 配置struts.xml文件与struts-user.xml文件

    struts.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <constant name="struts.action.extension" value="do,action"/>
    <!-- 设置浏览器是否缓存静态内容 -->
    <constant name="struts.serve.static.browserCache" value="false"/>
    <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件 -->
    <constant name="struts.configuration.xml.reload" value="true"/>
    <!-- 打印详细的错误信息,开发阶段打开,便于寻找错误 -->
    <constant name="struts.devMode" value="true" />
     <!-- 设置默认的视图主题为simple -->
    <constant name="struts.ui.theme" value="simple" />
    <!-- 由spring创建action对象-->
    <constant name="struts.objectFactory" value="spring" /> 
    
    <!-- 文件包含 -->
    <include file="struts-user.xml"></include>
</struts>

 struts-user.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="forward" namespace="/" extends="struts-default">
	<action name="registerUI" class="userRegAction" method="registerUI">
		<result>/WEB-INF/forward/register.jsp</result>
	</action>
	<action name="register" class="userRegAction" method="register">
		<result>/WEB-INF/forward/register.jsp</result>
	</action>
</package> 
</struts>

 如有对struts2不了解的同学,可以看看struts2相关的文件,在此不在细写

 

配置web.xml文件

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 -->
	<context-param>
	   <param-name>contextConfigLocation</param-name>
	   <param-value>classpath:beans.xml</param-value>
	</context-param>
	<!-- 对Spring容器进行实例化,放在application范围 -->
	<listener>
	      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

   <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
   </filter-mapping> 

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

  测试:访问http://localhost:8081/mydaxia01/registerUI.action,控制台输出

调用了UserRegAction.registerUI()方法

 测试成功,到此三个框架的集成已结束!

3
1
分享到:
评论
3 楼 ahuango 2010-02-09  
我也是做web开发的,对楼主使用的技术比较感兴趣,现在还可以参加吗?
2 楼 ariestiger 2010-02-05  
你第0天还说要用struts1.x,我都快忘光了,还好你现在用的是struts2
1 楼 laojiang 2010-02-05  
struts2的用法比较复杂,最好也没有xml配置文件

相关推荐

Global site tag (gtag.js) - Google Analytics