




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Spring与Mybatis整合环境搭建本文主要介绍Spring Mybatis的常用整合方法。目录一、 准备1二、 配置文件21. 数据库连接基本信息 perties 22. 在数据库中创建表与及相应的序列 23. 创建相应的 JavaBean34. 准备表的映射文件 UserTable.xml45. 总酉己置文件 sqlMapConfig.xml46.Spring 酉己置文件 beans.xml57.相应的 DAO67.1 IUserDAO.java67.2UseDAOImpl7三、测试8一、准备1.mybatis-spring-1.1.1.jar 或者更高版本 下载地址:
2、 http:/code.google.Com/p/mybatis/在这个jar包里有spring和mybatis的jar包需要一同导入。如果其他版本没有的话,可以 去他们的官方下载。2.如果需要对数据库操作可能还需要引用到的jar 包有commons-dbcp.jar,commons-pool.jar,commons-io.jar以及对应的数据库马区动,我这里使用的是oracle数据库,所以jar包ojdbc6.jar3.JAVA环境需要安装JRM者JDK版本最好1.6或者以上。设置好环境变量。4.IDE工具(Eclipse当然也可以用其他的)二、配置文件1. 数据库连接基本信息 jdbc.p
3、roperties连接数据库的一些基本信息,放在src下driverClassName= oracle.jdbc.driver.OracleDriverurl= jdbc:oracle:thin::1521:orclusername= adminpassword= adminmaxActive= 255maxIdle= 20maxWait= 100具体的一些信息,你可以根据自己的数据库情况进行一定的修改。2. 在数据库中创建表与及相应的序列Create table usertable(Userid int primary key,Username varchar2(32) n
4、ot null,Password varchar2(32) );Create sequence usertable_seq start with 1 increment by 1 nocahe;Commit;3. 创建相应的JavaBeanpackagecom.springmybatis.po;publicclass UserTableprivateint userId ;private private publicStringuserName ;Stringpassword;UserTable()publicint getUserId()returnuserId ;publicthisvoi
5、d setUserId(.userId = userId;int userId )publicString getUserName()returnuserName ;public void setUserName( String userName )this.userName = userName;publicString getPassword()returnpassword ;void setPassword( String password )publicthis . password = password; 4. 准备表的映射文件 UserTable.xml<? xml vers
6、ion ="1.0" encoding ="UTF-8" ?> <! DOCTYPE mapper PUBLIC "-//DTD Mapper 3.0/EN""/dtd/mybatis-3-mapper.dtd"<mapper namespace ="com.springmybatis.po.UserTable"><resultMaptype ="UserTable"id ="us
7、erMap" >< idproperty< idproperty< idproperty</ resultMap >="userId" column ="userid" ></ id >="userName"column="password"column="username"></ id >="password"></ id ><select id ="getU
8、ser" parameterType ="UserTable"resultMap= "userMap" >select userId,userName from usertable where userId=#userId</ select ><insert id ="addUser" parameterType ="UserTable" > insert into usertable(userid,username,password)values(userid_seq.ne
9、xtval,#userName,#password)</ insert ><update id ="updateUser" parameterType ="UserTable" > update usertable set username=#userName,password=#password where userid=#userId</ update ></ mapper >5. 总配置文件 sqlMapConfig.xml对于熟悉Mybatis这个orm框架的肯定知道,我们一般都需要一个对数据库操作
10、的总配置文件。<? xml version ="1.0" encoding ="UTF-8"?><! DOCTYPE configuration PUBLIC "-//DTD Config 3.0/EN""/dtd/mybatis-3-config.dtd"> <configuration ><!- 别名-><typeAliases ><typeAlias type ="com.spr
11、ingmybatis.po.UserTable"alias ="UserTable" /></ typeAliases >< mappers >= "com/springmybatis/po/UserTable.xml"/><mapper resource </ mappers ></ configuration >6.Spring 酉己置文件 beans.xml<? xml version ="1.0" encoding ="UTF-8&qu
12、ot; ?> <beans xmlns ="/schema/beans"xmlns:xsi ="/2001/XMLSchema-instance"xsi:schemaLocation ="/schema/beans/schema/beans/spring-beans.xsd"><!- config db pr
13、operty file location -><bean id ="propertyConfigurer"class ="org.springframework.beans.factory.config.PropertyPlaceholderCo nfigurer" ><property name ="locations" > <list ><value >perties </ value ></ list ></ property &
14、gt;</ bean ><bean id ="dataSource" destroy-method ="close"class ="mons.dbcp.BasicDataSource"><property name ="driverClassName" ><value >$driverClassName </ value ></ property><propertyname ="url"><value &g
15、t;$u _ </ value ></ property><propertyname ="username"><value >$username </ value > </ property><propertyname ="password"><value > $password </ value > </ property><propertyname ="maxActive"><value >
16、;$maxActive </ value > </ property><propertyname ="maxIdle"><value >$maxIdle </ value > </ property><propertyname ="maxWait"><value >$(maxWait) </ value ></ property >< propertyname ="validationQuery"><
17、;value >select count(1) from Dual</ value ></ property><!- test when get connection -><propertyname ="testOnBorrow"><value >true </ value ></ property><!- test when return connection to pool -><propertyname ="testOnReturn">
18、<value >true </ value ></ property><propertyname ="testWhileIdle"><value >true </ value ></ property></ bean ><!- 创建SqlSessionFactory,同时指定数据源 -><bean id ="sqlSessionFactory"class ="org.mybatis.spring.SqlSessionFactoryB
19、ean">< property name ="dataSource" ref ="dataSource" />< property name = "configLocation"value ="classpath:sqlMapConfig.xml"></ property ></ bean ><bean id ="userDAO" class ="com.springmybatis.dao.UserDAOImpl&q
20、uot;>< property name = "sqlSessionFactory" ref = "sqlSessionFactory" ></ property > </ bean ></ beans >7.相应的DAO7.1 lUserDAO.javapackage com.springmybatis.dao;import com.springmybatis.po.UserTable;public interface/*lUserDAO* add user into db* param userT
21、able necessary user information* return add if succeed*/public boolean addUser(UserTable userTable);/* validate user* param userTable* return*/publicboolean checkUser(UserTable userTable);public boolean updateUser( UserTable userTable );7.2UseDAOImplpackage com.springmybatis.dao;import org.apache.ib
22、atis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import com.springmybatis.po.UserTable;public class UserDAOImpl implements IUserDAOprivate SqlSessionFactory sqlSessionFactory;public SqlSessionFactory getSqlSessionFactory()return sqlSessionFactory;public void setSqlSessionFa
23、ctory( SqlSessionFactory sqlSessionFactory )(this.sqISessionFactory = sqISessionFactory;public boolean addUser( UserTable userTable )(SqlSession session = this.getSqlSessionFactory().openSession();int i1 = session.insert( "addUser", userTable );session.close();return i1 != 0 ;public boolea
24、n updateUser( UserTable userTable )(SqlSession session = this.getSqlSessionFactory().openSession();int i = session.update( "updateUser", userTable );return i != 0;public boolean checkUser( UserTable userTable )(return false;二、测试package com.springmybatis.dao;import org.springframework.beans.BeansException;import o
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- (高清版)DB50∕T 802-2017 渝小吃 腊肉白蜂糕烹饪技术规范
- 2024篮球裁判员法则与试题与答案
- 体育经纪人如何进行高效沟通试题及答案
- 现代农业示范区建设项目可行性研究报告(模板)
- 提升能力的游泳救生员试题及答案
- 2024年体育经纪人模拟测试试题及答案
- 2024年游泳救生员法律责任考题
- 风电项目运营计划可行性研究报告(参考)
- 农业植保员2024年考试应对挑战的试题与答案
- 模具设计师资格考试潜能激发试题及答案
- 2024-2025学年七年级语文下学期期中模拟卷05
- 2025年中国储能检测认证行业市场运行态势及发展趋势预测报告-智研咨询发布
- 诊断与评估课件 第十二节 资赋优异儿童特征及学习资料
- 金店装修施工方案
- 政治薪火相传的传统美德+教案-2024-2025学年统编版道德与法治七年级下册
- 生物泌尿系统的组成课件-+2024-2025学年冀少版生物七年级下册
- 马鞍山职业技术学院马鞍山技师学院招聘笔试真题2024
- 2025年中国协同办公系统行业市场发展前景及发展趋势与投资战略研究报告
- 冷却塔维修施工方案
- 航天发射场智能化-深度研究
- 信息时代背景下班主任提升班级管理工作效率的策略研究
评论
0/150
提交评论