spring整合mybatis实例

  1. 编写数据源配置
  2. sqlsessionFactory
  3. sqlsessionTemplate
  4. 需要给接口加实现类
  5. 将自己写的实现类,注入到spring
  6. 测试使用即可

重点:数据源,工厂,sqlsession,绑定mybatis,映射,bean注入

实例如下:

一:导入依赖

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.2.13.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.40</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.2</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.3.1</version>
    </dependency>
    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.5.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.13.RELEASE</version>
        <!--            <scope>compile</scope>-->
    </dependency>
</dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>

                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>

            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>

                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>

            </resource>
        </resources>
    </build>

二:实体类及接口,及接口实现类

实体类:

package com.kuang.pojo;

public class books {
    private int bookID;
    private String bookName;
    private int bookCounts;
    private String datail;

    public int getBookID() {
        return bookID;
    }

    public void setBookID(int bookID) {
        this.bookID = bookID;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public int getBookCounts() {
        return bookCounts;
    }

    public void setBookCounts(int bookCounts) {
        this.bookCounts = bookCounts;
    }

    public String getDatail() {
        return datail;
    }

    public void setDatail(String datail) {
        this.datail = datail;
    }

    public books(int bookID, String bookName, int bookCounts, String datail) {
        this.bookID = bookID;
        this.bookName = bookName;
        this.bookCounts = bookCounts;
        this.datail = datail;
    }

    public books() {
    }

    @Override
    public String toString() {
        return "books{" +
                "bookID=" + bookID +
                ", bookName='" + bookName + '\'' +
                ", bookCounts=" + bookCounts +
                ", datail='" + datail + '\'' +
                '}';
    }
}

接口:

package com.kuang.dao;

import com.kuang.pojo.books;

import java.util.List;

public interface BookMapper {
   int addBook(books books) ;
   int deleteBookById(int id);
   int updateBook(books books);
   books queryBookById(int id);
   List<books> queryAllBook();
}

接口实现类:

package com.kuang.dao;

import com.kuang.pojo.books;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

public class BookMapperimpl implements BookMapper {
//以前我们所有的操作都是用session来执行,现在用slsessionTemplate来执行
    private SqlSessionTemplate sqlsession;


    public void getSqlsession(SqlSessionTemplate sqlsession) {
        this.sqlsession=sqlsession;

    }

    public int addBook(books books) {
        return 0;
    }

    public int deleteBookById(int id) {
        return 0;
    }

    public int updateBook(books books) {
        return 0;
    }

    public books queryBookById(int id) {
        return null;
    }

    public List<books> queryAllBook() {
       BookMapper mapper=sqlsession.getMapper(BookMapper.class);
       return mapper.queryAllBook();
    }

    public void setSqlsession(SqlSessionTemplate sqlsession) {
        this.sqlsession=sqlsession;
    }
}

实现接口的xml文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kuang.dao.BookMapper">
    <insert id="addBook" parameterType="books">
        insert into db4.books (bookName,bookCounts,datail)
        values(#{bookName},#{bookCounts},#{datail});
    </insert>
    <delete id="deleteBookById" parameterType="int">
        delete from db4.books
        where bookID=#{bookId}
    </delete>
    <update id="updateBook" parameterType="books">
        update db4.books
        set bookName=#{bookName},bookCounts=#{bookCounts},datail=#{datail}
        where bookID=#{bookID};
    </update>
    <select id="queryBookId" resultType="books">
        select * from db4.books
        where bookID=#{bookId}
    </select>
    <select id="queryAllBook" resultType="books">
        select * from db4.books
    </select>

</mapper>

三:mybatis核心配置文件及spring-dao配置文件

mybatis.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">
<configuration>
    <typeAliases>
        <package name="com.kuang.pojo"/>
    </typeAliases>

</configuration>

spring-dao.xml文件如下:

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


<!--    DataSource:会用spring的数据源替换mybatis的配置,c3p0,druid-->
    <!-- 关联数据库配置文件-->
<!--    <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>-->

    <!--   以前使用的数据源是mybatis的,现在用spring的-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db4?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="user" value="root"/>
        <property name="password" value="root"/>

    </bean>


<!--sqlsessionFactory-->
    <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
<!--        绑定mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!--&lt;!&ndash;        这里是配置映射器&ndash;&gt;-->
            <property name="mapperLocations" value="classpath:com/kuang/dao/*.xml"></property>

    </bean>
<!--    sqlsessionTemplate:就是我们使用的slsssion-->

<bean id="sqlsession" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0" ref="factory"/>
</bean>
<!--    这里是注入bean,参数是sqlsession-->
    <bean id="bookMapper" class="com.kuang.dao.BookMapperimpl">
    <property name="sqlsession" ref="sqlsession"/>
    </bean>
</beans>

最后测试:

import com.kuang.dao.BookMapper;
import com.kuang.pojo.books;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Mytest {
    @Test
    public void test(){
        ApplicationContext context= new ClassPathXmlApplicationContext("spring-dao.xml");
       BookMapper bookMapper= context.getBean("bookMapper", BookMapper.class);
       for(books book:bookMapper.queryAllBook()){
           System.out.println(book);
       }
    }
}

spring整合mybatis的时候,注意:数据源是交给spring来配置了,映射也是,xml文件也是交给spring来管理了。

最后:事务的四个特性:原子,隔离,持久,一致。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值