最近因为要学习dwr,我呢有个习惯,就是在学习即将结束的时候写点笔记,以便将来用的时候快速查阅!现将笔记整理如下:
我们做关联表的增加、删除、修改。
班级表(myclass):id、name
学生表(student):id、name、age、sex、birthday、class_id
一、与Hbiernate、Spring框架集成。
二、加入dwr插件。
1、新建工程demo
2、Add a new source folder。名字是conf。里面主要存放一下Hibernate和Sping配置文件。
3、在conf下新建applicationContext.xml、demo-service.xml、jdbc.properties、log4j.properties。四个文件。
其作用分别如下:
3.1-->applicationContext.xml文件,既是Hbiernate的配置文件也是Spring的配置文件。
<?xml version="1.0" encoding="utf-8"?> <beans default-autowire="byType" 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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>${jdbc.driverClassName}</value> </property> <property name="url"> <value>${jdbc.url}</value> </property> <property name="username"> <value>${jdbc.username}</value> </property> <property name="password"> <value>${jdbc.password}</value> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"> <ref bean="dataSource" /> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLInnoDBDialect </prop><!--<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>--> <prop key="hibernate.cache.use_query_cache">false</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> <property name="mappingDirectoryLocations"> <list> <value> classpath:/com/inspur/mydemo/domain/hbm/mysql </value> </list> </property> </bean> </beans>
(注意事项:1、beans里的标签库不能少。2、配置数据库表和java中的bean对象配置文件hbm的路径不能错)
3.2-->demo-service.xml,这个主要是Sping的配置文件配置。
<?xml version="1.0" encoding="utf-8"?> <beans default-autowire="byType" 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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <import resource="applicationContext.xml" /> <!-- service --> <bean id="classService" class="com.inspur.mydemo.service.impl.ClassServiceImpl" autowire="byType"> </bean> <bean id="StudentService" class="com.inspur.mydemo.service.impl.StudentServiceImpl" autowire="byType"> </bean> <!-- dwr --> <bean id="mydemoDwr" class="com.inspur.mydemo.dwr.MydemoDwr" autowire="byType"> </bean> </beans>
3.3-->jdbc.properties,配置的是数据库链接。
# mysql----------------- jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=GBK jdbc.username=root jdbc.password=root
3.4-->log4j.properties,配置的是日志系统。
### set log levels - for more verbose logging change 'info' to 'debug' ## log4j.rootLogger=info,stdout,A2 ### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %p %c<%M>:[%L] - %m%n #setting for appender A2 log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender log4j.appender.A2.File=d:/temp/logs/advert.log log4j.appender.A2.Append=true log4j.appender.A2.DatePattern='.'yyyy-MM-dd log4j.appender.A2.layout=org.apache.log4j.PatternLayout log4j.appender.A2.layout.ConversionPattern=%d %-5p - %m%n log4j.logger.com.inspur.sms=debug log4j.logger.org.springframework=info log4j.logger.org.hibernate=warn log4j.logger.org.directwebremoting=warn log4j.logger.com.co=debug