spring mybatis 集成

maven jar

<dependencies>
         <!--spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>


spring config

<!--log4j config-->
    <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" >
        <property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
        <property name="targetMethod" value="initLogging" />
        <property name="arguments">
            <list>
                <value>file:res/log4j.properties</value>
            </list>
        </property>
    </bean>


    <context:component-scan base-package="Server.ServerCore.Controller"/>
    <context:component-scan base-package="Server.ServerCore.Repository"/>
    <context:component-scan base-package="Server.ServerCore.Service"/>
    <context:component-scan base-package="Server.ServerCore.Model"/>
    <context:component-scan base-package="Server.ServerCore.Model.dbsql"/>
    <context:component-scan base-package="Server"/>



    <!--DataSource config-->
      <!--boneCp config and druid config-->
    <bean id="boneCpDataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/landlords?useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
        <property name="partitionCount" value="3"/>
        <property name="maxConnectionsPerPartition" value="60"/>
        <property name="minConnectionsPerPartition" value="10"/>
        <property name="poolAvailabilityThreshold" value="20"/>
        <property name="acquireIncrement" value="5"/>
        <property name="statementsCacheSize" value="100"/>
        <property name="closeConnectionWatch" value="false"/>
        <property name="logStatementsEnabled" value="false"/>
    </bean>


    <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/landlords?useUnicode=true&amp;characterEncoding=UTF-8" />
        <property name="username" value="root"/>
        <property name="password" value="root" />
        <property name="maxActive"><value>20</value></property>
        <property name="initialSize"><value>1</value></property>
        <property name="maxWait"><value>60000</value></property>
        <property name="minIdle"><value>1</value></property>

        <property name="timeBetweenEvictionRunsMillis"><value>60000</value></property>
        <property name="minEvictableIdleTimeMillis"><value>300000</value></property>

        <property name="validationQuery"><value>SELECT 'x'</value></property>
        <property name="testWhileIdle"><value>true</value></property>
        <property name="testOnBorrow"><value>false</value></property>
        <property name="testOnReturn"><value>false</value></property>

        <property name="poolPreparedStatements"><value>true</value></property>
        <property name="maxOpenPreparedStatements"><value>20</value></property>
    </bean>

    <bean id="mysqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"
          autowire="default">
        <property name="dataSource" ref="druidDataSource"/>
        <property name="mapperLocations" value="classpath:Server/ServerCore/Model/dbsql/mapper/*.xml"/>
    </bean>

    <!--Note that only interfaces with at least one method will be registered; concrete classes will be ignored.
    register mapper-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="Server.ServerCore.Model.dbsql.mapper" />
        <property name="sqlSessionFactoryBeanName" value="mysqlSessionFactoryBean" />
    </bean>

你可能感兴趣的:(spring mybatis 集成)