您好,登錄后才能下訂單哦!
近來打算開發一個技術框架,并應用此框架開發出一個論壇,開始打算用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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。