- eassen
- 等级: 初级会员
- 文章: 1
- 积分: 70
- 来自: ...
|
源代码http://eassen.iteye.com/blog/1399002
近来打算开发一个技术框架,并应用此框架开发出一个论坛,开始打算用sturts2+spring+hibernate+jquery为基础,然后再进行封装,最后形成技术框架,但通过此网站获知mybatis比hibernate更灵活,实用。因此开始逐步接触mybatis。
以下是mybatis3.0的开发实例,使用的java环境jdk5.0,ide为eclipse3.7,数据库是oracle11g。
1.数据环境配置
- <?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 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 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 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
- 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;
- }
- }
- 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
- 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);
- }
- 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加载
- 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;
- 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();
- }
- }
- }
- 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;
-
-
-
- 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.数据查询,插入,更新,删除测试
- 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);
- 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 {
- ConnectionFactory.close();
- }
- }
- }
- 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);
-
-
- 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 {
-
- ConnectionFactory.close();
- }
- }
- }
7.控制台信息输出
- 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.
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|