Mybatis-Spring整合配置汇总

Maven依赖

文件名:pom.xml

  • maven依赖包
	<dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.13version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.2version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.48version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>5.2.6.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>5.2.6.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.aspectjgroupId>
            <artifactId>aspectjweaverartifactId>
            <version>1.8.9version>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>2.0.4version>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.12version>
        dependency>
    dependencies>
  • 支持Idea在java包下加载xml文件,防止报错
    <build>
        <resources>
            <resource>
                <directory>src/main/resourcesdirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>truefiltering>
            resource>
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>truefiltering>
            resource>
        resources>
    build>

Mybatis配置文件

文件名:mybatis-config.xml

  • 配置Mybatis配置文件(也可以在Spring中配置)



<configuration>

    <typeAliases>
        <package name="com.example.pojo"/>
    typeAliases>

configuration>

Spring配置文件

  • Spring配置文件

<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
        http://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=false&useUnicode=true&characterEncoding=utf-8"/>
       <property name="username" value="root"/>
       <property name="password" value="123456"/>
   bean>
   
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <property name="dataSource" ref="dataSource"/>
   
       <property name="configLocation" value="classpath:mybatis-config.xml"/>
       <property name="mapperLocations" value="classpath:com/example/mapper/*.xml"/>
   bean>
   
   <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
   
       <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
   bean>

   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="dataSource"/>
   bean>



   <tx:advice id="txAdvice" transaction-manager="transactionManager">
       <tx:attributes>


           <tx:method name="add" propagation="REQUIRED"/>
           <tx:method name="delete" propagation="REQUIRED"/>
           <tx:method name="update" propagation="REQUIRED"/>
           <tx:method name="query" read-only="true"/>
           <tx:method name="*" propagation="REQUIRED"/>
       tx:attributes>
   tx:advice>


   <aop:config>
       <aop:pointcut id="txPointcut" expression="execution(* com.example.mapper.UserMapper.*(..))"/>
       <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
   aop:config>
beans>

其中sqlSession可以不配

整合配置文件,用于管理Mapper中的类(mybatis-config.xml)

  • 配置sqlSession

文件名:mybatis-config.xml:


<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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <import resource="spring-config.xml"/>




    <bean id="userMapper" class="com.example.mapper.UserMapperImpl">
        <property name="sqlSession" ref="sqlSession"/>
    bean>
beans>

userMapperImpl(通过sqlSessionTemplate来操作)

public class UserMapperImpl implements UserMapper {

    //所有操作通过sqlSessionTemplate来操作
    private SqlSessionTemplate sqlSession;

    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }

    public List<User> queryUser() {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.queryUser();
        return users;
    }
}
  • 不配置sqlSession

文件名:mybatis-config.xml:


<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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <import resource="spring-config.xml"/>

    <bean id="userMapper" class="com.example.mapper.UserMapperImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    bean>



beans>

userMapperImpl(通过sqlSessionFactory来操作,继承SqlSessionDaoSupport类,getSqlSession()获得sqlSession对象)

public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper {
    public List<User> queryUser() {
    	//获取Mapper的接口
        UserMapper mapper = getSqlSession().getMapper(UserMapper.class);
        //从Mybatis的Mapper.xml中查询数据
        List<User> users = mapper.queryUser();
        return users;
    }
}

你可能感兴趣的:(Mybatis-Spring整合配置汇总)