Spring整合Mybatis详解

Spring整合Mybatis详解_第1张图片

整合过程

1、导包

(1).Spring的包

(2).mybatis的包   

(3).再导入一个结合的包

2、项目搭建


Spring整合Mybatis详解_第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">
        
        <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>
                
                <property name="configLocation" value="classpath:resource/mybatis/sqlMapConfig.xml">property>
        bean>

        
        
        <bean name="userDao" class="cn.hd.dao.impl.UserDaoImpl">
                <property name="sqlSessionFactory" ref="sqlsessionFactory">property>
        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" ?>
 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>

    
    <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" ?>
 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" ?>
 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>


你可能感兴趣的:(Mybatis框架)