Spring——MyBatis整合

Posted xiao-ran

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring——MyBatis整合相关的知识,希望对你有一定的参考价值。

一、xml配置版

  1.导入依赖

<!--MyBatis和Spring的整合包 由MyBatis提供-->
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>
  <version>1.3.0</version>
</dependency>
<!--MyBatis的核心jar文件-->
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.4.1</version>
</dependency>

  2.创建实体类

public class User {
    private  Integer id;
    private  String name;
    private Double money;

    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 Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name=‘" + name + ‘‘‘ +
                ", money=" + money +
                ‘}‘;
    }
}

  3.创建Dao层接口及其映射文件

public interface UserDao {
    List<User> getAll();
}
<?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">
<!--小配置根节点 namespace代表命名空间-->
<mapper namespace="com.wdksoft.dao.UserDao">
    <select id="getAll" resultType="User">
        select * from user
    </select>
</mapper>

  4.Service层接口及其实现类

public interface UserService {
    List<User> getAll();
}
public class UserServiceImpl implements UserService {
    private UserDao userDao;

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public List<User> getAll() {
        return userDao.getAll();
    }
}

  5.配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!-- xml文件的头文件,起到对文件的约束作用(例如:必须存在哪些节点) -->
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <typeAliases>
        <package name="com.wdksoft.entity"/>
    </typeAliases>

    <mappers><!--管理我们配置的映射文件-->
        <package name="com.wdksoft.dao"/>
    </mappers>

</configuration>
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/bank?useUniCode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123
 <!--加载配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--配置数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--配置SqlSessionFactoryBean-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--加载Mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
    <!--扫描Dao层,省略代码-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.wdksoft.dao"/>
    </bean>
    <!--注入dao层-->
    <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.wdksoft.dao.UserDao"></property>
        <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"></property>
    </bean>
    <!--注入Service-->

    <bean id="userService" class="com.wdksoft.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
    </bean>

二、注解版

  Spring配置文件:

<!--包扫描器-->
    <context:component-scan base-package="com.wdksoft"/>
    <!--加载配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--配置数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--配置SqlSessionFactoryBean-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--加载Mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
    <!--扫描Dao层,省略代码-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.wdksoft.dao"/>
    </bean>

  其他加入相应注解即可

技术图片

 

技术图片

 

 

 

以上是关于Spring——MyBatis整合的主要内容,如果未能解决你的问题,请参考以下文章

请教mybatis+spring+atomikos的整合问题

7.Spring:整合Mybatis

基于Spring+SpringMVC+MyBatis开发书评网Spring和MyBatis的整合

Spring-整合Mybatis

最新最全面的Spring详解——Spring-Mybatis整合

Spring Boot:Spring Boot整合Mybatis案例