主题:Mybatis3.0查询,保存,更新,删除数据。
近来打算开发一个技术框架,并应用此框架开发出一个论坛,开始打算用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;
private String mydwtDm;
private String mydwtMc;
private String yxbz;
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;
private String mydwtDm;
private String mydwtMc;
private String yxbz;
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 {
int deleteByPrimaryKey(String mydwtDm);
int insert(DmMydwt record);
int insertSelective(DmMydwt record);
DmMydwt selectByPrimaryKey(String mydwtDm);
int updateByPrimaryKeySelective(DmMydwt record);
int updateByPrimaryKey(DmMydwt record);
}
Java代码
package com.dao;
import com.pojo.DmMydwt;
public interface DmMydwtMapper {
int deleteByPrimaryKey(String mydwtDm);
int insert(DmMydwt record);
int insertSelective(DmMydwt record);
DmMydwt selectByPrimaryKey(String mydwtDm);
int updateByPrimaryKeySelective(DmMydwt record);
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();
}
}
public static SqlSession getSession() {
if(sqlSession == null){
sqlSession = factory.openSession();
}
return sqlSession;
}
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();
}
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();
}
}
public static SqlSession getSession() {
if(sqlSession == null){
sqlSession = factory.openSession();
}
return sqlSession;
}
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();
}
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.
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341