【Java】mybatis 连接 sqlserver

1.pom.xml
		<!-- 连接sqlserver库: https://mvnrepository.com/artifact/com.microsoft.sqlserver/sqljdbc4 -->
		<dependency>
		    <groupId>com.microsoft.sqlserver</groupId>
		    <artifactId>sqljdbc4</artifactId>
		    <version>4.0</version>
		</dependency>
		<!-- 连接池  -->
		<dependency>
		    <groupId>commons-dbcp</groupId>
		    <artifactId>commons-dbcp</artifactId>
		    <version>1.4</version>
		</dependency>
		<!--mybatis -->
		<dependency>
		    <groupId>org.mybatis</groupId>
		    <artifactId>mybatis</artifactId>
		    <version>3.2.2</version>
		</dependency>
		<dependency>
		    <groupId>org.mybatis</groupId>
		    <artifactId>mybatis-spring</artifactId>
		    <version>1.2.0</version>
		</dependency>
		<!--mybatis end-->

2.applicationContext.xml

	<!--                  mybatis 	start                 		-->
		<!--配置数据源,这里使用Spring默认-->
     <bean id="sqlServerDataSource" class="org.apache.commons.dbcp.BasicDataSource">
			<property name="driverClassName" value="${jdbc.sqlserver.driver}"/>
	        <property name="url" value="${jdbc.sqlserver.url}"/>
	        <property name="username" value="${jdbc.sqlserver.username}"/>
	        <property name="password" value="${jdbc.sqlserver.password}"/>
	        <property name="initialSize" value="${jdbc.initialSize}"/>
	        <property name="minIdle" value="${jdbc.minIdle}"/>
	        <property name="maxIdle" value="${jdbc.maxIdle}"/>
	        <property name="maxActive" value="${jdbc.maxActive}"/>
	        <property name="maxWait" value="${jdbc.maxWait}"/>
	        <property name="defaultAutoCommit" value="${jdbc.defaultAutoCommit}"/>
	        <property name="removeAbandoned" value="${jdbc.removeAbandoned}"/>
	        <property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}"/>
	        <property name="testWhileIdle" value="${jdbc.testWhileIdle}"/>
	        <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/>
	        <property name="numTestsPerEvictionRun" value="${jdbc.numTestsPerEvictionRun}"/>
	        <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/>
     </bean>
     
      <!--扫描Mapper-->
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
         <property name="basePackage" value="com.naton.dao"/>
 		 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
     </bean>
 
     <!--配置sqlSessionFactory-->
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
         <property name="configLocation" value="classpath:mybatis-config.xml"/>
         <property name="dataSource" ref="sqlServerDataSource"/>
         <property name="mapperLocations">
            <list>
                <value>classpath:mapper/*.xml</value>
            </list>
        </property>
     </bean>
     
     <!-- (事务管理)transaction manager -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="sqlServerDataSource" />
    </bean>
     
     <!-- 使用annotation定义数据库事务,这样可以在类或方法中直接使用@Transactional注解来声明事务 -->
     <tx:annotation-driven transaction-manager="transactionManager" />
     <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="approve" propagation="REQUIRED" />
            <tx:method name="undo" propagation="REQUIRED" />
            <tx:method name="load*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="search*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="*" propagation="SUPPORTS" read-only="true" />
        </tx:attributes>
     </tx:advice>
     <aop:config>
        <aop:pointcut id="serviceMethod" expression="execution(* com.naton.dao.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
     </aop:config> 
  
     
	<!--                  mybatis    end                         -->
3.配置文件
jdbc.sqlserver.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.sqlserver.url=jdbc:sqlserver://127.0.0.1:1433;database=test
jdbc.sqlserver.username=sa
jdbc.sqlserver.password=sa
jdbc.initialSize=5
jdbc.minIdle=5
jdbc.maxIdle=20
jdbc.maxActive=100
jdbc.maxWait=100000
jdbc.defaultAutoCommit=false
jdbc.removeAbandoned=true
jdbc.removeAbandonedTimeout=600
jdbc.testWhileIdle=true
jdbc.timeBetweenEvictionRunsMillis=60000
jdbc.numTestsPerEvictionRun=20

jdbc.minEvictableIdleTimeMillis=300000

mapper文件放在resource/mapper文件夹下面

猜你喜欢

转载自blog.csdn.net/jack_eusong/article/details/79207782