spring的XML配置文件
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!-- 数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/jeasyweb" />
<property name="username" value="root" />
<property name="password" value="sa" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show.sql=true
</value>
</property>
<property name="mappingLocations">
<list>
<value>classpath:com/domain/User.hbm.xml</value>
</list>
</property>
</bean>
<!-- 事务管理 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true" />
<bean id="myTarget" class="com.dao.UserDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
实体JAVA类和XML文件:
package com.domain;
public class User {
private Integer id;
private String email;
private String username;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
XML映射配置文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.domain.User" table="User" >
<id name="id" type="integer">
<column name="ID" />
<generator class="increment" />
</id>
<property name="username" type="string">
<column name="USERNAME" length="16" />
</property>
<property name="email" type="string">
<column name="EMAIL" length="256" />
</property>
</class>
</hibernate-mapping>
DAO代码:
package com.dao;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;
import com.domain.User;
public class UserDao extends HibernateDaoSupport {
/**
* 更新某一字段的操作。并使用事务管理。
*/
@Transactional
public void updateField() {
// getHibernateTemplate().bulkUpdate("update com.domain.User set username = '管理员4' where id = 1 ");
// 有意制造异常
// getHibernateTemplate().bulkUpdate("update spring.User2 set username = '管理员4' where id = 1 ");
User u = new User();
u.setUsername("xx");
getHibernateTemplate().save(u);
User u1 = new User();
u1.setUsername("x111111111111111111111111111111111111111111111111111111111111111111111111111x");
getHibernateTemplate().save(u1);
}
}
测试代码:
package com.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.dao.UserDao;
public class TestMain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("NewFile.xml");
UserDao um = (UserDao) appContext.getBean("myTarget");
um.updateField();
}
}
日志配置:
# This is the configuring for logging displayed in the Application Server
log4j.rootCategory=DEBUG, stdout,logfile
#stdout configure
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern= %d %p [%c] - %m%n
#logfile configure
log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.File=../logs/jeasyweb.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern= %d %p [%c] - %m%n
# Control logging for other open source packages
# Changing the log level to DEBUG when debug
log4j.logger.org.springframework=DEBUG
log4j.logger.org.dozer=ERROR
log4j.logger.org.apache.commons=DEBUG
# Changing the log level to DEBUG will display SQL Hibernate generated
log4j.logger.org.hibernate=DEBUG
log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.cache=DEBUG
log4j.logger.net.sf.ehcache=DEBUG
log4j.logger.org.apache.commons.validator.ValidatorResources=ERROR