spring整合myBatis

1、簡介java

Spring是一個輕量級的控制反轉(IoC)和麪向切面(AOP)的容器框架。mysql

兩個重要模塊:Spring 面向方面編程(AOP)和控制反轉 (IOC) 容器。spring

控制反轉模式(也稱做依賴性介入)的基本概念是:不建立對象,可是描述建立它們的方式。在代碼中不直接與對象和服務鏈接,但在配置文件中描述哪個組件須要哪一項服務。sql

容器 (在 Spring 框架中是 IOC 容器) 負責將這些聯繫在一塊兒。在典型的 IOC 場景中,容器建立了全部對象,並設置必要的屬性將它們鏈接在一塊兒,決定什麼時間調用方法。數據庫

MyBatis 是支持普通 SQL查詢,存儲過程和高級映射的優秀 持久層框架。MyBatis 消除了幾乎全部的JDBC代碼和參數的手工設置以及
結果集的檢索。
MyBatis 使用簡單的 XML或註解用於配置和原始映射,將接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java
對象)映射成數據庫中的記錄。
    將建立SqlsessionFactory交給Spring。

二.實例express

引入jar包:編程

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

  

1.建立實體類session

package cn.happy.pojo;

public class Topic {
    private Integer tid;
    private String  tname;

    public Integer getTid() {
        return tid;
    }

    public void setTid(Integer tid) {
        this.tid = tid;
    }

    public String getTname() {
        return tname;
    }

    public void setTname(String tname) {
        this.tname = tname;
    }
}

2.建立dao層mybatis

public interface ITopicDao {
    /*
    * 添加主題的方法
    * */
    public int addTopic(Topic topic);

3.建立ITopicDao.xmlapp

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper  namespace="cn.happy.dao.ITopicDao">
   <insert id="addTopic" >
       insert into topic (tname) values (#{tname});
   </insert>

</mapper>

4.建立service層和他的實現類

public interface ITopicService {
    /*
* 添加主題的方法
* */
    public int addTopic(Topic topic);


}

 ITopicServiceImpl

public class TopicServiceImpl implements ITopicService {
    ITopicDao dao;

    public int addTopic(Topic topic) {
        return dao.addTopic(topic);
    }

    public ITopicDao getDao() {
        return dao;
    }

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

5.建立配置文件

01.Mybatis-config.xml

<?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">
<!--經過這個配置文件,完成mybatis與數據庫的鏈接-->
<configuration>
    <!-- 引入jdbc.properties-->
   <!-- <properties resource="jdbc.properties"/>-->

    <typeAliases>
        <package name="cn.happy.pojo"></package>
    </typeAliases>

    <mappers>
        <package name="cn.happy.dao"></package>
    </mappers>
</configuration>

02.jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/newssystem
jdbc.username=root

03.spring的配置文件

採用MapperScannerConfigurer,它將會查找類路徑下的映射器並自動將它們建立成MapperFactoryBean。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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
">
    <!--1.識別jdbc.properties文件-->
       <context:property-placeholder location="jdbc.properties"></context:property-placeholder>

    <!--工廠配置-->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!--大配置文件-->
        <property name="configLocation" value="classpath:MyBatis-config.xml"></property>
    </bean>
    <!--配置數據源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
    </bean>


    <!--dao(只需提供接口不需提供實現類 ) 映射文件的掃描器能夠動態的在內存中構建接口實現類,代理對象-->
    <bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.happy.dao"></property>
        <property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>
    </bean>

    <!--service-->
    <bean id="topicService" class="cn.happy.service.impl.TopicServiceImpl">
        <property name="dao" ref="ITopicDao"></property>
    </bean>
    <!--事物管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--AspectJ 配置事物-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="StockException"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <!--定義切點-->
        <aop:pointcut id="mypoint" expression="execution(* *..service.*.*(..))"></aop:pointcut>
        <!--配置顧問-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"></aop:advisor>
    </aop:config>




</beans>

6.建立測試類

public class test01 {
    @Test
    public void testAdd() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        ITopicService topicService = (ITopicService)ctx.getBean("topicService");
        Topic topic = new Topic();
        topic.setTname("白夜行");
        topicService.addTopic(topic);

    }
}
相關文章
相關標籤/搜索