Java Spring Configuration Transaction

Spring Transactions

The management of transactions in Spring is to extract the transaction code through AOP

XML placement

The first step is to create a c3p0 connection
pool and replace the database-related properties in sessionFactory with c3p0

<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    <property name="jdbcUrl" value="jdbc:mysql:///MyDB"></property>
    <property name="user" value="root"></property>
    <property name="password" value="123456"></property>
</bean>

The second step is to create a transaction management object

<bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <!-- 注入会话工厂 -->
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

The third step is to set the session policy

<tx:advice id="advice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="insert*" read-only="false"/>
        <tx:method name="select*" read-only="true"/>
        <tx:method name="update*" read-only="false"/>
        <tx:method name="delete*" read-only="false"/>
    </tx:attributes>
</tx:advice>

The fourth step of weaving

<aop:config>
    <aop:pointcut expression="execution(* com.lanou.dao.impl.*.*(..))" id="point"/>
    <aop:advisor advice-ref="advice" pointcut-ref="point"/>
</aop:config>

Annotation configuration

The first two steps are the same as the annotations.
The third step is to start the AOP transaction annotation.

<tx:annotation-driven/>

The fourth step is to add annotations

@Transactional
public class UserDaoImpl extends HibernateDaoSupport implements UserDao

SSH operation database (HibernateDaoSupport)

为了节省代码 Spring提供模板类用于dao层操作数据库
封装了常用功能:获取session 执行查询等...
在SSH中 只要进行CRUD就继承HibernateDaoSupport 它能帮我们管理session
注意:别忘记注入sessionFactory

实现步骤:
    1.直接将Dao实现类继承自 HibernateDaoSupport类
    2.通过调用父类的getHibernateTemplate获取模板对象
    3.面向对象的  save delete update get
    4.使用hql语句/使用离线查询

Using HQL instance

public List<User> selectAllUser() {
    // 演示如何使用HQL
    return getHibernateTemplate().execute(new HibernateCallback<List<User>>() {

        // 将CRUD的操作放到这个方法中 模板对象会自动调用
        @Override
        public List<User> doInHibernate(Session session) throws HibernateException {
            String hql = "from User";
            Query query = session.createQuery(hql);
            List<User> list = query.list();
            return list;
        }
    });
}

Offline query instance

public User selectUserByName(String name) {
    // 离线查询对象
    DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
    // 添加查询条件
    criteria.add(Restrictions.eq("name", name));
    List<User> list = (List<User>) getHibernateTemplate().findByCriteria(criteria);
    if (list != null && list.size() > 0) {
        return list.get(0);
    }
    return null;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324835665&siteId=291194637