MyBatis和Spring的整合

Posted 听风忆雪

tags:

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

 MyBatis和Spring整合(XML版)

所需要导入的jar文件

 

<!--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>

 

创建实体类

public class Bank implements Serializable {
    private int accountid;
    private String accountname;
    private double balance;

    public Integer getAccountid() {
        return accountid;
    }

    public void setAccountid(Integer accountid) {
        this.accountid = accountid;
    }

    public String getAccountname() {
        return accountname;
    }

    public void setAccountname(String accountname) {
        this.accountname = accountname;
    }

    public double getBalance() {
        return balance;
    }

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

}

Dao层接口

public interface IBankDao {
    public List<Bank> getAllBank();
}

小配置

<?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.spring.dao.IBankDao">
    <select id="getAllBank" resultType="Bank">
        select * from banks
    </select>
</mapper>

Service层接口

public interface IBankService {
    public List<Bank> getAllBank();
}

Service层实现类

public class IBankServiceImpl implements IBankService {

    private IBankDao iBankDao;

    @Override
    public List<Bank> getAllBank() {
        return iBankDao.getAllBank();
    }
    public IBankDao getiBankDao() {
return iBankDao;
}

public void setiBankDao(IBankDao iBankDao) {
this.iBankDao = iBankDao;
}
}

创建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">
<configuration>
    <!--配置别名-->
    <typeAliases>
        <package name="com.spring.entity"/>
    </typeAliases>
    <!--加载小配置文件-->
    <mappers>
        <package name="com.spring.dao"/>
    </mappers>
</configuration>

创建jdbc.properties文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/banks?useUniCode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123

创建applicationContext.xml文件

<!--加载配置文件-->
    <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.spring.dao"/>
    </bean>
    <!--注入Dao层-->
    <bean id="iBankDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.spring.dao.IBankDao"/>
        <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
    </bean>
    <!--Service层-->
    <bean id="iBankService" class="com.spring.service.impl.IBankServiceImpl">
        <property name="iBankDao" ref="iBankDao"/>
    </bean>

测试类

public class SpringMybaitsTest {
    public static void main(String[] args) {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        IBankService iBankService = (IBankService)ctx.getBean("iBankService");


        List<Bank> allBank = iBankService.getAllBank();
        for(Bank bank:allBank){
            System.out.println(bank.getAccountname());
        }
    }
}

MyBatis和Spring整合(注解版)

修改applicationContext.xml文件

<!--扫描注解-->
    <context:component-scan base-package="com.spring"/>
    <!--加载配置文件-->
    <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.spring.dao"/>
    </bean>

Dao层接口添加注解

@Repository
public interface IBankDao {
    public List<Bank> getAllBank();
}

Service层实现类添加注解

@Service("iBankService")
public class IBankServiceImpl implements IBankService {
    @Resource
    private IBankDao iBankDao;
    @Override
    public List<Bank> getAllBank() {
        return iBankDao.getAllBank();
    }

  }

 

 

 

 

 

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

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

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

spring和mybatis的整合(包含原始dao的整合方式和mapper代理整合方式)

7.Spring:整合Mybatis

spring整合mybatis时运行测试代码报错,是怎么回事?

MyBatis与Spring的整合(Eclipse版本和IDEA版本)