首页 > 代码库 > Spring和Mybatis的整合

Spring和Mybatis的整合

复习之前的事物
Properties

事务:

数据库四种隔离级别
1.读未提交 Read_Uncommitted
2.读已经提交 Read_committed
3.可重复读 Repeatable_read
4.串行化 Serializable

 

1.这三个jar包是必须有的

要想整合这两个模块就准备的材料
大配置中的头文件
技术分享

 

 
 <dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>
  <version>1.3.0</version>
</dependency>


<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-tx</artifactId>
  <version>4.1.3.RELEASE</version>
</dependency> 
 
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>

技术分享

 

 

技术分享

 


<!--01.识别jdbc.properties文件-->context
<context:property-placeholder location="jdbc.properties"/>

<?xml version="1.0" encoding="UTF-8"?>
<beans xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd "
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns="http://www.springframework.org/schema/beans">

<!--01.识别jdbc.properties文件-->
<context:property-placeholder location="jdbc.properties"/>
<!--02.阿里巴巴-->
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
<property value="http://www.mamicode.com/${jdbc.driverClassName}" name="driverClassName"/>
<property value="http://www.mamicode.com/${jdbc.url}" name="url"/>
<property value="http://www.mamicode.com/${jdbc.user}" name="username"/>
<property value="http://www.mamicode.com/${jdbc.password}" name="password"/>



</bean>
<!--工厂配置-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>

<!--大配置路径-->
<property name="configLocation" value="http://www.mamicode.com/classpath:MyBatis-config.xml"></property>
</bean>
<!--dao 实现类映射文件的扫描器可以在动态的内存中构建接口的实现类,代理对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="http://www.mamicode.com/cn.spring21.dao"></property>
<property name="sqlSessionFactoryBeanName" value="http://www.mamicode.com/sessionFactory"></property>
</bean>

<!--service-->
<bean id="bookservice" class="cn.spring21.service.BookServiceImpl">
<property name="dao" ref="IBookDAO"></property>

</bean>
<!--事务管理器-->



<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">

<property name="dataSource" ref="dataSource"/>

</bean>

<!-- 第三种:AspectJ AOP 配置事务 -->



<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="buy*" rollback-for="StockException" propagation="REQUIRED" isolation="DEFAULT"/>
</tx:attributes>

</tx:advice>


<aop:config>
<!--配置了切点Pointcut-->
<aop:pointcut id="mypoint" expression="execution(* *..service.*.*(..))"/>
<!--顾问-->
<aop:advisor pointcut-ref="mypoint" advice-ref="txAdvice"/>
</aop:config>

</beans>

 
<?xml version="1.0" encoding="UTF-8" ?>
<!--文档类型,头文件-->
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
别名方便我们编写代码
<typeAliases>

<package name="cn.spring21.entity"></package>
</typeAliases>

</configuration>

 

直接写这个项目上的实体类就能行

 

0.dao层 只有接口没有实现类

public interface IBookDAO {
    public int addBook(Book bok);
}

 

0.service层实现

public class BookServiceImpl implements IBookService {
    //植入dao 对象间的交互
    IBookDAO dao;


    public int addBook(Book book) {
        return dao.addBook(book);
    }

    public IBookDAO getDao() {
        return dao;
    }

    public void setDao(IBookDAO dao) {
        this.dao = dao;
    }
}

jdbc的作用:可以连接数据库properties

0.jdbc.properties文件配置如下:为下面的大配置链接数据库的路径做了铺垫

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql:///bookshop

jdbc.user=root

jdbc.password=1234

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
</bean>
这里面引一道
<!--大配置路径-->
<property name="configLocation" value="http://www.mamicode.com/classpath:MyBatis-config.xml"></property>

!--big config path-->

 

最后的三个注意点:

 

1.整合引入一个节点

Mybatis-spring 目的识别到(工厂SqlSessionFactoryBean和扫描器

MapperScannerConfigurer,都位于这个包下)

2.注意头文件,加上一个tx事务

3.注意pom.xml注意builder能自动的将src.main.java下的包中的配置文件编译到target目录

  <build>
  <resources>
    <resource>
      <directory>src/main/java</directory>
      <includes>
        <include>**/*.*</include>
      </includes>
    </resource>
  </resources>
</build>


Spring和Mybatis的整合