spring与mybatis结合

本文详细介绍如何在Maven项目中整合Spring框架,包括配置常量、数据源、事务管理、MyBatis集成及XML配置等关键步骤。通过具体代码示例,展示如何实现Spring与MyBatis的无缝连接,为读者提供一个完整的开发流程指导。
maven中的操作
 <properties>
        <!--在这个标记下配置常量-->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <spring.version>5.1.5.RELEASE</spring.version>
        <!--常量版本太多容易管理定义一个常量只要跟这个包版本一致找这个常量即可-->
    </properties>
—————————————————————————————
  <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
            <!--使用常量通过常量名来调用-->
        </dependency>


需要的jar:

数据数据源c3p0

Spring包

mybatis包


xml配置:注意Spring的xml文件不要和mybatis的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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
       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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--注解配置-->
    <context:annotation-config></context:annotation-config>
    <context:component-scan base-package="com.sun.SpringAndMybatis"></context:component-scan>

    <!--事务管理员-->
    <tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager"/>
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--数据库链接-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test2?useUnicode=true&
                characterEncoding=UTF-8&useSSL=false"/>
        <property name="user" value="root"/>
        <property name="password" value="Sb1996350."/>
        <property name="maxPoolSize" value="3"/>
        <property name="minPoolSize" value="1"/>
    </bean>

    <!--创建mybatis对象工厂sqlSessionFactoryBean-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--数据源对象 将dataSource对象放入、属性dataSource中-->
        <!--相应的配置找里面的属性进行配置即可-->
        <!--mybatis配置的xml路径-->
        <property name="mapperLocations" value="classpath:mybatis/*.xml"></property>
    </bean>

    <!--扫描接口-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--扫描mapper映射接口-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">
            <!--value通过名字来查找sqlSessionFactory对象-->
        </property>
        <property name="basePackage" value="com.sun.SpringAndMybatis.Dao"/>
    </bean>
</beans>

其余操作就跟mybatis操作是一样的


package com.sun.SpringAndMybatis;

import com.sun.SpringAndMybatis.Dao.Stu;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test2 {


    public static void main(String[] args) {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("app.xml");
        Test test = applicationContext.getBean(Test.class);
        Stu stu = test.Select(5);
        System.out.println(stu.toString());
    }
}


@Component
@Transactional
public class Test {
    @Autowired
    private StuMapper stuMapper;//引用接口 通过标记将对象赋值给他

    public void rgeUser(Integer sid, String sname, String address) {
        Stu stu = new Stu();
        stu.setAddress(address);
        stu.setSname(sname);
        stuMapper.insertSelective(stu);
    }

    public Stu Select(Integer sid) {
        Stu stu = stuMapper.selectByPrimaryKey(sid);
        return stu;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值