1.所需要导入的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>

2.MyBatis和Spring整合(xml版)

2.1创建Dao层接口

public interface AccountDao {
    //查询所有账户
    public List<Account> getAllAccount();
}

2.2创建Dao层接口小配置文件

<!--namespace需要指向接口全路径-->
<mapper namespace="cn.spring.dao.AccountDao">

    <select id="getAllAccount" resultType="cn.spring.entity.Account">
        select * from accounts
    </select>
</mapper>

2.3创建Service层接口

public interface AccountService {
    //查询所有账户
    public List<Account> getAllAccount();
}

2.4创建Service层接口实现类

public class AccountServiceImpl implements AccountService {

    AccountDao accountDao;

    @Override
    public List<Account> getAllAccount() {

        return accountDao.getAllAccount();
    }

    public AccountDao getAccountDao() {
        return accountDao;
    }

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

}

2.5 配置applicationContext.xml文件

<context:component-scan base-package="cn.spring"/>
<!-- 导入jdbc.properties文件-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:jdbc.properties"></property>
</bean>
<!--配置数据源  spring内置的数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driver}"></property>
    <property name="url" value="${jdbc.url}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>

<!--配置mybatis的核心对象sqlsessionfactorybean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="typeAliasesPackage" value="cn.spring.entity"></property>
    <property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>

<!--MyBatis的Dao接口的包扫描器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="cn.spring.dao"></property>
</bean>

<!--将Service实现类对象声明到Spring容器中
隐式注入-->
<bean id="accountService" class="cn.spring.service.impl.AccountServiceImpl" autowire="byType"></bean>

2.6编写测试类

@Test
 public void getAll(){
    ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
    AccountService accountService = (AccountService)context.getBean("accountService");
    List<Account> allAccount = accountService.getAllAccount();
    for (Account account:allAccount){
        System.out.println(account.getAccountid());
    }
}

3.MyBatis和Spring整合(注解版)

3.1创建Dao层接口

@Repository
public interface AccountDao {
    //查询所有账户
    public List<Account> getAllAccount();
}

3.2同上2.2

3.3同上2.3

3.4创建Service层接口实现类

@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    AccountDao accountDao;

    @Override
    public List<Account> getAllAccount() {

        return accountDao.getAllAccount();
    }

    public AccountDao getAccountDao() {
        return accountDao;
    }

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

}

3.5编写applicationContext.xml文件

<context:component-scan base-package="cn.spring"/>
<!-- 导入jdbc.properties文件-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:jdbc.properties"></property>
</bean>
<!--配置数据源  spring内置的数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driver}"></property>
    <property name="url" value="${jdbc.url}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>

<!--配置mybatis的核心对象sqlsessionfactorybean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="typeAliasesPackage" value="cn.spring.entity"></property>
    <property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>

<!--MyBatis的Dao接口的包扫描器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="cn.spring.dao"></property>
</bean>

3.6编写测试类

@Test
 public void getAll(){
    ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
    AccountService accountService = (AccountService)context.getBean("accountService");
    List<Account> allAccount = accountService.getAllAccount();
    for (Account account:allAccount){
        System.out.println(account.getAccountid());
    }
}

MyBatis和Spring整合案例的更多相关文章

  1. MyBatis学习(四)MyBatis和Spring整合

    MyBatis和Spring整合 思路 1.让spring管理SqlSessionFactory 2.让spring管理mapper对象和dao. 使用spring和mybatis整合开发mapper ...

  2. Mybatis与Spring整合,使用了maven管理项目,作为初学者觉得不错,转载下来

    转载自:http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype ...

  3. Mybatis+struts2+spring整合

    把student项目改造成ssm  struts2 +mybatis+spring 1,先添加spring支持:类库三个,applicationContext.xml写在webinf下四个命名空间,监 ...

  4. mybatis与spring整合(基于配置文件)

    本文主要介绍了如何将mybatis和spring整合在一起使用,本人使用的是mybatis3.05 + spring3.1.0M2 ,使用dbcp作为数据库连接池. 1.编写数据访问接口(UserDa ...

  5. mybatis与spring整合时读取properties问题的解决

    在学习mybatis与spring整合是,想从外部引用一个db.properties数据库配置文件,在配置文件中使用占位符进行引用,如下: <context:property-placehold ...

  6. Spring+SpringMVC+MyBatis深入学习及搭建(九)——MyBatis和Spring整合

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6964162.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(八)--My ...

  7. Mybatis第五篇【Mybatis与Spring整合】

    Mybatis与Spring整合 既然我们已经学了Mybatis的基本开发了,接下来就是Mybatis与Spring的整合了! 以下使用的是Oracle数据库来进行测试 导入jar包 aopallia ...

  8. MyBatis 与 Spring 整合

    MyBatis-Spring 项目 目前大部分的 Java 互联网项目,都是用 Spring MVC + Spring + MyBatis 搭建平台的. 使用 Spring IoC 可以有效的管理各类 ...

  9. Mybatis(六) Spring整合mybatis

    心莫浮躁~踏踏实实走,一步一个脚印,就算不学习,玩,能干嘛呢?人生就是那样,要找点有意思,打发时间的事情来做,而钻研技术,动脑动手的过程,还是比其他工作更有意思些~ so,努力啥的都是强迫自己做自以为 ...

随机推荐

  1. LeetCode 605. 种花问题(Can Place Flowers) 6

    605. 种花问题 605. Can Place Flowers 题目描述 假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. ...

  2. Python之匿名函数使用示例

    #!/usr/bin/env python # -*- coding:utf8 -*- # #匿名函数 # y = lambda x:x+1 # print(y(10)) name = 'AK' #一 ...

  3. NOIP2017[提高组] 宝藏 题解

    解析 我们观察范围可以发现n非常的小,(一般来说不是搜索就是状压dp)所以说对于这题我们可以用记忆化搜索或者dp,我们发现起点不同那么最终答案也就不同,也就是说答案是跟起点有关的,于是我们便可以想到去 ...

  4. WUSTOJ 1327: Lucky Numbers(Java)

    题目链接:1327: Lucky Numbers Description A lucky number is made by the following rules: Given a positive ...

  5. jmu-Java-02基本语法-04-动态数组

    题目: 根据输入的n,打印n行乘法口诀表.需要使用二维字符串数组存储乘法口诀表的每一项,比如存放1*1=1.为了保证程序中使用了二维数组,需在打印完乘法口诀表后使用Arrays.deepToStrin ...

  6. PowerShell自动部署ASP.NET Core程序到 IIS

    Windows PowerShell 是一种命令行外壳程序和脚本环境,使命令行用户和脚本编写者可以利用 .NET Framework的强大功能.有关于更多PowerShell的信息,可参阅百度词条 接 ...

  7. 获取ApplicationContext进而获取Ioc实例方法

    在正常情况下 spring管理的类可直接调用Ioc容器中的实例,但在一些特殊情况下(例如拦截器中获取dao实例),Bean需要实现某个功能,但该功能必须借助于Spring容器才能实现,此时就必须让该B ...

  8. github新建远程仓库初始化记录

    …or create a new repository on the command line echo "# 输出内容" >> README.md git init ...

  9. eclipse 部署Tomcat 只有web-inf webapps没有项目文件

    只有webapps 修改eclipse 默认的 发布位置改为 tomcat的安装位置 eclipse 默认的web-content 改为webroot ,发布后web-inf目录下才会有webroot ...

  10. nginx buffer

    1.错误日志:warn:an upstream response is buffered to a temporary file 解决办法:增加fastcgi_buffers 8 4K;     fa ...