Java Web开发中Spring+MyBatis框架的简单搭建
这里使用的eclipse,首先创建一个动态web项目。
1、导入Spring IOC、AOP、DAO、dbcp、dbdrive、mybatis.jar 、 mybatis-spring.jar 本人使用的jar包和版本如下:
aopalliance.jar
aspectjweaver.jar
commons-dbcp-1.4.jar
commons-logging.jar
commons-pool-1.5.6.jar
mybatis-3.2.5.jar
mybatis-spring-1.2.2.jar
mysql-connector-java-5.1.7-bin.jar
spring-aop-4.1.6.RELEASE.jar
spring-aspects-4.1.6.RELEASE.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
spring-jdbc-4.1.6.RELEASE.jar
spring-tx-4.1.6.RELEASE.jar
如果需要也可以导入springMVC的两个jar包
spring-web-4.1.6.RELEASE.jar
spring-webmvc-4.1.6.RELEASE.jar
2、将mybatis的主配置文件迁移到Spring的application.xml中
我的application模板如下:
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
</beans>
在里面首先配置dataSource和SqlSessionFactoryBean配置如下:
<!-- 注入连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="root"></property>//必须写value
<property name="password" value="root"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mydb1"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>//不能写成driver
</bean>
<!-- 注入SqlSessionFactoryBean class可以在test类中输入,alt+/ 可以提示,方便找出路径 -->
<bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定连接资源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 指定映射文件 -->
<property name="mapperLocations" value="classpath:com/***/mapper/*.xml"></property>//文件的路径
</bean>
然后写一个测试类,测试SqlSessionFactory是否可以加载
主体代码如下:
public class TestSqlSessionFactory {
@Test
public void test1(){
ApplicationContext ac =
new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println(ac.getBean("ssf"));
}
}
无误证明配置信息正确,可以接着操作了。
接着配置mapperFactoryBean
配置如下:
<bean id="mapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.xdl.mapper.EmpMapper"></property>
<property name="sqlSessionFactory" ref="ssf"></property>
</bean>
配置完成
3、mybatis中的 mapper映射文件和mapper接口 和原来书写一样
映射文件模板如下
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.xdl.mapper.EmpMapper">
<!-- 查找所有员工 -->
<select id="findAll" resultType="com.xdl.bean.Emp">
select * from emp
</select>
</mapper>
接口如下
package com.***l.mapper;
import java.util.List;
import com.xdl.bean.Emp;
public interface EmpMapper {
List<Emp> findAll();
}
4、从spring 容器中获取Mapper映射器的实现类,通过接口名获取
简单代码如下;
ApplicationContext ac =
new ClassPathXmlApplicationContext("applicationContext.xml");
EmpMapper em = ac.getBean("mapper",EmpMapper.class);
System.out.println(em.findAll());
至此,一个简单的spring+mybatis整合起来了
Java Web开发中Spring+MyBatis框架的简单搭建的更多相关文章
- Java Web开发SpringMVC和MyBatis框架开发环境搭建和简单有用
1.下载SpringMVC框架架包,下载地址: 点击下载 点击打开地址如图所看到的.点击下载就可以 然后把相关的jar拷贝到lib下导入 2.MyBatis(3.4.2)下载 X-Amz-Algori ...
- Java Web开发中路径问题小结
Java Web开发中,路径问题是个挺麻烦的问题,本文小结了几个常见的路径问题,希望能对各位读者有所帮助. (1) Web开发中路径的几个基本概念 假设在浏览器中访问了如下的页面,如图1所示: 图1 ...
- Java Web 开发中路径相关问题小结
Java Web开发中路径问题小结 (1) Web开发中路径的几个基本概念 假设在浏览器中访问了如下的页面,如图1所示: 图1 Eclipse中目录结构如图2所示: 图2 那么针对这个站点的几个基本概 ...
- Java Web开发中MVC设计模式简介
一.有关Java Web与MVC设计模式 学习过基本Java Web开发的人都已经了解了如何编写基本的Servlet,如何编写jsp及如何更新浏览器中显示的内容.但是我们之前自己编写的应用一般存在无条 ...
- java web开发中常用的协议的使用和java-web 常见的缓冲技术
一.DNS协议 作用将域名解析为IP 类似于我们只需要知道中央一台,中央二台,而不需要知道它的频率,方便记忆. java dns 域名解析协议实现 1 域名解析,将域名可转换为ip地址InetAd ...
- Java Web开发中路径问题小结(getRequestUrl getContextUrl getServletUrl)
看以博客感觉不错,分享一下http://www.cnblogs.com/tianguook/archive/2012/08/31/2665755.html (1) Web开发中路径的几个基本概念 假设 ...
- Java Web开发中的名词解释
1.JVM Java虚拟机,class文件的运行时环境,就好比软件运行在操作系统一样,java要运行在JVM中才行,这也是Java之所以支持扩平台的基础. 2.Servlet/JSP 是满足一定接口需 ...
- JAVA WEB开发中的资源国际化
为什么要国际化? 不同国家与地区语言,文化,生活习惯等差异.在数字,时间,语言,货币,日期,百分数等的不同. 两个名词: I18N:即资源国际化,全称为Internationalization,因为首 ...
- java web开发中的奇葩事web.xml中context-param中的注释
同事提交了代码.结果除同事之外,其他人全部编译报错.报错说web.xml中配置的一个bean 没有定义.按照报错提示,各种找,无果. 由于代码全部都是提交到svn主干,之前也没有做过备份,只能一步一步 ...
随机推荐
- 对象级别锁 vs 类级别锁 – Java
同步针对的是多线程.同步的方法或代码块同时只能由一个线程执行. Java支持多线程来执行.这可能会导致两个或多个线程访问同一个字段或对象.同步是一个使所有并发执行的线程同步的过程.同步避免了由于共享内 ...
- Vulkan Tutorial 21 Staging buffer
操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 Introduction 顶点缓冲区现在已经可以正常工作,但相比于显卡内部读取数据, ...
- HTML4,HTML5,XHTML 之间有什么区别?
原始日期:2014-10-25 14:12 我来从HTML的历史谈谈他们3者的区别.在HTML的早期发展中,W3C成立之前,很多标准的制定都是在浏览器的开发者们互相讨论的情况下完成的,比如HTML 2 ...
- mysql的my.ini文件详解
mysql数据库在配置时包含很多信息:端口号,字符编码,指定根路径 basedir,指定数据存放的路径等信息 mysql的字体编码分为两种: 服务器编码 客户端输入的编码 通常服务器的编码都是utf- ...
- org.apache.commons.lang3 的随机数生成
apache org.apache.commons.lang3 的随机数生成工具,方便使用. String a12 = RandomStringUtils.random(4, "012345 ...
- 投票系统 & js脚本简单刷票
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Java IO设计模式(装饰模式与适配器模式)
01. 装饰模式 1. 定义 Decorator装饰器,就是动态地给一个对象添加一些额外的职责,动态扩展,和下面继承(静态扩展)的比较.因此,装饰器模式具有如下的特征: 它必须持有一个被装饰的对象(作 ...
- hdoj 1251 字典树||map
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- CentOS7 下使用YUM安装 MySQL5.7
于2015年10月19日(美国时间),Oracle公司发布了开源数据库MySQL的最新版本5.7.到现在已有将近3年之久,经过这几年的改进,MySQL5.7性能最高可达前一个版本的3倍,现在官网的最新 ...
- STL的使用和背后数据结构
STL(Standard Template Library即,模板库)包括六个部分:容器(containers).迭代器(iterators).空间配置器(allocator).配接器(adapter ...