欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Mybatis学习 四

程序员文章站 2023-12-27 10:40:51
Spring 集成Mybatis 一:首先下载jar包(Spring、mybatis、dbcp及mybatis-spring),注意Spring与mybatis之间是有版本要求的,不然会报错,具体版本间对照网上很多,就不贴出来了 数据库连接池使用的是dbcp,这个看自己,可以随时替换 二:配置spr ......

spring 集成mybatis

一:首先下载jar包(spring、mybatis、dbcp及mybatis-spring),注意spring与mybatis之间是有版本要求的,不然会报错,具体版本间对照网上很多,就不贴出来了

数据库连接池使用的是dbcp,这个看自己,可以随时替换

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
<dependency>
<groupid>org.apache.commons</groupid>
<artifactid>commons-dbcp2</artifactid>
<version>2.1.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupid>org.mybatis</groupid>
<artifactid>mybatis-spring</artifactid>
<version>1.3.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupid>org.mybatis</groupid>
<artifactid>mybatis</artifactid>
<version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-context</artifactid>
<version>4.3.14.release</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-jdbc</artifactid>
<version>4.3.14.release</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.batch/spring-batch-infrastructure -->
<dependency>
<groupid>org.springframework.batch</groupid>
<artifactid>spring-batch-infrastructure</artifactid>
<version>3.0.9.release</version>
</dependency>

二:配置spring配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

<!--开启spring扫描-->
<context:component-scan base-package="com.founderit"/>
<context:annotation-config/>

<!--dbcp数据库连接池-->
<bean id="datasource"
class="org.apache.commons.dbcp2.basicdatasource"
destroy-method="close">
<property name="driverclassname" value="oracle.jdbc.driver.oracledriver"/>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
<property name="username" value="scott"/>
<property name="password" value="scott"/>
<!--maxactive: 最大连接数量-->
<property name="maxtotal" value="150"/>
<!--minidle: 最小空闲连接-->
<property name="minidle" value="5"/>
<!--maxidle: 最大空闲连接-->
<property name="maxidle" value="20"/>
<!--initialsize: 初始化连接-->
<property name="initialsize" value="30"/>
<!--removeabandonedtimeout: 超时时间(以秒数为单位)-->
<property name="removeabandonedtimeout" value="10"/>
</bean>

<!-- spring和mybatis完美整合 -->
<bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
<property name="datasource" ref="datasource"/>
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperlocations" value="com/founderit/mapper/*.xml"></property>
</bean>

<!-- dao接口所在包名,spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.mapperscannerconfigurer">
<property name="basepackage" value="com.founderit.dao"/>
<!-- <property name="sqlsessionfactorybeanname" value="sqlsessionfactory"></property>-->
</bean>
<!-- (事务管理)transaction manager -->
<bean id="transactionmanager"
class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
<property name="datasource" ref="datasource"/>
</bean>
</beans>


三:测试

Mybatis学习 四

 

 由于我们要在main方法中测试,所以讲userdao加了static关键字,运行提示错误,@autowired自动注入不支持静态域,再次修改测试

Mybatis学习 四

 

 我们从applicationcontext中得到testmain的实例,执行下面的bindssm方法,测试成功

另外需要注意的是必须从applicationcontext中得到testmain的实例,不能自己new maintest()得到实例,不然userdao会注入不成功

 

上一篇:

下一篇: