简单的来说,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. Hadoop的伪分布式安装和部署流程

    在opt目录创建install software test other四个目录 /opt/installed #安装包/opt/software #软件包/opt/other #其他/opt/test ...

  2. [Algo] 118. Array Deduplication IV

    Given an unsorted integer array, remove adjacent duplicate elements repeatedly, from left to right. ...

  3. 吴裕雄--天生自然GPU配置:查看本机显卡是否支持GPU

    NVIDIA的GF8级别以上的显卡才能支持physx物理加速(即GPU加速),ATI的显卡不支持. 打开:设备管理器,点击:显示适配器

  4. 03 Mybatis:01.Mybatis课程介绍及环境搭建&&02.Mybatis入门案例

    mybatis框架共四天第一天:mybatis入门 mybatis的概述 mybatis的环境搭建 mybatis入门案例 -------------------------------------- ...

  5. Half of UK 10-year-olds own a smartphone

    1. preposition n. 介词  pronoun  n. 代词 2. despite /preposition. (1) used to say that something happens ...

  6. Postgresql的导表

    背景 前面已经介绍了常用的备份与恢复了,接下来介绍一下导表. 正文 很多情况,会有把数据导出的需求,轻重缓急总会有特别紧急的情况,但是又不是专业干db的人,还是记录下来,以防不时之需. 针对于导表,个 ...

  7. Python map filter reduce enumerate zip 的用法

    map map(func, list) 把list中的数字,一个一个运用到func中,常和lambda一起用. nums = [1, 2, 3, 4, 5] [*map(lambda x: x**2, ...

  8. 使用GitHub管理Repository

    对已有的Repository进行修改 将已有的项目克隆到本地 git clone https://github.com/username/project-name 或者同步已经下载的项目 git pu ...

  9. linux中ftp中文名乱码问题

    问题触发环境 1. java中使用org.apache.commons.net.ftp.FTPClient包 2. 通过chrome浏览器的file标签上传文件 3. 在windows上部署的File ...

  10. 4. 监控利器nagios手把手企业级实战第三部

    1.nagios图形监控显示和管理服务器 虽然能显示,能报警.但是我们企业工作中需要一个历史趋势图. nagios只开放核心,插件是单独的形式,图像也一样,是插件或者整合的方式.所以可能看起来很多,这 ...