1.如果使用maven的话,pom.xml中加入:
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
不要忘记还要有需要的连接池依赖和jdbc驱动依赖.
2.编写spring配置文件:
<!-- 配置数据库连接,阿里数据源druid连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="jdbc:mysql://123.206.46.28:3306/first?useSSL=false&useUnicode=true&characterEncoding=UTF-8" />
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="username" value="root" />
<property name="password" value="gy5211314" />
<property name="filters" value="stat" />
<property name="maxActive" value="20" />
<property name="initialSize" value="1" />
<property name="maxWait" value="60000" />
<property name="minIdle" value="1" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="poolPreparedStatements" value="true" />
<property name="maxOpenPreparedStatements" value="20" />
</bean>
<!-- MyBatis sqlSessionFactory 配置 mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- <property name="configLocation" value="classpath:mybatis-configuration.xml" /> -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- scan for mappers and let them be autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.test.mapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- 事务相关控制 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 对业务层所有方法添加事务,除了以get、find、select开始的 -->
<tx:method name="*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="java.lang.Exception" />
<!-- 查询操作没有必要开启事务,给只读事务添加一个属性read-only -->
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="select*" read-only="true" />
<tx:method name="query*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- Service层事务控制 -->
<aop:config>
<aop:pointcut id="service" expression="execution(* com.test.service..*.*(..))" />
<aop:advisor pointcut-ref="service" advice-ref="txAdvice" />
</aop:config>
这里说一下注意问题:
1.在配置sqlSessionFactory的时候,需要配置数据源参数,也就是加入连接池.
2.如果你的持久层class文件和xml文件都在同一文件夹下的话,并且在xml文件中配置了resultMap,在下面配置自动扫描时,会自动扫描到包下的class和xml文件.所以这里不用再配置工厂所需的configLocation参数.configLocation参数例如:
<!--指定实体类别名,扫描包或者设置具体类及别名 -->
<configuration>
<typeAliases>
<package name="com.test.model"/>
<!-- <typeAlias type="com.test.model.Test" alias="test"/> -->
</typeAliases>
<!--指定持久层映射文件包名 -->
<mappers>
<package name="com.test.mapper" />
</mappers>
</configuration>
3.如果你在xml文件中并没有使用resultMap,并且xml和class没有在一个包下,那么这里就需要配置上面的配置文件指定实体类别名,并且需要配置xml所在位置:
<property name="mapperLocations" value="classpath:com/gao/mapper/*.xml"/>