Spring整合Mybatis詳解

整合過程

1、導包

(1).Spring的包

(2).mybatis的包   

(3).再導入一個結合的包

2、項目搭建


3、整合mybatis

1、原始整合

不管那種方式都依賴於SessionFactory。SqlSessionFactory需要依賴於讀取mybatis核心配置文件中的信息。

       分成三層:

1、         別名等屬性設置

2、         數據庫的連接信息

3、         讀取映射文件

其中的第二層可以使用連接池來取代。實際上,需要依賴兩個,一個是dataSource還有是核心配置文件。

2、mapper整合

              (1).Spring配置bean —》MapperFactoryBean類

       依賴於兩個  SQLSessionFactory和接口的位置

直接進入測試

              (2).配置一個掃描類,自動掃描某個位置下所有的mapper,並自動起名爲所寫類名對應的小寫。

applicationContext.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">
        <!--讀取db.properties-->
        <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>

        <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
               <property name="driverClass" value="${jdbc.driver}"></property>
               <property name="jdbcUrl" value="${jdbc.url}"></property>
               <property name="user" value="${jdbc.username}"></property>
               <property name="password" value="${jdbc.password}"></property>
       </bean>
        <bean name="sqlsessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
                <property name="dataSource" ref="dataSource"></property>
                <!--讀取sqlMapperConfig.xml-->
                <property name="configLocation" value="classpath:resource/mybatis/sqlMapConfig.xml"></property>
        </bean>

        <!--原始整合-->
        <!--配置UserDao-->
        <bean name="userDao" class="cn.hd.dao.impl.UserDaoImpl">
                <property name="sqlSessionFactory" ref="sqlsessionFactory"></property>
        </bean>

        <!--mapper整合-->
        <!--<bean name="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
                <property name="sqlSessionFactory" ref="sqlsessionFactory"></property>
                <property name="mapperInterface" value="cn.hd.mapper.UserMapper"></property>
        </bean>-->
        <!--自動掃描包下面的所有Mapper,當出現多個Mapper時,不用多次書寫bean-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
                <property name="basePackage" value="cn.hd.mapper"></property>
        </bean>

</beans>

UserDao.xml

package cn.hd.dao;

import cn.hd.entity.User;

public interface UserDao {
    /*沒有寫事務只能寫查詢,增刪改要提交事務纔有用*/
    User  findUserById(Integer id);
}

UserDaoImpl.xml

package cn.hd.dao.impl;

import cn.hd.dao.UserDao;
import cn.hd.entity.User;
import org.mybatis.spring.support.SqlSessionDaoSupport;

public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
    @Override
    public User findUserById(Integer id) {

        User user = getSqlSession().selectOne("test.findUserById", id);
        return user;
    }
}

SqlMapperConfig.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="cn.hd.entity"></package>
    </typeAliases>

    <!--讀取UserMapper.xml-->
    <mappers>
        <mapper resource="cn/hd/mapper/UserMapper.xml"></mapper>
    </mappers>
</configuration>

User.java

package cn.hd.entity;

public class User {
    private Integer id;
    private String name;
    private String sex;
    private String address;
    private Integer balance;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getBalance() {
        return balance;
    }

    public void setBalance(Integer balance) {
        this.balance = balance;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", address='" + address + '\'' +
                ", balance=" + balance +
                '}';
    }
}

UserMapper.JAVA接口

package cn.hd.mapper;

import cn.hd.entity.User;

public interface UserMapper {

    User findUserById(Integer id);
}
UserMapper.xml
<?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.hd.mapper.UserMapper">
    
    <select id="findUserById" parameterType="int" resultType="user">
        SELECT * FROM t_user WHERE id = #{id}
    </select>

</mapper>
測試類Demo

    

<?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.hd.mapper.UserMapper">
    
    <select id="findUserById" parameterType="int" resultType="user">
        SELECT * FROM t_user WHERE id = #{id}
    </select>

</mapper>


發佈了87 篇原創文章 · 獲贊 381 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章