Spring+Mybais整合
简单的来说,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整合的更多相关文章
- struts2 spring mybatis 整合(test)
这几天搭了个spring+struts2+mybatis的架子,练练手,顺便熟悉熟悉struts2. 环境:myEclipse10+tomcat7+jdk1.6(1.8的jre报错,所以换成了1.6) ...
- 【Java EE 学习 79 下】【动态SQL】【mybatis和spring的整合】
一.动态SQL 什么是动态SQL,就是在不同的条件下,sql语句不相同的意思,曾经在“酒店会员管理系统”中写过大量的多条件查询,那是在SSH的环境中,所以只能在代码中进行判断,以下是其中一个多条件查询 ...
- 3.springMVC+spring+Mybatis整合Demo(单表的增删该查,这里主要是贴代码,不多解释了)
前面给大家讲了整合的思路和整合的过程,在这里就不在提了,直接把springMVC+spring+Mybatis整合的实例代码(单表的增删改查)贴给大家: 首先是目录结构: 仔细看看这个目录结构:我不详 ...
- 由“单独搭建Mybatis”到“Mybatis与Spring的整合/集成”
在J2EE领域,Hibernate与Mybatis是大家常用的持久层框架,它们各有特点,在持久层框架中处于领导地位. 本文主要介绍Mybatis(对于较小型的系统,特别是报表较多的系统,个人偏向Myb ...
- spring+websocket整合
java-websocket的搭建非常之容易,没用框架的童鞋可以在这里下载撸主亲自调教好的java-websocket程序: Apach Tomcat 8.0.3+MyEclipse+maven+JD ...
- Hibernate 与 Spring 的整合
刚刚学习了hibernate和Spring的整合,现在来总结一下. 以实现一个功能为例,与大家分享一下整个过程. 需要实现的功能:建立一个Person类,该类包括name,sex,age,birtha ...
- Spring与Struts2整合VS Spring与Spring MVC整合
Spring与Struts2整合,struts.xml在src目录下 1.在web.xml配置监听器 web.xml <!-- 配置Spring的用于初始化ApplicationContext的 ...
- struts2+hibernate-jpa+Spring+maven 整合(1)
1.0.0 struts2 与 spring 的整合. 1.1.0 新建maven工程 , 编写pom.xml ,这里只需要简单的添加 一个组件就够了: 在myeclipse 生成的pom.xml 添 ...
- ASP.NET MVC Spring.NET 整合
请注明转载地址:http://www.cnblogs.com/arhat 在整合这三个技术之前,首先得说明一下整合的步骤,俗话说汗要一口一口吃,事要一件一件做.同理这个三个技术也是.那么在整合之前,需 ...
随机推荐
- Java 知识点(一)
博主对 Java知识点的整理基于 c语言,整理内容为 Java的重点及与 c语言的差异点或编程通要知识点.水平有限,欢迎指正.(参考书籍<Java 核心技术 卷Ⅰ>) Java 的类名:名 ...
- Vue 源码学习(1)
概述 我在闲暇时间学习了一下 Vue 的源码,有一些心得,现在把它们分享给大家. 这个分享只是 Vue源码系列 的第一篇,主要讲述了如下内容: 寻找入口文件 在打包的过程中 Vue 发生了什么变化 在 ...
- MySQL--事务,隔离性和隔离级别
事务 事务就是一组数据库操作,要么全部执行成功,要么全部执行失败,在MySQL中,事务是依靠存储引擎层实现的. ACID(Atomicity,Consistency,Isolation,Durabil ...
- CocoaPods-Alcatraz插件
Alcatraz:Xcode的插件管理工具,可通过它添加CocoaPods插件 下载地址:https://github.com/alcatraz/Alcatraz 建议: 不提倡通过终端命令下载Alc ...
- day64-html-form表单
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...
- OracleXE 11g user莫名过期
参考大大的 环境sqlplus 1.sysdba登陆 SQL>conn sys as sysdba password 2.查看用户状态 SQL>select username,accoun ...
- Linux-exec族函数
1.为什么需要exec族函数 (1).fork子进程是为了执行新程序(fork创建子进程后,子进程和父进程同时被OS调度执行,因此子程序可以单独的执行一个程序,这样程序宏观上将会和父进程程序同时进行) ...
- 把cifar数据转换为图片
参考 https://gist.github.com/fzliu/64821d31816bce595a4bbd98588b37f5 """ make_cifar10.py ...
- Git与IDEA集成
软件配置: 系统版本:Windows10 JDK版本:1.8 Git版本:2.19.1 IDEA版本:2016.3 Maven版本:3.5.4 Git安装: Git下载地址:https://git-s ...
- 网络安全与CTF在线学习资源网站
http://www.sec-wiki.com/skill/ 安全技能(里面渗透逆向编程都有介绍) http://blog.knownsec.com/Knownsec_RD_Checklist/ 知道 ...