近来打算开发一个技术框架,并应用此框架开发出一个论坛,开始打算用sturts2+spring+hibernate+jquery为基础,然后再进行封装,最后形成技术框架,但通过此网站获知mybatis比hibernate更灵活,实用。因此开始逐步接触mybatis。
以下是mybatis3.0的开发实例,使用的java环境jdk5.0,ide为eclipse3.7,数据库是oracle11g。
1.数据环境配置
Xml代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "mybatis-3-config.dtd">
<configuration>
<!-- - - - - - - 数据库环境配置- - - - - - - - - -->
<environments default="environments">
<environment id="eassen">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:eassen"/>
<property name="username" value="eassen"/>
<property name="password" value="oracle"/>
</dataSource>
</environment>
</environments>
<!-- - - - - - - -映射文件路径- - - - - - -->
<mappers>
<mapper resource="com/pojo/sql/DmMydwtMapper.xml"/>
</mappers>
</configuration>
Xml代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "mybatis-3-config.dtd">
<configuration>
<!-- - - - - - - 数据库环境配置- - - - - - - - - -->
<environments default="environments">
<environment id="eassen">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:eassen"/>
<property name="username" value="eassen"/>
<property name="password" value="oracle"/>
</dataSource>
</environment>
</environments>
<!-- - - - - - - -映射文件路径- - - - - - -->
<mappers>
<mapper resource="com/pojo/sql/DmMydwtMapper.xml"/>
</mappers>
</configuration>
2.数据库表映射(DM_MYDWT)XML文件
Xml代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.DmMydwtMapper">
<resultMap id="BaseResultMap" type="com.pojo.DmMydwt">
<id column="MYDWT_DM" property="mydwtDm" jdbcType="CHAR" javaType="String" />
<result column="MYDWT_MC" property="mydwtMc" jdbcType="VARCHAR"
javaType="String" />
<result column="YXBZ" property="yxbz" jdbcType="CHAR" javaType="String" />
<result column="XYBZ" property="xybz" jdbcType="CHAR" javaType="String" />
</resultMap>
<sql id="Base_Column_List">
MYDWT_DM, MYDWT_MC, YXBZ, XYBZ
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap"
parameterType="java.lang.String">
select
<include refid="Base_Column_List" />
from EASSEN.DM_MYDWT
where MYDWT_DM = #{mydwtDm,jdbcType=CHAR}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String"
flushCache="true">
delete from EASSEN.DM_MYDWT
where MYDWT_DM =
#{mydwtDm,jdbcType=CHAR}
</delete>
<insert id="insert" parameterType="com.pojo.DmMydwt" flushCache="true">
insert into EASSEN.DM_MYDWT (MYDWT_DM, MYDWT_MC, YXBZ,
XYBZ)
values
(#{mydwtDm,jdbcType=CHAR}, #{mydwtMc,jdbcType=VARCHAR},
#{yxbz,jdbcType=CHAR},
#{xybz,jdbcType=CHAR})
</insert>
<insert id="insertSelective" parameterType="com.pojo.DmMydwt"
flushCache="true">
insert into EASSEN.DM_MYDWT
<trim prefix="(" suffix=")" suffixOverrides=",">
MYDWT_DM,
MYDWT_MC,
YXBZ,
XYBZ,
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
#{mydwtDm,jdbcType=CHAR},
#{mydwtMc},
#{yxbz,jdbcType=CHAR},
#{xybz,jdbcType=CHAR},
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.pojo.DmMydwt"
flushCache="true">
update EASSEN.DM_MYDWT
<set>
<if test="mydwtMc != null">
MYDWT_MC = #{mydwtMc,jdbcType=VARCHAR},
</if>
<if test="yxbz != null">
YXBZ = #{yxbz,jdbcType=CHAR},
</if>
<if test="xybz != null">
XYBZ = #{xybz,jdbcType=CHAR},
</if>
</set>
where MYDWT_DM = #{mydwtDm,jdbcType=CHAR}
</update>
<update id="updateByPrimaryKey" parameterType="com.pojo.DmMydwt"
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}
</update>
</mapper>
Xml代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.DmMydwtMapper">
<resultMap id="BaseResultMap" type="com.pojo.DmMydwt">
<id column="MYDWT_DM" property="mydwtDm" jdbcType="CHAR" javaType="String" />
<result column="MYDWT_MC" property="mydwtMc" jdbcType="VARCHAR"
javaType="String" />
<result column="YXBZ" property="yxbz" jdbcType="CHAR" javaType="String" />
<result column="XYBZ" property="xybz" jdbcType="CHAR" javaType="String" />
</resultMap>
<sql id="Base_Column_List">
MYDWT_DM, MYDWT_MC, YXBZ, XYBZ
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap"
parameterType="java.lang.String">
select
<include refid="Base_Column_List" />
from EASSEN.DM_MYDWT
where MYDWT_DM = #{mydwtDm,jdbcType=CHAR}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String"
flushCache="true">
delete from EASSEN.DM_MYDWT
where MYDWT_DM =
#{mydwtDm,jdbcType=CHAR}
</delete>
<insert id="insert" parameterType="com.pojo.DmMydwt" flushCache="true">
insert into EASSEN.DM_MYDWT (MYDWT_DM, MYDWT_MC, YXBZ,
XYBZ)
values
(#{mydwtDm,jdbcType=CHAR}, #{mydwtMc,jdbcType=VARCHAR},
#{yxbz,jdbcType=CHAR},
#{xybz,jdbcType=CHAR})
</insert>
<insert id="insertSelective" parameterType="com.pojo.DmMydwt"
flushCache="true">
insert into EASSEN.DM_MYDWT
<trim prefix="(" suffix=")" suffixOverrides=",">
MYDWT_DM,
MYDWT_MC,
YXBZ,
XYBZ,
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
#{mydwtDm,jdbcType=CHAR},
#{mydwtMc},
#{yxbz,jdbcType=CHAR},
#{xybz,jdbcType=CHAR},
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.pojo.DmMydwt"
flushCache="true">
update EASSEN.DM_MYDWT
<set>
<if test="mydwtMc != null">
MYDWT_MC = #{mydwtMc,jdbcType=VARCHAR},
</if>
<if test="yxbz != null">
YXBZ = #{yxbz,jdbcType=CHAR},
</if>
<if test="xybz != null">
XYBZ = #{xybz,jdbcType=CHAR},
</if>
</set>
where MYDWT_DM = #{mydwtDm,jdbcType=CHAR}
</update>
<update id="updateByPrimaryKey" parameterType="com.pojo.DmMydwt"
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}
</update>
</mapper>
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;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class ConnectionFactory {
private static SqlSessionFactory factory;
private static SqlSession sqlSession = null;
// 读取MyBatis配置文件,创建SqlSessionFactory
static {
try {
Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
factory = new SqlSessionFactoryBuilder().build(reader, "eassen");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取SqlSession
* @return
*/
public static SqlSession getSession() {
if(sqlSession == null){
sqlSession = factory.openSession();
}
return sqlSession;
}
/**
* 从配置文件中获取数据库表映射对象信息
* @param mapper
* @return
*/
public static <T> T getMapper(Class<T> mapper) {
SqlSession session = getSession();
return (T) session.getMapper(mapper);
}
/**
* 数据提交
*/
public static void commit(){
sqlSession.commit();
}
/**
* 数据回滚
*/
public static void rollback(){
sqlSession.rollback();
}
/**
* 关闭sqlsession
*/
public static void close(){
if(sqlSession != null){
sqlSession.close();
}
}
}
Java代码
package com.db;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class ConnectionFactory {
private static SqlSessionFactory factory;
private static SqlSession sqlSession = null;
// 读取MyBatis配置文件,创建SqlSessionFactory
static {
try {
Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
factory = new SqlSessionFactoryBuilder().build(reader, "eassen");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取SqlSession
* @return
*/
public static SqlSession getSession() {
if(sqlSession == null){
sqlSession = factory.openSession();
}
return sqlSession;
}
/**
* 从配置文件中获取数据库表映射对象信息
* @param mapper
* @return
*/
public static <T> T getMapper(Class<T> mapper) {
SqlSession session = getSession();
return (T) session.getMapper(mapper);
}
/**
* 数据提交
*/
public static void commit(){
sqlSession.commit();
}
/**
* 数据回滚
*/
public static void rollback(){
sqlSession.rollback();
}
/**
* 关闭sqlsession
*/
public static void close(){
if(sqlSession != null){
sqlSession.close();
}
}
}
6.数据查询,插入,更新,删除测试
Java代码
package com.test;
import com.dao.DmMydwtMapper;
import com.db.ConnectionFactory;
import com.pojo.DmMydwt;
public class MydTest {
public static void main(String[] args) {
try {
// 获取对象
DmMydwtMapper mydwtMapper = ConnectionFactory
.getMapper(com.dao.DmMydwtMapper.class);
// 查询相关MYDWT_DM为“1001”的信息
DmMydwt mydwt = mydwtMapper.selectByPrimaryKey("1001");
System.out.println("mybatis查询测试:=================================");
System.out.println("MYDWT_DM:" + mydwt.getMydwtDm());
System.out.println("MYDWT_MC:" + mydwt.getMydwtMc());
// 插入数据
System.out.println("mybatis保存测试:=================================");
mydwt = new DmMydwt();
mydwt.setMydwtDm("aaaa");
mydwt.setMydwtMc("mybatis保存");
mydwt.setYxbz("Y");
mydwt.setXybz("Y");
mydwtMapper.insert(mydwt);
// 更新数据
System.out.println("mybatis更新测试:=================================");
mydwt.setMydwtMc(new String("mybatis保存".getBytes(),"UTF-8"));
mydwtMapper.updateByPrimaryKey(mydwt);
// 删除数据
System.out.println("mybatis删除测试:=================================");
mydwtMapper.deleteByPrimaryKey("1001");
ConnectionFactory.commit();
} catch (Exception e) {
ConnectionFactory.rollback();
e.printStackTrace();
} finally {
// 关闭sqlsession
ConnectionFactory.close();
}
}
}
Java代码
package com.test;
import com.dao.DmMydwtMapper;
import com.db.ConnectionFactory;
import com.pojo.DmMydwt;
public class MydTest {
public static void main(String[] args) {
try {
// 获取对象
DmMydwtMapper mydwtMapper = ConnectionFactory
.getMapper(com.dao.DmMydwtMapper.class);
// 查询相关MYDWT_DM为“1001”的信息
DmMydwt mydwt = mydwtMapper.selectByPrimaryKey("1001");
System.out.println("mybatis查询测试:=================================");
System.out.println("MYDWT_DM:" + mydwt.getMydwtDm());
System.out.println("MYDWT_MC:" + mydwt.getMydwtMc());
// 插入数据
System.out.println("mybatis保存测试:=================================");
mydwt = new DmMydwt();
mydwt.setMydwtDm("aaaa");
mydwt.setMydwtMc("mybatis保存");
mydwt.setYxbz("Y");
mydwt.setXybz("Y");
mydwtMapper.insert(mydwt);
// 更新数据
System.out.println("mybatis更新测试:=================================");
mydwt.setMydwtMc(new String("mybatis保存".getBytes(),"UTF-8"));
mydwtMapper.updateByPrimaryKey(mydwt);
// 删除数据
System.out.println("mybatis删除测试:=================================");
mydwtMapper.deleteByPrimaryKey("1001");
ConnectionFactory.commit();
} catch (Exception e) {
ConnectionFactory.rollback();
e.printStackTrace();
} finally {
// 关闭sqlsession
ConnectionFactory.close();
}
}
}
7.控制台信息输出
Java代码
2012-02-08 11:43:01 Logging initialized using 'org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl'adapter.
2012-02-08 11:43:01 PooledDataSource forcefully closed/removed all connections.
2012-02-08 11:43:01 PooledDataSource forcefully closed/removed all connections.
2012-02-08 11:43:01 PooledDataSource forcefully closed/removed all connections.
2012-02-08 11:43:01 PooledDataSource forcefully closed/removed all connections.
2012-02-08 11:43:02 Created connection 17671659.
2012-02-08 11:43:02 ooo Connection Opened
2012-02-08 11:43:02 ==> Executing: select MYDWT_DM, MYDWT_MC, YXBZ, XYBZ from EASSEN.DM_MYDWT where MYDWT_DM = ?
2012-02-08 11:43:02 ==> Parameters: 1001(String)
2012-02-08 11:43:02 <== Columns: MYDWT_DM, MYDWT_MC, YXBZ, XYBZ
2012-02-08 11:43:02 <== Row: 1001, 您经常访问的网站包括:, Y, Y
mybatis查询测试:=================================
MYDWT_DM:1001
MYDWT_MC:您经常访问的网站包括:
mybatis保存测试:=================================
2012-02-08 11:43:02 ==> Executing: insert into EASSEN.DM_MYDWT (MYDWT_DM, MYDWT_MC, YXBZ, XYBZ) values (?, ?, ?, ?)
2012-02-08 11:43:02 ==> Parameters: aaaa(String), mybatis保存(String), Y(String), Y(String)
mybatis更新测试:=================================
2012-02-08 11:43:02 ==> Executing: update EASSEN.DM_MYDWT set MYDWT_MC = ?, YXBZ = ?, XYBZ = ? where MYDWT_DM = ?
2012-02-08 11:43:02 ==> Parameters: mybatis保存(String), Y(String), Y(String), aaaa(String)
mybatis删除测试:=================================
2012-02-08 11:43:02 ==> Executing: delete from EASSEN.DM_MYDWT where MYDWT_DM = ?
2012-02-08 11:43:02 ==> Parameters: 1001(String)
2012-02-08 11:43:02 xxx Connection Closed
2012-02-08 11:43:02 Returned connection 17671659 to pool.
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。