Spring MyBatis整合
更新时间:2023-09-02 18:29:01 阅读量: 教育文库 文档下载
- spring推荐度:
- 相关推荐
Spring+MyBatis整合教程
一、添加需要的jar包
1加入Spring jar包
(注意:在这里需要添加Spring3.X以上版本,Spring2.5不支持Mybatis整合)
我在这里添加添加Spring-3.1.0版本
2、添加mybatis jar和mybatis-spring jar包mybatis-spring.jar是mybatis
提供的一个jar包
3、在这里使用DBCP数据源
4、添加驱动包这里使用mysql数据库为例子
添加junit-4.11测试包
添加完成之后的jar截图如下:
二、添加配置文件
主要需要以下配置文件
Jdbc.properties:配置jdbc连接数据库的相关属性
Log4j.properties:log4j日志工具的配置文件
Spring-mybatis.xml:spring+mybatis整合的主要配置文件
Mybatis.xml:mybatis主配置文件
Jdbc.properties文件
配置了连接数据需要的一些选项
我们以映射一个User对象为例子
1、建立实体并且生成相应的getter setter方法
public class User{
private Integer id;
private String name;
}
2创建映射接口
public interface UserMapper{
/**根据主键查询对象*/
int findById(Integer id);
/**根据主键删除对象*/
int deleteById(Integer id);
/**将对象更新到数据库*/
int update(User user);
/**新增加对象到数据库*/
int insert(User user);
}
3、在同一个包下面创建映射文件
UserMapper.xml(文件名字与映射接口名字保持一致)
这里需要注意的映射语句id属性时候名称要与映射接口名称保持一致<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC"-//http://www.77cn.com.cn//DTD Mapper 3.0//EN" "http://www.77cn.com.cn/dtd/mybatis-3-mapper.dtd">
<mapper namespace="http://www.77cn.com.cnerMapper">
<resultMap id="BaseResultMap"type="User">
<id column="id"property="id"jdbcType="BIGINT"/>
<result column="NAME"property="name"jdbcType="VARCHAR"/>
</resultMap>
<sql id="Base_Column_List">
id,NAME
</sql>
<select id="findById"
resultMap="BaseResultMap"parameterType="http://www.77cn.com.cnng.Integer"> select
<include refid="Base_Column_List"/>
from user
where id=#{id,jdbcType=BIGINT}
</select>
<delete id="deleteById"parameterType="http://www.77cn.com.cnng.Integer">
delete from user
where id=#{id,jdbcType=BIGINT}
</delete>
<update id="update"parameterType="User">
update user
set NAME=#{name,jdbcType=VARCHAR}
where id=#{id,jdbcType=BIGINT}
</update>
<insert id="insert"parameterType="User">
insert into user(id,NAME)
values(#{id,jdbcType=BIGINT},#{name,jdbcType=VARCHAR})
</insert>
</mapper>
4、好了现在我们的映射文件写好了下面开始配置mybatis主配置也就是
Mybatis.xml这个配置文件主要是用来设置mybatis运行时候一些全局的配置
Spring和Mybatis整合时候其实这个主配置文件的作用也不太大,要不要都可以,因为与Spring整合的话就是要它的控制权交给spring容器来管理,在这里我们添上这个主配置,再后面Spring中引入就行了,一般就用两个配置:
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC"-//http://www.77cn.com.cn//DTD Config 3.0//EN" "http://www.77cn.com.cn/dtd/mybatis-3-config.dtd">
<configuration>
<!---这个配置是给后面映射的类取别名,在后面使用就很方便-->
<typeAliases>
<typeAlias type="http://www.77cn.com.cner"alias="User"/>
</typeAliases>
<!--这个配置是将映射文件加入进去-->
<mappers>
<mapper resource="com/ib/mapper/UserMapper.xml"/> </mappers>
</configuration>
5、下面我们要进行Spring的核心配置
Spring-Mybatis.xml文件
<?xml version="1.0"encoding="UTF-8"?>
<beans xmlns="http://www.77cn.com.cn/schema/beans"
xmlns:xsi="http://www.77cn.com.cn/2001/XMLSchema-instance"
xmlns:context="http://www.77cn.com.cn/schema/context"
xmlns:tx="http://www.77cn.com.cn/schema/tx"
xsi:schemaLocation="http://www.77cn.com.cn/schema/beans http://www.77cn.com.cn/schema/beans/spring-beans-2.5.xsd
http://www.77cn.com.cn/schema/context
http://www.77cn.com.cn/schema/context/spring-context-2.5.xsd http://www.77cn.com.cn/schema/tx
http://www.77cn.com.cn/schema/tx/spring-tx-2.5.xsd"> <!--导入外部的properties文件-->
<context:property-placeholder
location="classpath:jdbc.properties"/>
这里我们的数据源使用的dbcp
<!--1、配置数据源-->
<bean id="dataSource"
class="http://www.77cn.com.cnmons.dbcp.BasicDataSource">
<property name="driverClassName"value="${jdbc_driverClassName}"/> <property name="url"value="${jdbc_url}"/>
<property name="username"value="${jdbc_username}"/>
<property name="password"value="${jdbc_password}"/> </bean>
<!--2、配置sqlSessionFactory-->
<bean id="sqlSessionFactory"
Class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource"ref="dataSource"/>
<!--注意这里:这里是将mybatis的主配置文件读取进来-->
<property name="configLocation"value="classpath:mybatis.xml" />
<!--这里如果我们不需要配置mybatis主配置文件的话也可以通过以下配置将文件自动扫描-->
<!--<property name="mapperLocations"value ="classpath:com/ib/mapper/*.xml"/>-->
</bean>
</beans>
至此:我们可以添加测试类进行测试:
public class TestUser{
private SqlSessionFactory sessionFactory;
@Before
public void before(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-mybatis.xml");
sessionFactory=(SqlSessionFactory) applicationContext.getBean("sqlSessionFactory");
}
@Test
public void testUserCDUI(){
/*获得session获得的这个session是默认提交事务*/
SqlSession session=sessionFactory.openSession(true);
/*获取User对象的映射器*/
UserMapper userMapper=session.getMapper(UserMapper.class);
//测试添加一个对象
User user=new User();
user.setId(1);
user.setName("敏敏");
userMapper.insert(user);
}
}
我们可以看到对象已经保存到数据库,其它方法大家可以去测试
总结:
通过自己学习,与大家分享这种心情的喜悦的,小弟能力有限,有什么不足的,希望大家多多指出!谢谢!
正在阅读:
Spring MyBatis整合09-02
QQ名称,qq名称四字(三篇)02-14
03.合同专用条款12-04
2019年诗词知识竞赛题及答案12-26
2019年黑木耳选择和鉴别窍门10-18
(整理)测量学试题及答案12-29
专题07 完形填空-2019年高考题和高考模拟题英语分项版汇编 Word版含解析01-10
2019年乡镇精准扶贫工作调研报告11-14
2019个人对照检查材料08-22
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- 整合
- MyBatis
- Spring
- 吉林大学2009级计算机学院《离散数学II》试题(A)
- 人教版小学四年级体育下册全册教案
- 【复习必做】2020八年级物理上册 3.5奇妙的透镜教案 (新版)粤教沪版
- 中国统计年鉴2014分地区城镇居民人均可支配收入
- 2015-2020年中国芳纶市场调研与投资方向研究报告
- 浅论私营企业员工激励机制及调整策略
- 高中新课标推荐背诵72篇古诗文理解性默写之报任安书(节选)
- 郑州治疗甲状腺好的医院
- 社会治安综合治理工作目标管理责任书
- 电路板(PCBA)外协加工要求
- 宁夏省2016年上半年内科学正(副)中级职称模拟试题
- 2010年社会工作者考试初级社会工作综合能力模拟冲刺试题及答案
- hTERT、p53在胃癌及癌前病变中的表达及相关性研究
- 怎样加强中学生思想道德建设
- 河南省三门峡市陕州中学2016届高三上学期第一次月考生物试卷【解析版】
- 常见急症及意外伤害现场救护
- 家乐福调查报告
- 超市门店目标管理与绩效考核制度
- 2017上海交大翻译硕士学费多不多
- 2014高考英语短文改错抓分练习(1)