加载的jar有:
log4j-1.2.16.jar
c3p0-0.9.1.2.jar
mysql-connector-java-5.1.14-bin.jar
org.springfaramework.asm-3.0.5.release.jar
org.springfaramework.beans-3.0.5.release.jar
org.springfaramework.context-3.0.5.release.jar
org.springfaramework.core-3.0.5.release.jar
org.springfaramework.expression-3.0.5.release.jar
org.springfaramework.aop-3.0.5.release.jar
org.springfaramework.aspects-3.0.5.release.jar
org.springfaramework.jdbc-3.0.5.release.jar
org.springfaramework.orm-3.0.5.release.jar
org.springfaramework.transaction-3.0.5.release.jar
commons-logging-1.1.1.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
hibernate3.jar
hibernate-jpa-2.0-api-1.0.1.final.jar
javassist-3.9.0.GA.jar
jta-1.1.jar
slf4j-api-1.5.8.jar
slf4j-log4j12-1.6.1.jar
aopalliance.jar
aspectjweaver-1.5.3.jar
文件结构图如下:
代码演示如下:
MainOrder.java
package org.baicai.spring.entity;
import java.io.Serializable;
public class MainOrder implements Serializable {
private static final long serialVersionUID = 6934677354492244854L;
private java.lang.Integer orderId;
private java.lang.String customer;
private java.lang.String description;
public MainOrder() {
}
public MainOrder(java.lang.Integer orderId,java.lang.String customer,java.lang.String description) {
this.orderId = orderId;
this.customer = customer;
this.description = description;
}
public void setOrderId (java.lang.Integer orderId) {
this.orderId = orderId;
}
public java.lang.Integer getOrderId() {
return orderId;
}
public void setCustomer (java.lang.String customer) {
this.customer = customer;
}
public java.lang.String getCustomer() {
return customer;
}
public void setDescription (java.lang.String description) {
this.description = description;
}
public java.lang.String getDescription() {
return description;
}
@Override
public String toString() {
return String.format("orderId:%s,customer:%s,description:%s",orderId,customer,description);
}
}
SubOrder.java
package org.baicai.spring.entity;
import java.io.Serializable;
public class SubOrder implements Serializable {
private static final long serialVersionUID = 2608058366049676469L;
private java.lang.Integer subId;
private java.lang.Integer orderId;
private java.lang.String goods;
private java.lang.String description;
public SubOrder() {
}
public SubOrder(java.lang.Integer subId,java.lang.Integer orderId,java.lang.String goods,java.lang.String description) {
this.subId = subId;
this.orderId = orderId;
this.goods = goods;
this.description = description;
}
public void setSubId (java.lang.Integer subId) {
this.subId = subId;
}
public java.lang.Integer getSubId() {
return subId;
}
public void setOrderId (java.lang.Integer orderId) {
this.orderId = orderId;
}
public java.lang.Integer getOrderId() {
return orderId;
}
public void setGoods (java.lang.String goods) {
this.goods = goods;
}
public java.lang.String getGoods() {
return goods;
}
public void setDescription (java.lang.String description) {
this.description = description;
}
public java.lang.String getDescription() {
return description;
}
@Override
public String toString() {
return String.format("subId:%s,orderId:%s,goods:%s,description:%s",subId,orderId,goods,description);
}
}
MainOrder.hbm.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.baicai.spring.entity">
<class name="MainOrder" table="mainorder">
<id name="orderId" type="java.lang.Integer">
<column name="orderId" sql-type="INT" not-null="true"
precision="11" scale="0">
</column>
<generator class="native"></generator>
</id>
<property name="customer" type="java.lang.String">
<column name="customer" sql-type="VARCHAR(50)"
not-null="false" length="50">
</column>
</property>
<property name="description" type="java.lang.String">
<column name="description" sql-type="VARCHAR(500)"
not-null="false" length="500">
</column>
</property>
</class>
</hibernate-mapping>
SubOrder.hbm.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.baicai.spring.entity">
<class name="SubOrder" table="suborder">
<id name="subId" type="java.lang.Integer">
<column name="subId" sql-type="INT" not-null="true"
precision="11" scale="0">
</column>
<generator class="native"></generator>
</id>
<property name="orderId" type="java.lang.Integer">
<column name="orderId" sql-type="INT" not-null="true"
precision="11" scale="0">
</column>
</property>
<property name="goods" type="java.lang.String">
<column name="goods" sql-type="VARCHAR(50)" not-null="false"
length="50">
</column>
</property>
<property name="description" type="java.lang.String">
<column name="description" sql-type="VARCHAR(500)"
not-null="false" length="500">
</column>
</property>
</class>
</hibernate-mapping>
MainOrderDao.java
package org.baicai.spring.dao;
import java.util.List;
import org.baicai.spring.entity.MainOrder;
public interface MainOrderDao {
public void saveMainorder(MainOrder mainorder) throws Exception;
public void updateMainorder(MainOrder mainorder) throws Exception;
public void deleteMainorder(MainOrder mainorder) throws Exception;
public MainOrder queryMainorderByKey(MainOrder mainorder) throws Exception;
public List<MainOrder> queryAllMainorder() throws Exception;
}
SuborderDao.java
package org.baicai.spring.dao;
import java.util.List;
import org.baicai.spring.entity.SubOrder;
public interface SuborderDao {
public void saveSuborder(SubOrder suborder) throws Exception;
public void updateSuborder(SubOrder suborder) throws Exception;
public void deleteSuborder(SubOrder suborder) throws Exception;
public SubOrder querySuborderByKey(SubOrder suborder) throws Exception;
public List<SubOrder> queryAllSuborder() throws Exception;
}
BasicDao.java
package org.baicai.spring.impl.hibernate;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class BasicDao extends HibernateDaoSupport {
public BasicDao() {
}
protected <T> void saveEntity(T t) throws Exception
{
getHibernateTemplate().save(t);
}
protected <T> void updateEntity(T t) throws Exception
{
getHibernateTemplate().update(t);
}
protected <T> void deleteEntity(T t)throws Exception
{
getHibernateTemplate().delete(t);
}
@SuppressWarnings("unchecked")
protected <T> T queryEntityByKey(T t,Serializable key)throws Exception
{
return (T)getHibernateTemplate().get(t.getClass(), key);
}
@SuppressWarnings("unchecked")
protected <T> List<T> queryEntityAll(final Class<T> c)throws Exception
{
List<T> list=getHibernateTemplate().executeFind(
new HibernateCallback<List<T>>() {
@Override
public List<T> doInHibernate(Session session)
throws HibernateException, SQLException {
return session.createCriteria(c).list();
}
});
return list;
}
}
MainOrderDaoImpl.java
package org.baicai.spring.impl.hibernate;
import java.util.List;
import org.baicai.spring.dao.MainOrderDao;
import org.baicai.spring.entity.MainOrder;
public class MainOrderDaoImpl extends BasicDao implements MainOrderDao{
@Override
public void saveMainorder(MainOrder mainorder) throws Exception {
super.saveEntity(mainorder);
}
@Override
public void updateMainorder(MainOrder mainorder) throws Exception {
super.updateEntity(mainorder);
}
@Override
public void deleteMainorder(MainOrder mainorder) throws Exception {
super.deleteEntity(mainorder);
}
@Override
public MainOrder queryMainorderByKey(MainOrder mainorder) throws Exception {
return super.queryEntityByKey(mainorder, mainorder.getOrderId());
}
@Override
public List<MainOrder> queryAllMainorder() throws Exception {
return super.queryEntityAll(MainOrder.class);
}
}
SubOrderDaoImpl.java
package org.baicai.spring.impl.hibernate;
import java.util.List;
import org.baicai.spring.dao.SuborderDao;
import org.baicai.spring.entity.SubOrder;
public class SubOrderDaoImpl extends BasicDao implements SuborderDao{
@Override
public void saveSuborder(SubOrder suborder) throws Exception {
super.saveEntity(suborder);
}
@Override
public void updateSuborder(SubOrder suborder) throws Exception {
super.updateEntity(suborder);
}
@Override
public void deleteSuborder(SubOrder suborder) throws Exception {
super.deleteEntity(suborder);
}
@Override
public SubOrder querySuborderByKey(SubOrder suborder) throws Exception {
return queryEntityByKey(suborder, suborder.getSubId());
}
@Override
public List<SubOrder> queryAllSuborder() throws Exception {
return queryEntityAll(SubOrder.class);
}
}
BasicDao.java
package org.baicai.spring.impl.jdbc;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class BasicDao extends JdbcDaoSupport {
public BasicDao() {
}
}
MainOrderDaoImpl.java
package org.baicai.spring.impl.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import org.baicai.spring.dao.MainOrderDao;
import org.baicai.spring.entity.MainOrder;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
public class MainOrderDaoImpl extends BasicDao implements MainOrderDao{
@Override
public void saveMainorder( final MainOrder mainorder) throws Exception {
KeyHolder holder = new GeneratedKeyHolder();
final String sql="insert into MainOrder(customer,description) values(?,?)";
getJdbcTemplate().update(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection connection)
throws SQLException {
PreparedStatement ps =
connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
ps.setString(1, mainorder.getCustomer());
ps.setString(2, mainorder.getDescription());
return ps;
}
},holder);
mainorder.setOrderId(holder.getKey().intValue());
}
@Override
public void updateMainorder(MainOrder mainorder) throws Exception {
String sql="update MainOrder set customer=?,description=? where orderId=?";
getJdbcTemplate().update(sql,
mainorder.getCustomer(),mainorder.getDescription(),mainorder.getOrderId());
}
@Override
public void deleteMainorder(MainOrder mainorder) throws Exception {
String sql="delete table mainorder where orderId=?";
getJdbcTemplate().update(sql, mainorder.getOrderId());
}
@Override
public MainOrder queryMainorderByKey(MainOrder mainorder) throws Exception {
String sql="select * from mainorder where orderId=?";
return getJdbcTemplate().query(sql, new Object[]{mainorder.getOrderId()},new ResultSetExtractor<MainOrder>(){
@Override
public MainOrder extractData(ResultSet rs) throws SQLException,
DataAccessException {
MainOrder mainorder = new MainOrder();
mainorder.setCustomer(rs.getString("customer"));
mainorder.setDescription(rs.getString("description"));
mainorder.setOrderId(rs.getInt("orderid"));
return mainorder;
}
});
}
@Override
public List<MainOrder> queryAllMainorder() throws Exception {
String sql="select * from mainorder";
return getJdbcTemplate().query(sql, new RowMapper<MainOrder>(){
@Override
public MainOrder mapRow(ResultSet rs, int rowNumber)
throws SQLException {
MainOrder mainOrder= new MainOrder();
mainOrder.setCustomer(rs.getString("Customer"));
mainOrder.setDescription(rs.getString("Description"));
mainOrder.setOrderId(rs.getInt("orderId"));
return mainOrder;
}
});
}
}
SubOrderDaoImpl.java
package org.baicai.spring.impl.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import org.baicai.spring.dao.SuborderDao;
import org.baicai.spring.entity.MainOrder;
import org.baicai.spring.entity.SubOrder;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
public class SubOrderDaoImpl extends BasicDao implements SuborderDao{
@Override
public void saveSuborder(final SubOrder suborder) throws Exception {
KeyHolder holder = new GeneratedKeyHolder();
final String sql="insert into suborder(orderId,goods,description) values(?,?,?)";
getJdbcTemplate().update(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection connection)
throws SQLException {
PreparedStatement ps = connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
ps.setInt(1, suborder.getOrderId());
ps.setString(2, suborder.getGoods());
ps.setString(3, suborder.getDescription());
return ps;
}
}, holder);
suborder.setSubId(holder.getKey().intValue());
}
@Override
public void updateSuborder(SubOrder suborder) throws Exception {
String sql="update subordr set orderi=?,goods=?,description=? where subId=?";
getJdbcTemplate().update(sql,suborder.getOrderId(),suborder.getGoods(),suborder.getDes cription(),suborder.getOrderId());
}
@Override
public void deleteSuborder(SubOrder suborder) throws Exception {
String sql="delete table suborder where subId=?";
getJdbcTemplate().update(sql,suborder.getOrderId());
}
@Override
public SubOrder querySuborderByKey(final SubOrder suborder) throws Exception {
final String sql="select * from suborder where subId=? ";
return getJdbcTemplate().query(sql,new Object[]{suborder.getSubId()},new ResultSetExtractor<SubOrder>() {
@Override
public SubOrder extractData(ResultSet rs) throws SQLException,
DataAccessException {
SubOrder sb = new SubOrder();
sb.setOrderId(rs.getInt("orderId"));
sb.setDescription(rs.getString("Description"));
sb.setGoods(rs.getString("Goods"));
sb.setSubId(rs.getInt("SubId"));
return sb;
}
} );
}
@Override
public List<SubOrder> queryAllSuborder() throws Exception {
final String sql="select * from suborder";
return getJdbcTemplate().query(sql,new RowMapper<SubOrder>(){
@Override
public SubOrder mapRow(ResultSet rs, int rowNumber)
throws SQLException {
SubOrder so = new SubOrder();
so.setDescription(rs.getString("Description"));
so.setGoods(rs.getString("goods"));
so.setOrderId(rs.getInt("orderId"));
so.setSubId(rs.getInt("subId"));
return so;
}
});
}
}
DemoService.java
package org.baicai.spring.service;
import java.util.List;
import org.baicai.spring.entity.MainOrder;
import org.baicai.spring.entity.SubOrder;
public interface DemoService {
public void saveOrder(MainOrder order, List<SubOrder> subs)
throws Exception;
public List<MainOrder> queryAllMainorder() throws Exception;
public List<SubOrder> queryAllSuborder() throws Exception;
}
DemoServiceImpl.java
package org.baicai.spring.service.Impl;
import java.util.List;
import org.baicai.spring.dao.MainOrderDao;
import org.baicai.spring.dao.SuborderDao;
import org.baicai.spring.entity.MainOrder;
import org.baicai.spring.entity.SubOrder;
import org.baicai.spring.service.DemoService;
public class DemoServiceImpl implements DemoService {
private MainOrderDao mainOrderDao;
private SuborderDao subOrderDao;
public MainOrderDao getMainOrderDao() {
return mainOrderDao;
}
public void setMainOrderDao(MainOrderDao mainOrderDao) {
this.mainOrderDao = mainOrderDao;
}
public SuborderDao getSubOrderDao() {
return subOrderDao;
}
public void setSubOrderDao(SuborderDao subOrderDao) {
this.subOrderDao = subOrderDao;
}
@Override
public void saveOrder(MainOrder order, List<SubOrder> subs)
throws Exception {
mainOrderDao.saveMainorder(order);
for (SubOrder subOrder : subs) {
subOrder.setOrderId(order.getOrderId());
subOrderDao.saveSuborder(subOrder);
}
}
@Override
public List<MainOrder> queryAllMainorder() throws Exception {
return mainOrderDao.queryAllMainorder();
}
@Override
public List<SubOrder> queryAllSuborder() throws Exception {
return subOrderDao.queryAllSuborder();
}
}
HibernateDemo.java
package org.baicai.spring.demo;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.baicai.spring.entity.MainOrder;
import org.baicai.spring.entity.SubOrder;
import org.baicai.spring.service.DemoService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HibernateDemo {
public static void main(String[] args) throws Exception{
//添加测试数据
Random random=new Random();
MainOrder order=new MainOrder();
int sign =random.nextInt(10)+100;
order.setCustomer("mainOrder"+sign);
order.setDescription("description"+sign);
sign =random.nextInt(10)+100;
List<SubOrder> subs=new ArrayList<SubOrder>();
SubOrder subOrder= new SubOrder();
subOrder.setDescription("description"+sign);
subOrder.setGoods("goods"+sign);
subs.add(subOrder);
sign =random.nextInt(10)+100;
subOrder= new SubOrder();
subOrder.setDescription("description"+sign);
subOrder.setGoods("goods"+sign);
subs.add(subOrder);
//加载spring
ApplicationContext context = new ClassPathXmlApplicationContext("/applicationContextHibernate.xml");
System.out.println("========================================");
//测试
DemoService demoService=context.getBean("demoService",DemoService.class);
demoService.saveOrder(order, subs);
List<MainOrder> mlist = demoService.queryAllMainorder();
for (MainOrder mainOrder : mlist) {
System.out.println(mainOrder);
}
System.out.println("========================================");
List<SubOrder> slist= demoService.queryAllSuborder();
for (SubOrder subOrder2 : slist) {
System.out.println(subOrder2);
}
}
}
JdbcDemo.java
package org.baicai.spring.demo;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.baicai.spring.entity.MainOrder;
import org.baicai.spring.entity.SubOrder;
import org.baicai.spring.service.DemoService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class JdbcDemo {
public static void main(String[] args) throws Exception{
//添加测试数据
Random random=new Random();
MainOrder order=new MainOrder();
int sign =random.nextInt(10)+100;
order.setCustomer("mainOrder"+sign);
order.setDescription("description"+sign);
sign =random.nextInt(10)+100;
List<SubOrder> subs=new ArrayList<SubOrder>();
SubOrder subOrder= new SubOrder();
subOrder.setDescription("description"+sign);
subOrder.setGoods("goods"+sign);
subs.add(subOrder);
sign =random.nextInt(10)+100;
subOrder= new SubOrder();
subOrder.setDescription("description"+sign);
subOrder.setGoods("goods"+sign);
subs.add(subOrder);
//加载spring
ApplicationContext context = new ClassPathXmlApplicationContext("/applicationContextJdbc.xml");
System.out.println("========================================");
//测试
DemoService demoService=context.getBean("demoService",DemoService.class);
demoService.saveOrder(order, subs);
List<MainOrder> mlist = demoService.queryAllMainorder();
for (MainOrder mainOrder : mlist) {
System.out.println(mainOrder);
}
System.out.println("========================================");
List<SubOrder> slist= demoService.queryAllSuborder();
for (SubOrder subOrder2 : slist) {
System.out.println(subOrder2);
}
}
}
applicationContextHibernate.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-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- hibernate整合配置 -->
<!-- 数据库配置 -->
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:hibernate.properties"></property>
</bean>
<bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${hibernate.connection.driver_class}"></property>
<property name="jdbcUrl" value="${hibernate.connection.url}"></property>
<property name="user" value="${hibernate.connection.username}"></property>
<property name="password" value="${hibernate.connection.password}"></property>
<property name="minPoolSize" value="${hibernate.c3p0.min_size}"></property>
<property name="maxPoolSize" value="${hibernate.c3p0.max_size}"></property>
</bean>
<!-- hibernateSessionFactory配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 数据源参数配置 -->
<property name="dataSource" ref="c3p0"></property>
<!-- hbm文件的配置 -->
<property name="mappingResources">
<list>
<value>org/baicai/spring/entity/hbm/Mainorder.hbm.xml</value>
<value>org/baicai/spring/entity/hbm/Suborder.hbm.xml</value>
</list>
</property>
<!-- 其它属性配置 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.current_session_context_class">
${hibernate.current_session_context_class}
</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean>
<!-- dao配置 -->
<bean id="basicDao" class="org.baicai.spring.impl.hibernate.BasicDao" abstract="true">
<!-- 给dao注入hibernateSessionFactory -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="mainOrderDao" class="org.baicai.spring.impl.hibernate.MainOrderDaoImpl" parent="basicDao"></bean>
<bean id="suborderDao" class="org.baicai.spring.impl.hibernate.SubOrderDaoImpl" parent="basicDao"></bean>
<bean id="demoService" class="org.baicai.spring.service.Impl.DemoServiceImpl">
<property name="mainOrderDao" ref="mainOrderDao"></property>
<property name="subOrderDao" ref="suborderDao"></property>
</bean>
<!-- 事物的配置 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice transaction-manager="transactionManager" id="txAdvice">
<tx:attributes>
<!-- 配置方法签名事务拦截方式 -->
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="query*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!-- 事务拦截切面配置 -->
<aop:pointcut expression="execution(* org.baicai.spring.serivce.impl.**.*(..))" id="serviceMethods"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/>
</aop:config>
</beans>
applicationContextJdbc.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-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:hibernate.properties"></property>
</bean>
<bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${hibernate.connection.driver_class}"></property>
<property name="jdbcUrl" value="${hibernate.connection.url}"></property>
<property name="user" value="${hibernate.connection.username}"></property>
<property name="password" value="${hibernate.connection.password}"></property>
<property name="maxPoolSize" value="${hibernate.c3p0.max_size}"></property>
<property name="minPoolSize" value="${hibernate.c3p0.min_size}"></property>
</bean>
<bean id="basicDao" class="org.baicai.spring.impl.jdbc.BasicDao" abstract="true">
<property name="dataSource" ref="c3p0"></property>
</bean>
<bean id="subOrderDao" class="org.baicai.spring.impl.jdbc.SubOrderDaoImpl" parent="basicDao"> </bean>
<bean id="mainOrderDao" class="org.baicai.spring.impl.jdbc.MainOrderDaoImpl" parent="basicDao"> </bean>
<bean id="demoService" class="org.baicai.spring.service.Impl.DemoServiceImpl">
<property name="mainOrderDao" ref="mainOrderDao"></property>
<property name="subOrderDao" ref="subOrderDao"></property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="c3p0"></property>
</bean>
<tx:advice transaction-manager="transactionManager" id="fineMethod">
<tx:attributes >
<tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED"/>
<tx:method name="query*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* org.baicai.spring.service.impl.**.*(..))" id="servicePointcut"/>
<aop:advisor advice-ref="fineMethod" pointcut-ref="servicePointcut"/>
</aop:config>
</beans>
hibernate.properties
#connection
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc\:mysql\://127.0.0.1\:3306/Spring?characterEncoding\=utf8
hibernate.connection.username=root
hibernate.connection.password=paul
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
#common
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=15
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.current_session_context_class=thread
#other
system.url=jdbc\:mysql\://127.0.0.1\:3306/mysql?characterEncoding\=utf8
build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project default="reset.all">
<property file="../src/hibernate.properties"></property>
<path id="mysql">
<fileset file="../../../JAR_File/jdbc/mysql-connector-java-5.1.14-bin.jar"></fileset>
</path>
<target name="create.sql">
<sql userid="${hibernate.connection.username}" password="${hibernate.connection.password}" url="${system.url}" driver="${hibernate.connection.driver_class}" src="create.sql" encoding="utf-8" delimiter=";">
<classpath refid="mysql"></classpath>
</sql>
</target>
<target name="create.table.sql">
<sql delimiter=";" driver="${hibernate.connection.driver_class}" password="${hibernate.connection.password}" url="${hibernate.connection.url}" userid="${hibernate.connection.username}" src="create.table.sql" encoding="utf-8" classpathref="mysql" />
</target>
<target name="reset.all">
<antcall target="create.sql" />
<antcall target="create.table.sql" />
</target>
</project>
create.sql
drop database if exists Spring;
create database Spring default character set utf8;
create.table.sql
create table MainOrder
(
orderId integer auto_increment primary key,
customer varchar(50) not null,
description varchar(500) not null
);
create table SubOrder
(
subId integer auto_increment primary key,
orderId integer not null references MainOrder(orderId),
goods varchar(50) not null,
description varchar(500) not null
);
执行结果如下:
HibernateTemplate()
j
getJdbcTemplate()(数据有清空)