添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xsi:schemaLocation="http://www.springframework.org/schema/beans   
                           http://www.springframework.org/schema/beans/spring-beans-3.0.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:3306/test"/>  
        <property name="username" value="root"/>  
        <property name="password" value="xxxxxx"/>
        <property name="initialSize" value="1"></property>
        <property name="maxActive" value="100"></property>
        <property name="maxIdle" value="2"></property>
        <property name="minIdle" value="1"></property>
    </bean>  
    <!-- sessionFactory -->  
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <property name="mappingResources">  
                <value>com/zzj/entity/Config.hbm.xml</value>  
            </list>  
        </property>
        <property name="hibernateProperties">  
            <value>  
                hibernate.dialect=org.hibernate.dialect.MySQLDialect
                hibernate.show_sql=true
            </value>  
        </property>  
    </bean> 
    <!-- Dao --> 
    <bean id="configDao" class="com.zzj.dao.ConfigDao">
    	<property name="sessionFactory" ref="sessionFactory"/>
    </bean>
</beans>  
package com.zzj.entity;
public class Config {
	private Integer id;
	private String name;
	private String value;
	public Config() {
	public Config(Integer id, String name, String value) {
		this.id = id;
		this.name = name;
		this.value = value;
	public Integer getId() {
		return id;
	public void setId(Integer id) {
		this.id = id;
	public String getName() {
		return name;
	public void setName(String name) {
		this.name = name;
	public String getValue() {
		return value;
	public void setValue(String value) {
		this.value = value;
	@Override
	public String toString() {
		return "Config [id=" + id + ", name=" + name + ", value=" + value + "]";
Hibernate映射文件:
<?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">  
<hibernate-mapping>  
    <class name="com.zzj.entity.Config" table="config">
        <id name="id" column="id">
            <generator class="identity" />
        <property name="name" column="name" />
        <property name="value" column="value" />
    </class>
</hibernate-mapping>
Dao类:
package com.zzj.dao;
import java.sql.SQLException;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class ConfigDao extends HibernateDaoSupport {
	public void save(){
		getHibernateTemplate().execute(new HibernateCallback<Integer>() {
			@SuppressWarnings("deprecation")
			@Override
			public Integer doInHibernate(Session session) throws HibernateException, SQLException {
				System.out.println("autoCommit:" + session.connection().getAutoCommit());
				Query query = session.createQuery("update Config set value = :value where name = :name");
				query.setString("name", "Tom");
				query.setString("value", "11");
				return query.executeUpdate();
package com.zzj;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zzj.dao.ConfigDao;
public class Test {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-common.xml");
		ConfigDao configDao = (ConfigDao) context.getBean("configDao");
		configDao.save();

Spring没有配置事务管理器,Dao类中也没有开启和提交事务的操作,事务也提交了!

注意:Dao类中输出的autoCommit为true。 数据库为MySQL,Spring版本3.2.1,Hibernate版本3.3.2。Spring配置文件: <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schema 老司机6517: 业务逻辑包裹在一个大的 SQL 语句中,而 Service 层可以做的事情很少。SQL 都是针对特定的业务功能编写的,复用性差。当我要开发另一个业务功能的时候,只能重新写个满足新需求的 SQL 语句,这就可能导致各种长得差不多、区别很小的 SQL 语句满天飞。 以上这段话没理解是什么意思,不是说基于贫血模型的开发业务都挤在service层吗?这里为什么说service没事可做?另外为什么会sql满天飞,基于贫血模型的开发,sql也基本只做一些增删改查啊