新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
近来打算开发一个技术框架,并应用此框架开发出一个论坛,开始打算用sturts2+spring+hibernate+jquery为基础,然后再进行封装,最后形成技术框架,但通过此网站获知mybatis比hibernate更灵活,实用。因此开始逐步接触mybatis。
创新互联公司专注于安塞企业网站建设,自适应网站建设,商城开发。安塞网站建设公司,为安塞等地区提供建站服务。全流程按需策划设计,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务
以下是mybatis3.0的开发实例,使用的java环境jdk5.0,ide为eclipse3.7,数据库是oracle11g。
1.数据环境配置
Xml代码
Xml代码
2.数据库表映射(DM_MYDWT)XML文件
Xml代码
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
javaType="String" />
MYDWT_DM, MYDWT_MC, YXBZ, XYBZ
parameterType="java.lang.String">
select
from EASSEN.DM_MYDWT
where MYDWT_DM = #{mydwtDm,jdbcType=CHAR}
flushCache="true">
delete from EASSEN.DM_MYDWT
where MYDWT_DM =
#{mydwtDm,jdbcType=CHAR}
insert into EASSEN.DM_MYDWT (MYDWT_DM, MYDWT_MC, YXBZ,
XYBZ)
values
(#{mydwtDm,jdbcType=CHAR}, #{mydwtMc,jdbcType=VARCHAR},
#{yxbz,jdbcType=CHAR},
#{xybz,jdbcType=CHAR})
flushCache="true">
insert into EASSEN.DM_MYDWT
MYDWT_DM,
MYDWT_MC,
YXBZ,
XYBZ,
#{mydwtDm,jdbcType=CHAR},
#{mydwtMc},
#{yxbz,jdbcType=CHAR},
#{xybz,jdbcType=CHAR},
flushCache="true">
update EASSEN.DM_MYDWT
MYDWT_MC = #{mydwtMc,jdbcType=VARCHAR},
YXBZ = #{yxbz,jdbcType=CHAR},
XYBZ = #{xybz,jdbcType=CHAR},
where MYDWT_DM = #{mydwtDm,jdbcType=CHAR}
flushCache="true">
update EASSEN.DM_MYDWT
set MYDWT_MC =
#{mydwtMc,jdbcType=VARCHAR},
YXBZ = #{yxbz,jdbcType=CHAR},
XYBZ =
#{xybz,jdbcType=CHAR}
where MYDWT_DM = #{mydwtDm,jdbcType=CHAR}
Xml代码
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
javaType="String" />
MYDWT_DM, MYDWT_MC, YXBZ, XYBZ
parameterType="java.lang.String">
select
from EASSEN.DM_MYDWT
where MYDWT_DM = #{mydwtDm,jdbcType=CHAR}
flushCache="true">
delete from EASSEN.DM_MYDWT
where MYDWT_DM =
#{mydwtDm,jdbcType=CHAR}
insert into EASSEN.DM_MYDWT (MYDWT_DM, MYDWT_MC, YXBZ,
XYBZ)
values
(#{mydwtDm,jdbcType=CHAR}, #{mydwtMc,jdbcType=VARCHAR},
#{yxbz,jdbcType=CHAR},
#{xybz,jdbcType=CHAR})
flushCache="true">
insert into EASSEN.DM_MYDWT
MYDWT_DM,
MYDWT_MC,
YXBZ,
XYBZ,
#{mydwtDm,jdbcType=CHAR},
#{mydwtMc},
#{yxbz,jdbcType=CHAR},
#{xybz,jdbcType=CHAR},
flushCache="true">
update EASSEN.DM_MYDWT
MYDWT_MC = #{mydwtMc,jdbcType=VARCHAR},
YXBZ = #{yxbz,jdbcType=CHAR},
XYBZ = #{xybz,jdbcType=CHAR},
where MYDWT_DM = #{mydwtDm,jdbcType=CHAR}
flushCache="true">
update EASSEN.DM_MYDWT
set MYDWT_MC =
#{mydwtMc,jdbcType=VARCHAR},
YXBZ = #{yxbz,jdbcType=CHAR},
XYBZ =
#{xybz,jdbcType=CHAR}
where MYDWT_DM = #{mydwtDm,jdbcType=CHAR}
3.数据库表(DM_MYDWT)java
Java代码
package com.pojo;
import java.io.Serializable;
public class DmMydwt implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1078518054837885063L;
/**
* EASSEN.DM_MYDWT.MYDWT_DM
* @ibatorgenerated 2012-02-05 20:14:30
*/
private String mydwtDm;
/**
* EASSEN.DM_MYDWT.MYDWT_MC
* @ibatorgenerated 2012-02-05 20:14:30
*/
private String mydwtMc;
/**
* EASSEN.DM_MYDWT.YXBZ
* @ibatorgenerated 2012-02-05 20:14:30
*/
private String yxbz;
/**
* EASSEN.DM_MYDWT.XYBZ
* @ibatorgenerated 2012-02-05 20:14:30
*/
private String xybz;
public String getMydwtDm() {
return mydwtDm;
}
public void setMydwtDm(String mydwtDm) {
this.mydwtDm = mydwtDm;
}
public String getMydwtMc() {
return mydwtMc;
}
public void setMydwtMc(String mydwtMc) {
this.mydwtMc = mydwtMc;
}
public String getYxbz() {
return yxbz;
}
public void setYxbz(String yxbz) {
this.yxbz = yxbz;
}
public String getXybz() {
return xybz;
}
public void setXybz(String xybz) {
this.xybz = xybz;
}
}
Java代码
package com.pojo;
import java.io.Serializable;
public class DmMydwt implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1078518054837885063L;
/**
* EASSEN.DM_MYDWT.MYDWT_DM
* @ibatorgenerated 2012-02-05 20:14:30
*/
private String mydwtDm;
/**
* EASSEN.DM_MYDWT.MYDWT_MC
* @ibatorgenerated 2012-02-05 20:14:30
*/
private String mydwtMc;
/**
* EASSEN.DM_MYDWT.YXBZ
* @ibatorgenerated 2012-02-05 20:14:30
*/
private String yxbz;
/**
* EASSEN.DM_MYDWT.XYBZ
* @ibatorgenerated 2012-02-05 20:14:30
*/
private String xybz;
public String getMydwtDm() {
return mydwtDm;
}
public void setMydwtDm(String mydwtDm) {
this.mydwtDm = mydwtDm;
}
public String getMydwtMc() {
return mydwtMc;
}
public void setMydwtMc(String mydwtMc) {
this.mydwtMc = mydwtMc;
}
public String getYxbz() {
return yxbz;
}
public void setYxbz(String yxbz) {
this.yxbz = yxbz;
}
public String getXybz() {
return xybz;
}
public void setXybz(String xybz) {
this.xybz = xybz;
}
}
4.DAO
Java代码
package com.dao;
import com.pojo.DmMydwt;
public interface DmMydwtMapper {
/**
* 根据主键删除
* 参数:主键
* 返回:删除个数
* @ibatorgenerated 2012-02-05 20:14:58
*/
int deleteByPrimaryKey(String mydwtDm);
/**
* 插入,空属性也会插入
* 参数:pojo对象
* 返回:删除个数
* @ibatorgenerated 2012-02-05 20:14:58
*/
int insert(DmMydwt record);
/**
* 插入,空属性不会插入
* 参数:pojo对象
* 返回:删除个数
* @ibatorgenerated 2012-02-05 20:14:58
*/
int insertSelective(DmMydwt record);
/**
* 根据主键查询
* 参数:查询条件,主键值
* 返回:对象
* @ibatorgenerated 2012-02-05 20:14:58
*/
DmMydwt selectByPrimaryKey(String mydwtDm);
/**
* 根据主键修改,空值条件不会修改成null
* 参数:1.要修改成的值
* 返回:成功修改个数
* @ibatorgenerated 2012-02-05 20:14:58
*/
int updateByPrimaryKeySelective(DmMydwt record);
/**
* 根据主键修改,空值条件会修改成null
* 参数:1.要修改成的值
* 返回:成功修改个数
* @ibatorgenerated 2012-02-05 20:14:58
*/
int updateByPrimaryKey(DmMydwt record);
}
Java代码
package com.dao;
import com.pojo.DmMydwt;
public interface DmMydwtMapper {
/**
* 根据主键删除
* 参数:主键
* 返回:删除个数
* @ibatorgenerated 2012-02-05 20:14:58
*/
int deleteByPrimaryKey(String mydwtDm);
/**
* 插入,空属性也会插入
* 参数:pojo对象
* 返回:删除个数
* @ibatorgenerated 2012-02-05 20:14:58
*/
int insert(DmMydwt record);
/**
* 插入,空属性不会插入
* 参数:pojo对象
* 返回:删除个数
* @ibatorgenerated 2012-02-05 20:14:58
*/
int insertSelective(DmMydwt record);
/**
* 根据主键查询
* 参数:查询条件,主键值
* 返回:对象
* @ibatorgenerated 2012-02-05 20:14:58
*/
DmMydwt selectByPrimaryKey(String mydwtDm);
/**
* 根据主键修改,空值条件不会修改成null
* 参数:1.要修改成的值
* 返回:成功修改个数
* @ibatorgenerated 2012-02-05 20:14:58
*/
int updateByPrimaryKeySelective(DmMydwt record);
/**
* 根据主键修改,空值条件会修改成null
* 参数:1.要修改成的值
* 返回:成功修改个数
* @ibatorgenerated 2012-02-05 20:14:58
*/
int updateByPrimaryKey(DmMydwt record);
}
5.mybatis加载
Java代码
package com.db;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;