dwr spring hibernate的简单例子

hibernate负责的部分:

H.a,数据源的配置

H.b,表到类的映射,表关系到类关系的映射

H.c,工具类

 

H.a~~~~~~~~~数据源配置

<session-factory>
	<property name="dialect">
		org.hibernate.dialect.MySQLDialect
	</property>
	<property name="connection.url">
		jdbc:mysql://localhost:3306/hibernateorm
	</property>
	<property name="connection.username">root</property>
	<property name="connection.password">yang</property>
	<property name="connection.driver_class">
		com.mysql.jdbc.Driver
	</property>
	<property name="myeclipse.connection.profile">
		com.mysql.jdbc.Driver
	</property>

	<!-- 自动更新表结构 -->
	<property name="hibernate.hbm2ddl.auto">update</property>

	<!-- 其它特性 -->
	<property name="format_sql">true</property>
	<property name="show_sql">true</property>


	<!-- Connection Pool -->
	<property name="c3p0.max_size">20</property>
	<property name="c3p0.min_size">5</property>
	<property name="c3p0.timeout">5000</property>
	<property name="c3p0.max_statements">0</property>
	<property name="c3p0.acquire_increment">5</property>
	<property name="c3p0.idle_test_period">3000</property>
	<mapping resource="com/yang/pojo/Merchandise.hbm.xml" />
	<mapping resource="com/yang/pojo/UserTrue.hbm.xml" />
	<mapping resource="com/yang/pojo/User.hbm.xml" />
	<mapping resource="com/yang/pojo/Auction.hbm.xml" />

</session-factory>

 H.b~~~表到类的映射,表关系到类关系的映射(表user和表usertrue一对一关系)

    User.hbm.xml

	<class name="com.yang.pojo.User" table="users"
		catalog="hibernateorm">
		<id name="userId" type="java.lang.Integer">
			<column name="userId" />
			<generator class="com.yang.util.NativeHiLoGenerator">
				<param name="table">hi_value</param>
				<param name="column">next_value</param>
				<param name="max_lo">1</param>
			</generator>
		</id>

		<!-- one to one about Usertrue foreignkey association -->
		<one-to-one name="userTrue" class="com.yang.pojo.UserTrue" cascade="save-update,delete"
			property-ref="user">
		</one-to-one>

		<property name="userName" type="java.lang.String">
			<column name="userName" length="20" not-null="true" />
		</property>
		<property name="password" type="java.lang.String">
			<column name="password" length="50" not-null="true" />
		</property>
		<property name="sex" type="java.lang.String">
			<column name="sex" length="2" />
		</property>
		<property name="birthday" type="java.util.Date">
			<column name="birthday" length="0" />
		</property>
		<property name="email" type="java.lang.String">
			<column name="email" length="50" not-null="true" />
		</property>
		<property name="passwordQuestion" type="java.lang.String">
			<column name="passwordQuestion" length="100"
				not-null="true" />
		</property>
		<property name="passwordAnswer" type="java.lang.String">
			<column name="passwordAnswer" length="100" not-null="true" />
		</property>
		<property name="creditStanding" type="java.lang.Integer">
			<column name="creditStanding" />
		</property>
	</class>

  UserTrue.hbm.xml

<class name="com.yang.pojo.UserTrue" table="usertrue"
		catalog="hibernateorm" lazy="true">
		<id name="userTrueId" type="java.lang.Integer">
			<column name="userTrueId" />
			<generator class="com.yang.util.NativeHiLoGenerator">
				<param name="table">hi_value</param>
				<param name="column">next_value</param>
				<param name="max_lo">1</param>
			</generator>
		</id>
		<!-- one to one about User using sharine the foreign key in User table -->
		<many-to-one name="user" class="com.yang.pojo.User"
			unique="true" column="userId">
		</many-to-one>

		<property name="trueName" type="java.lang.String">
			<column name="trueName" length="20" not-null="true" />
		</property>
		<property name="identityCard" type="java.lang.String">
			<column name="identityCard" length="18" not-null="true" />
		</property>
		<property name="cellPhone" type="java.lang.String">
			<column name="cellphone" length="20" not-null="true" />
		</property>

		<property name="homePhone" type="java.lang.String">
			<column name="homephone" length="20" />
		</property>
		<property name="address" type="java.lang.String">
			<column name="address" length="50" />
		</property>
	</class>

 H.c~~~主码生成器

private static final Log log = LogFactory.getLog(NativeHiLoGenerator.class);

	private IdentifierGenerator proxiedIdGen;

	public NativeHiLoGenerator() {
		super();
	}

	// 生成一个主键值
	public Serializable generate(SessionImplementor session, Object object)
			throws HibernateException {
		return proxiedIdGen.generate(session, object);
	}

	//
	public Object generatorKey() {
		if (proxiedIdGen instanceof PersistentIdentifierGenerator) {
			return ((PersistentIdentifierGenerator) proxiedIdGen)
					.generatorKey();
		} else {
			return this;
		}
	}

	//
	public String[] sqlCreateStrings(Dialect dialect) throws HibernateException {
		if (proxiedIdGen instanceof PersistentIdentifierGenerator) {
			return ((PersistentIdentifierGenerator) proxiedIdGen)
					.sqlCreateStrings(dialect);
		} else {
			return new String[] {};
		}
	}

	public String[] sqlDropStrings(Dialect dialect) throws HibernateException {
		if (proxiedIdGen instanceof PersistentIdentifierGenerator) {
			return ((PersistentIdentifierGenerator) proxiedIdGen)
					.sqlDropStrings(dialect);
		} else {
			return null;
		}
	}

	// 配置生成器
	public void configure(Type type, Properties params, Dialect dialect)
			throws MappingException {
		Class generatorClass = null;
		// 使用的数据库支持sequence,则使用seqhilo算法
		if (dialect.supportsSequences()) {
			log.debug("Support for SEQUENCE detected - delegate to SequenceHiLoGenerator");
			generatorClass = SequenceHiLoGenerator.class;
		}
		// otherwise use the TableHiLoGenerator
		else {
			log.debug("No support for SEQUENCE detected - delegate to TableHiLoGenerator");
			// 否则使用hilo算法
			generatorClass = TableHiLoGenerator.class;

			// -- max_lo -> max_lo
			// 设置max_lo参数值
			if (!SequenceHiLoGenerator.MAX_LO.equals(TableHiLoGenerator.MAX_LO)) {
				String value = params.getProperty(SequenceHiLoGenerator.MAX_LO);

				if (value != null) {
					params.setProperty(TableHiLoGenerator.MAX_LO, value);
				}
			}

			// -- sequence name -> table name
			// 设置参数值(sequenc或table参数)
			if (!SequenceHiLoGenerator.SEQUENCE
					.equals(TableHiLoGenerator.TABLE)) {
				String value = params
						.getProperty(SequenceHiLoGenerator.SEQUENCE);

				if (value != null) {
					params.setProperty(TableHiLoGenerator.TABLE, value);
				}
			}
		}
		IdentifierGenerator idgen = null;
		try {
			idgen = (IdentifierGenerator) generatorClass.newInstance();
		} catch (Exception e) {
			throw new MappingException("could not instantiate id generator", e);
		}

		// 如果主键生成器支持配置,则配置它
		if (idgen instanceof Configurable) {
			((Configurable) idgen).configure(type, params, dialect);
		}

		// ok
		this.proxiedIdGen = idgen;
	}

 

Spring负责部分:

S.a,整合hibernate负责的数据源

S.b,管理hibernate的session(各种事务)

S.c,把各对象的依赖关系注入式配置出来

 

S.a~~~~~~~~整合hibernate的数据源

	<!-- ~~~~~~~~~~~~~DataSource about hibernate.cfg.xml~~~~~~~~~~~~ -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation"
			value="classpath:hibernate.cfg.xml">
		</property>
	</bean>

S. b~~~~~~~session管理(声明式事务)

<!-- transactionManager about the hibernate session -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref local="sessionFactory" />
		</property>
	</bean>

	<!-- transactionInterceptor about transactionManaer and interface-->
	<!-- 事务拦截类 -->
	<bean id="transactionInterceptor"
		class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<property name="transactionManager">
			<ref bean="transactionManager" />
		</property>
		<property name="transactionAttributes">
			<props>
				<prop key="delete*">PROPAGATION_REQUIRED</prop>
				<prop key="save*">PROPAGATION_REQUIRED</prop>
				<prop key="isExist*">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
		<property name="transactionAttributeSource">
			<value>
				<!--
					加入service接口事务
				-->
				com.yang.serviceFace.UserServiceI.save*=PROPAGATION_REQUIRED
				com.yang.serviceFace.UserServiceI.isExist*=PROPAGATION_REQUIRED
				com.yang.serviceFace.UserServiceI.delete*=PROPAGATION_REQUIRED
			</value>
		</property>
	</bean>

 

S.c~~~~~~~各依赖关系注入配置

	<!-- userDao about sessionFactory -->
	<bean id="userDao" class="com.yang.dao.UserDao">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>

	<!-- userTarget about UserService and userDAO -->
	<bean id="userServiceTarget" class="com.yang.service.UserService">
		<property name="userDao">
			<ref bean="userDao" />
		</property>
	</bean>

	<!-- userService about userTarget and transactionInterceptor in AOP -->
	<bean id="userService"
		class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="proxyInterfaces">
			<value>com.yang.serviceFace.UserServiceI</value>
		</property>
		<property name="interceptorNames">
			<list>
				<idref local="transactionInterceptor" />
				<idref local="userServiceTarget" />
			</list>
		</property>
	</bean>
	</bean>

 DWR负责部分:

D.a,把在spring里的配出的Service以javaScript函数形式给jsp页面调用

 

D.a~~~~~dwr.xml

<dwr>
	<allow>
		<!-- 第一个测试dwr类 -->
		<create creator="new" javascript="service">
			<param name="class" value="com.yang.service.HelloWorld" />
		</create>
		<create creator="spring" javascript="userService">
		<param name="beanName" value="userService" />
		</create>
	</allow>
</dwr>

 

Web服务器负责的部分:

W.a,xml服务器加载时惟一读取

 

W.a~~~~~~~web.xml

 

<!--~~~~~~~~~~~  初始化Spring上下 ~~~~~~~~~~~~~~~-->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml</param-value>
	</context-param>

	<!-- 用于分布式服务器 -->
	<distributable />

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

	<!-- dwr Servlet -->
	<servlet>
		<servlet-name>dwr-invoker</servlet-name>
		<servlet-class>
			org.directwebremoting.servlet.DwrServlet
		</servlet-class>
		<init-param>
			<param-name>debug</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>crossDomainSessionSecurity</param-name>
			<param-value>false</param-value>
		</init-param>
	</servlet>

	<!-- url association dwr-invoker -->
	<servlet-mapping>
		<servlet-name>dwr-invoker</servlet-name>
		<url-pattern>/dwr/*</url-pattern>
	</servlet-mapping>

 

Jsp页面负责的部分:

J.a,引入dwr服务,展示内容,

 

J.a~~~~~~~jsp页面

		<script type="text/javascript" src="dwr/util.js"></script>
		<script type="text/javascript" src="dwr/engine.js"></script>
		<script type="text/javascript" src="dwr/interface/service.js"></script>
		<script type="text/javascript" src="dwr/interface/userService.js"></script>
		<script type="text/javascript">
			function firstDwr(){
				var user = $('user').value;
				service.sayHello(user,callBackHello);
			}
			function callBackHello(data){
				window.alert(data);
			}
			function hello(){
				var user = $('user').value;
				service.sayHello(user,callBack);
			}
			function callBack(msg){
				DWRUtil.setValue("showResult",msg);
			}
			
			
			
			function isExitUser(){
				var name = $('user').value;
				userService.isExistByName(name,function(data){
					window.alert(data);	
				});
								
				
			}
        </script>

	</head>

	<body>
		<input id="user" type="text" />
		<input type="button" value="检测是否存在此用户" οnclick="isExitUser()" />
		<div id="showResult"></div>
	</body>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值