ibatis简单搭建与操作

第一步:搭建ibatis环境。导入相关的jaribatis-sqlmap-2.3.4.726.jar 包,ibatis-2.3.0.677.jar 、mysql-connector-java-5.1.6-bin.jar。

第二步:编写链接数据库的配置文件 jdbc.properties

这是对应Oracle数据库的配置

jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@192.168.0.247:1521:orcl
jdbc.username=qqt
jdbc.password=qqt123


这是对应mysql数据库的配置

driver=mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/jiemaiipi
username=qqt
password=qqt123


第三步:开始写自己的小demo了

编写实体类如下:

package com.ipi.qqt.model;


/**
 * 新表:白名单
 * @author liyintao
 *
 */
public class UserRelationNew {

// 注意这里需要保证有一个无参构造方法,因为包括Hibernate在内的映射都是使用反射的,如果没有无参构造可能会出现问题
public UserRelationNew(){};
private String phoneNumA;//父卡号码
private String phoneNumB;//子卡号码
private String applyNum;//申请号码:1 子卡,2父卡
private String createdTime;//建立时间
private String authorizeTime;//生效时间
private String accuracy;//精度:1 GPS,0 CELLID
private String buildSeqNum;//操作流水号:BOSS、MISC侧传来的交易流水号
private String buildSource;//建立来源:0 BOSS,1 平台
private String busiType;//业务类型:LBMP分配业务代码
private String buildWay;//建立方式:0短信,1语音,2客户端
private String respId;//授权流水号:LBMP异步授权流水号
private String isAuthorize;//授权状态:1 已授权,0 未授权, 2暂停



@Override
public String toString() {
return "UserRelationNew [accuracy=" + accuracy + ", applyNum="
+ applyNum + ", authorizeTime=" + authorizeTime
+ ", buildSeqNum=" + buildSeqNum + ", buildSource="
+ buildSource + ", buildWay=" + buildWay + ", busiType="
+ busiType + ", createdTime=" + createdTime + ", isAuthorize="
+ isAuthorize + ", phoneNumA=" + phoneNumA + ", phoneNumB="
+ phoneNumB + ", respId=" + respId + "]";
}

public String getPhoneNumA() {
return phoneNumA;
}
/** 父卡号码*/
public void setPhoneNumA(String phoneNumA) {
this.phoneNumA = phoneNumA;
}
public String getPhoneNumB() {
return phoneNumB;
}
public void setPhoneNumB(String phoneNumB) {
this.phoneNumB = phoneNumB;
}
public String getApplyNum() {
return applyNum;
}
/** 申请号码:1 子卡,2父卡 */
public void setApplyNum(String applyNum) {
this.applyNum = applyNum;
}
public String getCreatedTime() {
return createdTime;
}

/**建立时间 */
public void setCreatedTime(String createdTime) {
this.createdTime = createdTime;
}
public String getAuthorizeTime() {
return authorizeTime;
}
/**生效时间 */
public void setAuthorizeTime(String authorizeTime) {
this.authorizeTime = authorizeTime;
}
public String getAccuracy() {
return accuracy;
}
/** 精度:1 GPS,0 CELLID*/
public void setAccuracy(String accuracy) {
this.accuracy = accuracy;
}
public String getBuildSeqNum() {
return buildSeqNum;
}
/** 操作流水号:BOSS、MISC侧传来的交易流水号*/
public void setBuildSeqNum(String buildSeqNum) {
this.buildSeqNum = buildSeqNum;
}
public String getBuildSource() {
return buildSource;
}
/** 建立来源:0 BOSS,1 平台 */
public void setBuildSource(String buildSource) {
this.buildSource = buildSource;
}
public String getBusiType() {
return busiType;
}
/** 业务类型:LBMP分配业务代码*/
public void setBusiType(String busiType) {
this.busiType = busiType;
}
public String getBuildWay() {
return buildWay;
}
/** 建立方式:0短信,1语音,2客户端*/
public void setBuildWay(String buildWay) {
this.buildWay = buildWay;
}
public String getRespId() {
return respId;
}
/** 授权流水号:LBMP异步授权流水号*/
public void setRespId(String respId) {
this.respId = respId;
}
public String getIsAuthorize() {
return isAuthorize;
}
/** 授权状态:1 已授权,0 未授权, 2暂停*/
public void setIsAuthorize(String isAuthorize) {
this.isAuthorize = isAuthorize;
}
}



第四步:定义实体类对应的xml文件

UserRelationNew.xml

<?xml version="1.0" encoding="UTF-8" ?>


<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">


<sqlMap namespace="UserRelationNew">
<typeAlias alias="userRelationNew"
type="com.ipi.qqt.model.UserRelationNew" />


<!-- JavaBean对应的表 -->
<resultMap id="UserRelationNewBeanId" class="userRelationNew">
<result property="phoneNumA" column="PHONE_NUM_A" />
<result property="phoneNumB" column="PHONE_NUM_B" />
<result property="applyNum" column="APPLY_NUM" />
<result property="createdTime" column="CREATED_TIME" />
<result property="authorizeTime" column="AUTHORIZE_TIME" />
<result property="accuracy" column="ACCURACY" />
<result property="buildSeqNum" column="BUILD_SEQNUM" />
<result property="buildSource" column="BUILD_SOURCE" />
<result property="busiType" column="BUSI_TYPE" />
<result property="buildWay" column="BUILD_WAY" />
<result property="respId" column="RESP_ID" />
<result property="isAuthorize" column="IS_AUTHORIZE" />
</resultMap>

<!-- 从数据库获取白名单列表 -->
<select id="getuserRelationNew" parameterClass="userRelationNew"
resultMap="UserRelationNewBeanId" >
select 
PHONE_NUM_A,
  PHONE_NUM_B,
  APPLY_NUM,
  CREATED_TIME,
  AUTHORIZE_TIME,
  ACCURACY,
  BUILD_SEQNUM,
  BUILD_SOURCE,
  BUSI_TYPE,
  BUILD_WAY,
  RESP_ID,
  IS_AUTHORIZE
from 
USER_RELATION_NEW
<dynamic prepend="WHERE">
<isNotEmpty prepend="and" property="phoneNumA">
PHONE_NUM_A=#phoneNumA#
</isNotEmpty>
<isNotEmpty prepend="and" property="phoneNumB">
PHONE_NUM_B=#phoneNumB#
</isNotEmpty>
<isNotEmpty prepend="and" property="isAuthorize">
IS_AUTHORIZE=#isAuthorize#
</isNotEmpty>
</dynamic>
</select>

<!--  向白名单表中  添加  白名单信息  --> 
  <insert id="insertuserRelationNew" parameterClass="userRelationNew" >
  insert into USER_RELATION_NEW (
  PHONE_NUM_A,PHONE_NUM_B,APPLY_NUM,CREATED_TIME,AUTHORIZE_TIME,ACCURACY,BUILD_SEQNUM,
BUILD_SOURCE,BUSI_TYPE,BUILD_WAY,RESP_ID,IS_AUTHORIZE
  ) 
  values(
  #phoneNumB#, 
  #phoneNumB#, 
  #applyNum#, 
  sysdate,
  sysdate, 
  #accuracy#, 
  #buildSeqNum#, 
  #buildSource#, 
  #busiType#, 
  #buildWay#, 
  #respId#, 
  #isAuthorize#
)
  </insert> 






</sqlMap>


第五步:编写配置文件:SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">


<sqlMapConfig>
<properties resource="jdbc.properties"/>
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="${jdbc.driver}" />
<property name="JDBC.ConnectionURL" value="${jdbc.url}" />
<property name="JDBC.Username" value="${jdbc.username}" />
<property name="JDBC.Password" value="${jdbc.password}" />
</dataSource>


</transactionManager>


<!-- 这里可以写多个实体的映射文件 -->
<sqlMap resource="persistence/OrderRecordInfo.xml" />
<sqlMap resource="persistence/UserRelationNew.xml" />
</sqlMapConfig>


第六步:编写接口Dao

UserRelationNewDao.java文件(其中只实现了添加跟查询功能,但是一般的接口都写完了)

package com.ipi.qqt.dao;


import java.util.List;


import com.ipi.qqt.model.UserRelationNew;


/**
 * 白名单数据操作
 * @author liyintao
 *
 */
public interface UserRelationNewDao {

/**
* 查询  新白名单 是否存在。不存在返回0
* 对应的参数最多三个  phoneNumA,phoneNumB,isAuthorize这是在xml中定义的select语句
* @param userRelationNew
* @return
*/
public List<UserRelationNew> selectUserRelation(UserRelationNew userRelationNew);

/**
* 添加白名单信息
* @param userRelationNew
* @return  返回是否添加成功
*/
public boolean addUserRelationNew(UserRelationNew userRelationNew);

/**
* 删除白名单信息,条件是根据用户需求删除
* @param userRelationNew
* @return
*/
public boolean deleteUserRelationNewById(UserRelationNew userRelationNew);

/**
* 更新白名单,根据用户发送短信需求更新
* @param userRelationNew
* @return
*/
public boolean updateUserRelationNew(UserRelationNew userRelationNew);

/**
* 根据用户提供信息查询白名单集合
* @param name
* @return 白名单信息列表
*/
public List<UserRelationNew> selectUserRelationNewByName(String name);
}



第七步:实现Dao中的接口 

UserRelationNewDaoImpl.java文件

package com.ipi.qqt.dao.impl;


import java.sql.SQLException;
import java.util.List;


import com.ibatis.sqlmap.client.SqlMapClient;
import com.ipi.qqt.dao.UserRelationNewDao;
import com.ipi.qqt.model.UserRelationNew;
import com.ipi.qqt.utils.SqlMapClientUtil;


public class UserRelationNewDaoImpl implements UserRelationNewDao{


@SuppressWarnings("unchecked")
public List<UserRelationNew> selectUserRelation(UserRelationNew userRelationNew) {
SqlMapClient sc = SqlMapClientUtil.getSqlMapClient();
List<UserRelationNew> users = null;
try {
//getuserRelationNew对应xml中的 id="getuserRelationNew"
users = sc.queryForList("getuserRelationNew",userRelationNew);
} catch (Exception e) {
e.printStackTrace();
System.out.println("数据查询失败!!!");
}
return users;
}



public static void main(String[] args) {
UserRelationNewDaoImpl dao = new UserRelationNewDaoImpl();
//添加表数据
UserRelationNew addUser= new UserRelationNew();
addUser.setPhoneNumA("13838573895");
addUser.setPhoneNumB("13811111111");
addUser.setApplyNum("2");
//addUser.setCreatedTime("");
//addUser.setAuthorizeTime("");
addUser.setAccuracy("0");
addUser.setBuildSeqNum("liushuihao");
addUser.setBuildSource("1");
addUser.setBusiType("fpyw");
addUser.setBuildWay("2");
addUser.setRespId("110");
addUser.setIsAuthorize("2");
boolean add = dao.addUserRelationNew(addUser);
System.out.println(add);
//查询表数据
UserRelationNew user= new UserRelationNew();
user.setPhoneNumA("13838573895");//返回值是 1
user.setIsAuthorize("1");//这相当于两个条件限制

//user.setPhoneNumA("18842364323");//返回值是2
System.out.println(dao.selectUserRelation(user).size()+"**********************");
}




public boolean addUserRelationNew(UserRelationNew userRelationNew) {
Object object = null;
boolean flag = false;
try {
SqlMapClient sc = SqlMapClientUtil.getSqlMapClient();
object = sc.insert("insertuserRelationNew", userRelationNew);
System.out.println("添加白名单数据信息的返回值:" + object);
} catch (SQLException e) {
e.printStackTrace();
}
if (object != null) {
flag = true;
}
return flag;
}




public boolean deleteUserRelationNewById(UserRelationNew userRelationNew) {
// TODO Auto-generated method stub
return false;
}




public List<UserRelationNew> selectUserRelationNewByName(String name) {
// TODO Auto-generated method stub
return null;
}




public boolean updateUserRelationNew(UserRelationNew userRelationNew) {
// TODO Auto-generated method stub
return false;
}
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值