Mybatis-Spring整合、Spring实现事务管理

本文详细介绍Mybatis与Spring框架的整合步骤,包括数据源、sqlSessionFactory和sqlSessionTemplate的配置,实现接口的方式,以及如何在Spring中注入实现类进行测试。同时,还提供了两种实现接口的具体代码示例,以及如何通过Spring的ApplicationContext获取并使用这些组件。
部署运行你感兴趣的模型镜像

Mybatis-Spring整合

整合步骤:

  1. 编写数据源配置
  2. 编写 sqlSessionFactory配置
  3. 编写sqlSessionTemplate配置
  4. 实现接口实现类
  5. 将实现类,注入到Spring
  6. 测试使用即可

代码实现

步骤1,2,3实现

   <!--数据源配置-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
    <property name="username" value="root"/>
    <property name="password" value="123456"/>
</bean>
    <!--sqlSessionFactory配置-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <!--绑定Mabatis配置文件-->
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
    <property name="mapperLocations" value="classpath:com/practice/dao/*.xml"/>
</bean>
    <!--sqlSession配置-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>

步骤4实现

  1. 方式一
public class UserMapper implements Mapper {
    private SqlSessionTemplate sqlSessionTemplate;

    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }
    public List<user> selectUser() {
        Mapper mapper = sqlSessionTemplate.getMapper(Mapper.class);
        return mapper.selectUser();
    }
}

  1. 方式二(使用getSqlSession()来获取SqlSession对象)
public class UserMapper2 extends SqlSessionDaoSupport implements Mapper {

    public List<user> selectUser() {
        Mapper mapper = getSqlSession().getMapper(Mapper.class);
        return mapper.selectUser();
    }
}

步骤5实现

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
       https://www.springframework.org/schema/aop/spring-aop.xsd
">
   <import resource="beans.xml"/>
    <bean id="userMapper" class="com.practice.dao.UserMapper">
        <property name="sqlSessionTemplate" ref="sqlSession"/>
    </bean>
    <bean id="userMapper2" class="com.practice.dao.UserMapper2">
       <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
</beans>

步骤6实现

public class myTest {
    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        Mapper userMapper = context.getBean("userMapper2", Mapper.class);
        List<user> users = userMapper.selectUser();
        for (user user : users) {
            System.out.println(user);
        }
    }
}

Spring实现事务管理(xml文件配置)

  1. 配置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
       https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
       https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
       https://www.springframework.org/schema/tx/spring-tx.xsd
">
    <!--数据源配置-->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
    <property name="username" value="root"/>
    <property name="password" value="123456"/>
	</bean>
    <!--sqlSessionFactory配置-->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <!--绑定Mabatis配置文件-->
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
    <property name="mapperLocations" value="classpath:com/practice/dao/*.xml"/>
	</bean>
    <!--sqlSession配置-->
	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0" ref="sqlSessionFactory"/>
	</bean>
	 <!--配置声明式事务-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <constructor-arg ref="dataSource" />
    </bean>
    <!--配置事务通知;-->
    <tx:advice id="txad" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
     <!--配置事务切入-->
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* com.practice.dao.*.*(..))"/>
        <aop:advisor advice-ref="txad" pointcut-ref="pointcut"/>
    </aop:config>
  1. 余下步骤,便是整合步骤的4,5,6步,

您可能感兴趣的与本文相关的镜像

Llama Factory

Llama Factory

模型微调
LLama-Factory

LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值