除暖

SSM整合-配置分页插件与事务 03

电脑版发表于:2022/8/15 14:17

续写于SSM整合-Spring整合MyBatis 02
一、
1、配分页插件

2、配事务jdbc类型的事务
2.1、开启事务驱动

2.2、注入jdbc类型的事务

目前在applicationContext.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--  包扫描-->
    <context:component-scan base-package="com.itheima"/>
    <!--    加载properties文件-->
    <context:property-placeholder location="classpath*:jdbc.properties"/>
    <!--    事务驱动-->
    <tx:annotation-driven transaction-manager="txManager"/>
    <!--数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--    将Mybatis集合到spring-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--        实体类所在的包位置-->
        <property name="typeAliasesPackage" value="com.itheima.domian"/>
        <!--        分页插件-->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <props>
                            <prop key="helperDialect">mysql</prop>
                            <prop key="reasonable">true</prop>
                        </props>
                    </property>
                </bean>
            </array>
        </property>
    </bean>
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>


2.3、在UserService业务接口添加事务的注解,注意增删改不能设为只读

import org.springframework.transaction.annotation.Transactional;

@Transactional(readOnly = true)
public interface UserService {
    /**
     * 保存用户信息
     * @param user
     * @return
     */
    @Transactional(readOnly = false)
    public boolean save(User user);

    /**
     * 删除用户信息
     * @param uuid
     * @return
     */
    @Transactional(readOnly = false)
    public boolean delete(Integer uuid);

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @Transactional(readOnly = false)
    public boolean update(User user);

    /**
     * 查询用户信息
     * @param uuid
     * @return
     */
    public User get(Integer uuid);

    /**
     * 分页查询用户信息
     * @param page
     * @param size
     * @return
     */
   public PageInfo<User> getAll(int page, int size);

    /**
     * 用户的登录
     * @param userName
     * @param password
     * @return
     */
    public User login(String userName,String password);
}

Transactional注解详解:https://blog.csdn.net/weixin_44517305/article/details/117747798


关于TNBLOG
TNBLOG,技术分享。技术交流:群号677373950
ICP备案 :渝ICP备18016597号-1
App store Android
精彩评论
{{item.replyName}}
{{item.content}}
{{item.time}}
{{subpj.replyName}}
@{{subpj.beReplyName}}{{subpj.content}}
{{subpj.time}}
猜你喜欢