简单的来说,Spring主要用于在业务层(当然spring也有数据库交互的模块,个人觉得spring在这方面有一点不如mybatis),而mybatis主要用于数据持久化,在一个完整的项目中无论是业务代码,还是与数据库交互部分的代码缺一不可,下边我来说一下spring和mybatis整合方法。

1.导入依赖:(spring核心依赖包这里就不写了)

  

        <dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency> <!--spring与Mybatis整合包-->
      <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!--jdbc依赖和数据源,这里我是用的是c3p0提供的,还有dbcp,和spring自带的-->
       <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
<!--托管事务的依赖-->

2.创建数据表

3.编写java代码

实体类

 public class Account {
private int accountid;
private String accountname;
private Double accountmonkey;
//省略set、get方法
}

DAO层接口

//DAO层接口,不写实现类。
public interface AccountDao {
List<Account>getAll();//查询数据库中所有信息
}

Service层接口

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

Service层实现类

 public class AccountServiceImpl implements AccountService {
//注入dao接口实例,省略set、get方法
private AccountDao dao;
@Override
public List<Account> getAll() {
return dao.getAll();
}
}

4.数据库连接参数(database.properties)

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3307/account
jdbc.username=root
jdbc.password=root

5.核心配置文件(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" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--引入外部配置文件,这里引入的是保存数据库连接参数的database.properties文件-->
<context:property-placeholder location="classpath:database.properties"/> <!--引入c3p0数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--Mybatis核心配置-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置数据源-->
<property name="dataSource" ref="dataSource"/>
<!--小配置文件的目录-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<!--实体类别名-->
<property name="typeAliasesPackage" value="com.cn.entity"/>
</bean>
<!--mapper代理对象,生成dao动态代理,也就是说每多一个DAO接口,就要创建一个这样的节点-->
<bean id="accountDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!--将sqlSessionFactory注入-->
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<!--接口注入-->
<property name="mapperInterface" value="com.cn.dao.AccountDao"/>
</bean>
<!--service层bean实例-->
<bean id="service" class="com.cn.service.impl.AccountServiceImpl">
<property name="dao" ref="accountDao"></property>
</bean>
<!--mapper文件扫描,看情况使用(我没用过,应该是在sqlSessionFactory中没有做配置时可以这样配置)-->
<!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="/com.cn.dao"/>
</bean>-->
<!--开启事务,目前没啥用,就是个习惯-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> </beans>

哦还有一个小配置文件

<?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="com.cn.dao.accountDao">
<select id="getAll" resultType="Account">
select * from account
</select>
</mapper>

大功告成,开始测试

 public class App
{
public static void main( String[] args )
{
ApplicationContext context = new ClassPathXmlApplicationContext("/applicationContext.xml");
AccountService bean = context.getBean(AccountService.class);
System.out.println(bean.getAll());
}
}

下期使用存java配置方式进行配置

  

Spring+Mybais整合的更多相关文章

  1. struts2 spring mybatis 整合(test)

    这几天搭了个spring+struts2+mybatis的架子,练练手,顺便熟悉熟悉struts2. 环境:myEclipse10+tomcat7+jdk1.6(1.8的jre报错,所以换成了1.6) ...

  2. 【Java EE 学习 79 下】【动态SQL】【mybatis和spring的整合】

    一.动态SQL 什么是动态SQL,就是在不同的条件下,sql语句不相同的意思,曾经在“酒店会员管理系统”中写过大量的多条件查询,那是在SSH的环境中,所以只能在代码中进行判断,以下是其中一个多条件查询 ...

  3. 3.springMVC+spring+Mybatis整合Demo(单表的增删该查,这里主要是贴代码,不多解释了)

    前面给大家讲了整合的思路和整合的过程,在这里就不在提了,直接把springMVC+spring+Mybatis整合的实例代码(单表的增删改查)贴给大家: 首先是目录结构: 仔细看看这个目录结构:我不详 ...

  4. 由“单独搭建Mybatis”到“Mybatis与Spring的整合/集成”

    在J2EE领域,Hibernate与Mybatis是大家常用的持久层框架,它们各有特点,在持久层框架中处于领导地位. 本文主要介绍Mybatis(对于较小型的系统,特别是报表较多的系统,个人偏向Myb ...

  5. spring+websocket整合

    java-websocket的搭建非常之容易,没用框架的童鞋可以在这里下载撸主亲自调教好的java-websocket程序: Apach Tomcat 8.0.3+MyEclipse+maven+JD ...

  6. Hibernate 与 Spring 的整合

    刚刚学习了hibernate和Spring的整合,现在来总结一下. 以实现一个功能为例,与大家分享一下整个过程. 需要实现的功能:建立一个Person类,该类包括name,sex,age,birtha ...

  7. Spring与Struts2整合VS Spring与Spring MVC整合

    Spring与Struts2整合,struts.xml在src目录下 1.在web.xml配置监听器 web.xml <!-- 配置Spring的用于初始化ApplicationContext的 ...

  8. struts2+hibernate-jpa+Spring+maven 整合(1)

    1.0.0 struts2 与 spring 的整合. 1.1.0 新建maven工程 , 编写pom.xml ,这里只需要简单的添加 一个组件就够了: 在myeclipse 生成的pom.xml 添 ...

  9. ASP.NET MVC Spring.NET 整合

    请注明转载地址:http://www.cnblogs.com/arhat 在整合这三个技术之前,首先得说明一下整合的步骤,俗话说汗要一口一口吃,事要一件一件做.同理这个三个技术也是.那么在整合之前,需 ...

随机推荐

  1. Django模型基础(三)——关系表的数据操作

    模型之间可以有三种表关系,即一对一,一对多和多对多.表关联之间的数据操作在Django中可以很方便的操作到.在模型中,表关联的字段类型是关联表的实例,而不是字段本身类型.关联字段在数据库中会在其后补上 ...

  2. h5-localStorage储存的使用

    <!-- localStorage的使用: 1.存储的内容大概20mb 2.不同浏览器不能共享数据,但是在同意浏览器的不同窗口中可以共享数据 3.永久生效,他的数据是储存在硬盘上,并不会随着页面 ...

  3. DOCKER 学习笔记6 WINDOWS版尝鲜

    前言 经过前两节的学习,我们已经可以在Dokcer 环境下部署基本的主流环境有: Springboot 后端 MYSQL 持久化数据 以及Nginx 作为反向代理 虽说服务器上面的也没啥不好,但是毕竟 ...

  4. 深入分析Java反射(七)-简述反射调用的底层实现

    前提 Java反射的API在JavaSE1.7的时候已经基本完善,但是本文编写的时候使用的是Oracle JDK11,因为JDK11对于sun包下的源码也上传了,可以直接通过IDE查看对应的源码和进行 ...

  5. Struts 2 的常规配置

    Struts 2 的默认配置文件是struts.xml,该文件应该放在Web应用的类加载路径下,通常就是放在WEB-INF/classes路径下. struts.xml文件的最大作用是配置Action ...

  6. 有几个水洼(DFS)

    #include <iostream> #include<cstdio> using namespace std; #define maxn 105 char field[ma ...

  7. Django专题-ugettext_lazy

    标准翻译 使用函数 ugettext() 来指定一个翻译字符串. 作为惯例,使用短别名 _ 来引入这个函数以节省键入时间. 在下面这个例子中,文本 "Welcome to my site&q ...

  8. Spring-Boot 访问外部接口的几种方案总结

    一.简介 在Spring-Boot项目开发中,存在着本模块的代码需要访问外面模块接口,或外部url链接的需求,针对这一需求目前存在着三种解决方案,下面将对这三种方案进行整理和说明. 二.Spring- ...

  9. App 性能测试

    app常见性能测试点: https://blog.csdn.net/xiaomaoxiao336368/article/details/83547318

  10. swoole使用内存

    //swoole直接操作系统的内存 单线程每秒可执行三百万次 主要用于进程间的数据通信 $swoole_table = new swoole_table(1024);//1024为内创建内存对象所能存 ...