zoukankan      html  css  js  c++  java
  • mybatis3的一个介绍文章

    精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
    作者正文
       发表时间:前天   最后修改:前天
    源代码http://eassen.iteye.com/blog/1399002

    近来打算开发一个技术框架,并应用此框架开发出一个论坛,开始打算用sturts2+spring+hibernate+jquery为基础,然后再进行封装,最后形成技术框架,但通过此网站获知mybatis比hibernate更灵活,实用。因此开始逐步接触mybatis。

    以下是mybatis3.0的开发实例,使用的java环境jdk5.0,ide为eclipse3.7,数据库是oracle11g。

    1.数据环境配置

    Xml代码 复制代码 收藏代码
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "mybatis-3-config.dtd">
    3. <configuration>
    4. <!-- - - - - - - 数据库环境配置- - - - - - - - - -->
    5. <environments default="environments">
    6. <environment id="eassen">
    7. <transactionManager type="JDBC"/>
    8. <dataSource type="POOLED">
    9. <property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
    10. <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:eassen"/>
    11. <property name="username" value="eassen"/>
    12. <property name="password" value="oracle"/>
    13. </dataSource>
    14. </environment>
    15. </environments>
    16. <!-- - - - - - - -映射文件路径- - - - - - -->
    17. <mappers>
    18. <mapper resource="com/pojo/sql/DmMydwtMapper.xml"/>
    19. </mappers>
    20. </configuration>
    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "mybatis-3-config.dtd">  
    3.   
    4. <configuration>  
    5.     <!-- - - - - - - 数据库环境配置- - - - - - - - - -->  
    6.     <environments default="environments">  
    7.         <environment id="eassen">  
    8.             <transactionManager type="JDBC"/>  
    9.             <dataSource type="POOLED">  
    10.                 <property name="driver" value="oracle.jdbc.driver.OracleDriver"/>  
    11.                 <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:eassen"/>  
    12.                 <property name="username" value="eassen"/>  
    13.                 <property name="password" value="oracle"/>  
    14.             </dataSource>  
    15.         </environment>  
    16.     </environments>  
    17.     <!-- - - - - - - -映射文件路径- - - - - - -->  
    18.     <mappers>  
    19.         <mapper resource="com/pojo/sql/DmMydwtMapper.xml"/>  
    20.     </mappers>  
    21. </configuration>  

    2.数据库表映射(DM_MYDWT)XML文件

    Xml代码 复制代码 收藏代码
    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    4. <mapper namespace="com.dao.DmMydwtMapper">
    5. <resultMap id="BaseResultMap" type="com.pojo.DmMydwt">
    6. <id column="MYDWT_DM" property="mydwtDm" jdbcType="CHAR" javaType="String" />
    7. <result column="MYDWT_MC" property="mydwtMc" jdbcType="VARCHAR"
    8. javaType="String" />
    9. <result column="YXBZ" property="yxbz" jdbcType="CHAR" javaType="String" />
    10. <result column="XYBZ" property="xybz" jdbcType="CHAR" javaType="String" />
    11. </resultMap>
    12. <sql id="Base_Column_List">
    13. MYDWT_DM, MYDWT_MC, YXBZ, XYBZ
    14. </sql>
    15. <select id="selectByPrimaryKey" resultMap="BaseResultMap"
    16. parameterType="java.lang.String">
    17. select
    18. <include refid="Base_Column_List" />
    19. from EASSEN.DM_MYDWT
    20. where MYDWT_DM = #{mydwtDm,jdbcType=CHAR}
    21. </select>
    22. <delete id="deleteByPrimaryKey" parameterType="java.lang.String"
    23. flushCache="true">
    24. delete from EASSEN.DM_MYDWT
    25. where MYDWT_DM =
    26. #{mydwtDm,jdbcType=CHAR}
    27. </delete>
    28. <insert id="insert" parameterType="com.pojo.DmMydwt" flushCache="true">
    29. insert into EASSEN.DM_MYDWT (MYDWT_DM, MYDWT_MC, YXBZ,
    30. XYBZ)
    31. values
    32. (#{mydwtDm,jdbcType=CHAR}, #{mydwtMc,jdbcType=VARCHAR},
    33. #{yxbz,jdbcType=CHAR},
    34. #{xybz,jdbcType=CHAR})
    35. </insert>
    36. <insert id="insertSelective" parameterType="com.pojo.DmMydwt"
    37. flushCache="true">
    38. insert into EASSEN.DM_MYDWT
    39. <trim prefix="(" suffix=")" suffixOverrides=",">
    40. MYDWT_DM,
    41. MYDWT_MC,
    42. YXBZ,
    43. XYBZ,
    44. </trim>
    45. <trim prefix="values (" suffix=")" suffixOverrides=",">
    46. #{mydwtDm,jdbcType=CHAR},
    47. #{mydwtMc},
    48. #{yxbz,jdbcType=CHAR},
    49. #{xybz,jdbcType=CHAR},
    50. </trim>
    51. </insert>
    52. <update id="updateByPrimaryKeySelective" parameterType="com.pojo.DmMydwt"
    53. flushCache="true">
    54. update EASSEN.DM_MYDWT
    55. <set>
    56. <if test="mydwtMc != null">
    57. MYDWT_MC = #{mydwtMc,jdbcType=VARCHAR},
    58. </if>
    59. <if test="yxbz != null">
    60. YXBZ = #{yxbz,jdbcType=CHAR},
    61. </if>
    62. <if test="xybz != null">
    63. XYBZ = #{xybz,jdbcType=CHAR},
    64. </if>
    65. </set>
    66. where MYDWT_DM = #{mydwtDm,jdbcType=CHAR}
    67. </update>
    68. <update id="updateByPrimaryKey" parameterType="com.pojo.DmMydwt"
    69. flushCache="true">
    70. update EASSEN.DM_MYDWT
    71. set MYDWT_MC =
    72. #{mydwtMc,jdbcType=VARCHAR},
    73. YXBZ = #{yxbz,jdbcType=CHAR},
    74. XYBZ =
    75. #{xybz,jdbcType=CHAR}
    76. where MYDWT_DM = #{mydwtDm,jdbcType=CHAR}
    77. </update>
    78. </mapper>
    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="UTF-8" ?>  
    2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"   
    3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
    4. <mapper namespace="com.dao.DmMydwtMapper">  
    5.     <resultMap id="BaseResultMap" type="com.pojo.DmMydwt">  
    6.         <id column="MYDWT_DM" property="mydwtDm" jdbcType="CHAR" javaType="String" />  
    7.         <result column="MYDWT_MC" property="mydwtMc" jdbcType="VARCHAR"  
    8.             javaType="String" />  
    9.         <result column="YXBZ" property="yxbz" jdbcType="CHAR" javaType="String" />  
    10.         <result column="XYBZ" property="xybz" jdbcType="CHAR" javaType="String" />  
    11.     </resultMap>  
    12.     <sql id="Base_Column_List">  
    13.         MYDWT_DM, MYDWT_MC, YXBZ, XYBZ  
    14.     </sql>  
    15.     <select id="selectByPrimaryKey" resultMap="BaseResultMap"  
    16.         parameterType="java.lang.String">  
    17.         select  
    18.         <include refid="Base_Column_List" />  
    19.         from EASSEN.DM_MYDWT  
    20.         where MYDWT_DM = #{mydwtDm,jdbcType=CHAR}  
    21.     </select>  
    22.     <delete id="deleteByPrimaryKey" parameterType="java.lang.String"  
    23.         flushCache="true">  
    24.         delete from EASSEN.DM_MYDWT  
    25.         where MYDWT_DM =  
    26.         #{mydwtDm,jdbcType=CHAR}  
    27.     </delete>  
    28.     <insert id="insert" parameterType="com.pojo.DmMydwt" flushCache="true">  
    29.         insert into EASSEN.DM_MYDWT (MYDWT_DM, MYDWT_MC, YXBZ,  
    30.         XYBZ)  
    31.         values  
    32.         (#{mydwtDm,jdbcType=CHAR}, #{mydwtMc,jdbcType=VARCHAR},  
    33.         #{yxbz,jdbcType=CHAR},  
    34.         #{xybz,jdbcType=CHAR})  
    35.     </insert>  
    36.     <insert id="insertSelective" parameterType="com.pojo.DmMydwt"  
    37.         flushCache="true">  
    38.         insert into EASSEN.DM_MYDWT  
    39.         <trim prefix="(" suffix=")" suffixOverrides=",">  
    40.             MYDWT_DM,  
    41.             MYDWT_MC,  
    42.             YXBZ,  
    43.             XYBZ,  
    44.         </trim>  
    45.         <trim prefix="values (" suffix=")" suffixOverrides=",">  
    46.             #{mydwtDm,jdbcType=CHAR},  
    47.             #{mydwtMc},  
    48.             #{yxbz,jdbcType=CHAR},  
    49.             #{xybz,jdbcType=CHAR},  
    50.         </trim>  
    51.     </insert>  
    52.     <update id="updateByPrimaryKeySelective" parameterType="com.pojo.DmMydwt"  
    53.         flushCache="true">  
    54.         update EASSEN.DM_MYDWT  
    55.         <set>  
    56.             <if test="mydwtMc != null">  
    57.                 MYDWT_MC = #{mydwtMc,jdbcType=VARCHAR},  
    58.             </if>  
    59.             <if test="yxbz != null">  
    60.                 YXBZ = #{yxbz,jdbcType=CHAR},  
    61.             </if>  
    62.             <if test="xybz != null">  
    63.                 XYBZ = #{xybz,jdbcType=CHAR},  
    64.             </if>  
    65.         </set>  
    66.         where MYDWT_DM = #{mydwtDm,jdbcType=CHAR}  
    67.     </update>  
    68.     <update id="updateByPrimaryKey" parameterType="com.pojo.DmMydwt"  
    69.         flushCache="true">  
    70.         update EASSEN.DM_MYDWT  
    71.         set MYDWT_MC =  
    72.         #{mydwtMc,jdbcType=VARCHAR},  
    73.         YXBZ = #{yxbz,jdbcType=CHAR},  
    74.         XYBZ =  
    75.         #{xybz,jdbcType=CHAR}  
    76.         where MYDWT_DM = #{mydwtDm,jdbcType=CHAR}  
    77.     </update>  
    78. </mapper>  

    3.数据库表(DM_MYDWT)java

    Java代码 复制代码 收藏代码
    1. package com.pojo;
    2. import java.io.Serializable;
    3. public class DmMydwt implements Serializable {
    4. /**
    5. *
    6. */
    7. private static final long serialVersionUID = 1078518054837885063L;
    8. /**
    9. * EASSEN.DM_MYDWT.MYDWT_DM
    10. * @ibatorgenerated 2012-02-05 20:14:30
    11. */
    12. private String mydwtDm;
    13. /**
    14. * EASSEN.DM_MYDWT.MYDWT_MC
    15. * @ibatorgenerated 2012-02-05 20:14:30
    16. */
    17. private String mydwtMc;
    18. /**
    19. * EASSEN.DM_MYDWT.YXBZ
    20. * @ibatorgenerated 2012-02-05 20:14:30
    21. */
    22. private String yxbz;
    23. /**
    24. * EASSEN.DM_MYDWT.XYBZ
    25. * @ibatorgenerated 2012-02-05 20:14:30
    26. */
    27. private String xybz;
    28. public String getMydwtDm() {
    29. return mydwtDm;
    30. }
    31. public void setMydwtDm(String mydwtDm) {
    32. this.mydwtDm = mydwtDm;
    33. }
    34. public String getMydwtMc() {
    35. return mydwtMc;
    36. }
    37. public void setMydwtMc(String mydwtMc) {
    38. this.mydwtMc = mydwtMc;
    39. }
    40. public String getYxbz() {
    41. return yxbz;
    42. }
    43. public void setYxbz(String yxbz) {
    44. this.yxbz = yxbz;
    45. }
    46. public String getXybz() {
    47. return xybz;
    48. }
    49. public void setXybz(String xybz) {
    50. this.xybz = xybz;
    51. }
    52. }
    Java代码  收藏代码
    1. package com.pojo;  
    2.   
    3. import java.io.Serializable;  
    4.   
    5. public class DmMydwt implements Serializable {  
    6.     /** 
    7.      *  
    8.      */  
    9.     private static final long serialVersionUID = 1078518054837885063L;  
    10.   
    11.     /** 
    12.      * EASSEN.DM_MYDWT.MYDWT_DM 
    13.      * @ibatorgenerated 2012-02-05 20:14:30 
    14.      */  
    15.     private String mydwtDm;  
    16.   
    17.     /** 
    18.      * EASSEN.DM_MYDWT.MYDWT_MC 
    19.      * @ibatorgenerated 2012-02-05 20:14:30 
    20.      */  
    21.     private String mydwtMc;  
    22.   
    23.     /** 
    24.      * EASSEN.DM_MYDWT.YXBZ 
    25.      * @ibatorgenerated 2012-02-05 20:14:30 
    26.      */  
    27.     private String yxbz;  
    28.   
    29.     /** 
    30.      * EASSEN.DM_MYDWT.XYBZ 
    31.      * @ibatorgenerated 2012-02-05 20:14:30 
    32.      */  
    33.     private String xybz;  
    34.   
    35.     public String getMydwtDm() {  
    36.         return mydwtDm;  
    37.     }  
    38.   
    39.     public void setMydwtDm(String mydwtDm) {  
    40.         this.mydwtDm = mydwtDm;  
    41.     }  
    42.   
    43.     public String getMydwtMc() {  
    44.         return mydwtMc;  
    45.     }  
    46.   
    47.     public void setMydwtMc(String mydwtMc) {  
    48.         this.mydwtMc = mydwtMc;  
    49.     }  
    50.   
    51.     public String getYxbz() {  
    52.         return yxbz;  
    53.     }  
    54.   
    55.     public void setYxbz(String yxbz) {  
    56.         this.yxbz = yxbz;  
    57.     }  
    58.   
    59.     public String getXybz() {  
    60.         return xybz;  
    61.     }  
    62.   
    63.     public void setXybz(String xybz) {  
    64.         this.xybz = xybz;  
    65.     }  
    66. }  

     

    4.DAO

    Java代码 复制代码 收藏代码
    1. package com.dao;
    2. import com.pojo.DmMydwt;
    3. public interface DmMydwtMapper {
    4. /**
    5. * 根据主键删除
    6. * 参数:主键
    7. * 返回:删除个数
    8. * @ibatorgenerated 2012-02-05 20:14:58
    9. */
    10. int deleteByPrimaryKey(String mydwtDm);
    11. /**
    12. * 插入,空属性也会插入
    13. * 参数:pojo对象
    14. * 返回:删除个数
    15. * @ibatorgenerated 2012-02-05 20:14:58
    16. */
    17. int insert(DmMydwt record);
    18. /**
    19. * 插入,空属性不会插入
    20. * 参数:pojo对象
    21. * 返回:删除个数
    22. * @ibatorgenerated 2012-02-05 20:14:58
    23. */
    24. int insertSelective(DmMydwt record);
    25. /**
    26. * 根据主键查询
    27. * 参数:查询条件,主键值
    28. * 返回:对象
    29. * @ibatorgenerated 2012-02-05 20:14:58
    30. */
    31. DmMydwt selectByPrimaryKey(String mydwtDm);
    32. /**
    33. * 根据主键修改,空值条件不会修改成null
    34. * 参数:1.要修改成的值
    35. * 返回:成功修改个数
    36. * @ibatorgenerated 2012-02-05 20:14:58
    37. */
    38. int updateByPrimaryKeySelective(DmMydwt record);
    39. /**
    40. * 根据主键修改,空值条件会修改成null
    41. * 参数:1.要修改成的值
    42. * 返回:成功修改个数
    43. * @ibatorgenerated 2012-02-05 20:14:58
    44. */
    45. int updateByPrimaryKey(DmMydwt record);
    46. }
    Java代码  收藏代码
    1. package com.dao;  
    2.   
    3. import com.pojo.DmMydwt;  
    4.   
    5. public interface DmMydwtMapper {  
    6.     /** 
    7.      * 根据主键删除 
    8.      * 参数:主键 
    9.      * 返回:删除个数 
    10.      * @ibatorgenerated 2012-02-05 20:14:58 
    11.      */  
    12.     int deleteByPrimaryKey(String mydwtDm);  
    13.   
    14.     /** 
    15.      * 插入,空属性也会插入 
    16.      * 参数:pojo对象 
    17.      * 返回:删除个数 
    18.      * @ibatorgenerated 2012-02-05 20:14:58 
    19.      */  
    20.     int insert(DmMydwt record);  
    21.   
    22.     /** 
    23.      * 插入,空属性不会插入 
    24.      * 参数:pojo对象 
    25.      * 返回:删除个数 
    26.      * @ibatorgenerated 2012-02-05 20:14:58 
    27.      */  
    28.     int insertSelective(DmMydwt record);  
    29.   
    30.     /** 
    31.      * 根据主键查询 
    32.      * 参数:查询条件,主键值 
    33.      * 返回:对象 
    34.      * @ibatorgenerated 2012-02-05 20:14:58 
    35.      */  
    36.     DmMydwt selectByPrimaryKey(String mydwtDm);  
    37.   
    38.     /** 
    39.      * 根据主键修改,空值条件不会修改成null 
    40.      * 参数:1.要修改成的值 
    41.      * 返回:成功修改个数 
    42.      * @ibatorgenerated 2012-02-05 20:14:58 
    43.      */  
    44.     int updateByPrimaryKeySelective(DmMydwt record);  
    45.   
    46.     /** 
    47.      * 根据主键修改,空值条件会修改成null 
    48.      * 参数:1.要修改成的值 
    49.      * 返回:成功修改个数 
    50.      * @ibatorgenerated 2012-02-05 20:14:58 
    51.      */  
    52.     int updateByPrimaryKey(DmMydwt record);  
    53. }  

     

    5.mybatis加载

    Java代码 复制代码 收藏代码
    1. package com.db;
    2. import java.io.IOException;
    3. import java.io.Reader;
    4. import org.apache.ibatis.io.Resources;
    5. import org.apache.ibatis.session.SqlSession;
    6. import org.apache.ibatis.session.SqlSessionFactory;
    7. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    8. public class ConnectionFactory {
    9. private static SqlSessionFactory factory;
    10. private static SqlSession sqlSession = null;
    11. // 读取MyBatis配置文件,创建SqlSessionFactory
    12. static {
    13. try {
    14. Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
    15. factory = new SqlSessionFactoryBuilder().build(reader, "eassen");
    16. catch (IOException e) {
    17. e.printStackTrace();
    18. }
    19. }
    20. /**
    21. * 获取SqlSession
    22. * @return
    23. */
    24. public static SqlSession getSession() {
    25. if(sqlSession == null){
    26. sqlSession = factory.openSession();
    27. }
    28. return sqlSession;
    29. }
    30. /**
    31. * 从配置文件中获取数据库表映射对象信息
    32. * @param mapper
    33. * @return
    34. */
    35. public static <T> T getMapper(Class<T> mapper) {
    36. SqlSession session = getSession();
    37. return (T) session.getMapper(mapper);
    38. }
    39. /**
    40. * 数据提交
    41. */
    42. public static void commit(){
    43. sqlSession.commit();
    44. }
    45. /**
    46. * 数据回滚
    47. */
    48. public static void rollback(){
    49. sqlSession.rollback();
    50. }
    51. /**
    52. * 关闭sqlsession
    53. */
    54. public static void close(){
    55. if(sqlSession != null){
    56. sqlSession.close();
    57. }
    58. }
    59. }
    Java代码  收藏代码
    1. package com.db;  
    2.   
    3. import java.io.IOException;  
    4. import java.io.Reader;  
    5. import org.apache.ibatis.io.Resources;  
    6. import org.apache.ibatis.session.SqlSession;  
    7. import org.apache.ibatis.session.SqlSessionFactory;  
    8. import org.apache.ibatis.session.SqlSessionFactoryBuilder;  
    9.   
    10. public class ConnectionFactory {  
    11.     private static SqlSessionFactory factory;  
    12.     private static SqlSession sqlSession = null;  
    13.   
    14.       
    15.     // 读取MyBatis配置文件,创建SqlSessionFactory  
    16.     static {  
    17.         try {  
    18.             Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");  
    19.             factory = new SqlSessionFactoryBuilder().build(reader, "eassen");  
    20.         } catch (IOException e) {  
    21.             e.printStackTrace();  
    22.         }  
    23.     }  
    24.   
    25.     /** 
    26.      * 获取SqlSession 
    27.      * @return 
    28.      */  
    29.     public static SqlSession getSession() {  
    30.         if(sqlSession == null){  
    31.             sqlSession = factory.openSession();  
    32.         }  
    33.         return sqlSession;  
    34.     }  
    35.   
    36.     /** 
    37.      * 从配置文件中获取数据库表映射对象信息 
    38.      * @param mapper 
    39.      * @return 
    40.      */  
    41.     public static <T> T getMapper(Class<T> mapper) {  
    42.         SqlSession session = getSession();  
    43.         return (T) session.getMapper(mapper);  
    44.     }  
    45.       
    46.     /** 
    47.      * 数据提交 
    48.      */  
    49.     public static void commit(){  
    50.         sqlSession.commit();  
    51.     }  
    52.     /** 
    53.      * 数据回滚 
    54.      */  
    55.     public static void rollback(){  
    56.         sqlSession.rollback();  
    57.     }  
    58.     /** 
    59.      * 关闭sqlsession 
    60.      */  
    61.     public static void close(){  
    62.         if(sqlSession != null){  
    63.             sqlSession.close();  
    64.         }  
    65.     }  
    66.       
    67.       
    68. }  

    6.数据查询,插入,更新,删除测试

    Java代码 复制代码 收藏代码
    1. package com.test;
    2. import com.dao.DmMydwtMapper;
    3. import com.db.ConnectionFactory;
    4. import com.pojo.DmMydwt;
    5. public class MydTest {
    6. public static void main(String[] args) {
    7. try {
    8. // 获取对象
    9. DmMydwtMapper mydwtMapper = ConnectionFactory
    10. .getMapper(com.dao.DmMydwtMapper.class);
    11. // 查询相关MYDWT_DM为“1001”的信息
    12. DmMydwt mydwt = mydwtMapper.selectByPrimaryKey("1001");
    13. System.out.println("mybatis查询测试:=================================");
    14. System.out.println("MYDWT_DM:" + mydwt.getMydwtDm());
    15. System.out.println("MYDWT_MC:" + mydwt.getMydwtMc());
    16. // 插入数据
    17. System.out.println("mybatis保存测试:=================================");
    18. mydwt = new DmMydwt();
    19. mydwt.setMydwtDm("aaaa");
    20. mydwt.setMydwtMc("mybatis保存");
    21. mydwt.setYxbz("Y");
    22. mydwt.setXybz("Y");
    23. mydwtMapper.insert(mydwt);
    24. // 更新数据
    25. System.out.println("mybatis更新测试:=================================");
    26. mydwt.setMydwtMc(new String("mybatis保存".getBytes(),"UTF-8"));
    27. mydwtMapper.updateByPrimaryKey(mydwt);
    28. // 删除数据
    29. System.out.println("mybatis删除测试:=================================");
    30. mydwtMapper.deleteByPrimaryKey("1001");
    31. ConnectionFactory.commit();
    32. catch (Exception e) {
    33. ConnectionFactory.rollback();
    34. e.printStackTrace();
    35. finally {
    36. // 关闭sqlsession
    37. ConnectionFactory.close();
    38. }
    39. }
    40. }
    Java代码  收藏代码
    1. package com.test;  
    2.   
    3. import com.dao.DmMydwtMapper;  
    4. import com.db.ConnectionFactory;  
    5. import com.pojo.DmMydwt;  
    6.   
    7. public class MydTest {  
    8.   
    9.     public static void main(String[] args) {  
    10.         try {  
    11.             // 获取对象  
    12.             DmMydwtMapper mydwtMapper = ConnectionFactory  
    13.                     .getMapper(com.dao.DmMydwtMapper.class);  
    14.   
    15.             // 查询相关MYDWT_DM为“1001”的信息  
    16.             DmMydwt mydwt = mydwtMapper.selectByPrimaryKey("1001");  
    17.             System.out.println("mybatis查询测试:=================================");  
    18.             System.out.println("MYDWT_DM:" + mydwt.getMydwtDm());  
    19.             System.out.println("MYDWT_MC:" + mydwt.getMydwtMc());  
    20.               
    21.             // 插入数据  
    22.             System.out.println("mybatis保存测试:=================================");  
    23.             mydwt = new DmMydwt();  
    24.             mydwt.setMydwtDm("aaaa");  
    25.             mydwt.setMydwtMc("mybatis保存");  
    26.             mydwt.setYxbz("Y");  
    27.             mydwt.setXybz("Y");  
    28.             mydwtMapper.insert(mydwt);  
    29.             // 更新数据  
    30.             System.out.println("mybatis更新测试:=================================");  
    31.             mydwt.setMydwtMc(new String("mybatis保存".getBytes(),"UTF-8"));  
    32.             mydwtMapper.updateByPrimaryKey(mydwt);  
    33.               
    34.             // 删除数据  
    35.             System.out.println("mybatis删除测试:=================================");  
    36.             mydwtMapper.deleteByPrimaryKey("1001");  
    37.             ConnectionFactory.commit();  
    38.         } catch (Exception e) {  
    39.             ConnectionFactory.rollback();  
    40.             e.printStackTrace();  
    41.         } finally {  
    42.             // 关闭sqlsession  
    43.             ConnectionFactory.close();  
    44.         }  
    45.     }  
    46. }  

    7.控制台信息输出

    Java代码 复制代码 收藏代码
    1. 2012-02-08 11:43:01 Logging initialized using 'org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl' adapter.
    2. 2012-02-08 11:43:01 PooledDataSource forcefully closed/removed all connections.
    3. 2012-02-08 11:43:01 PooledDataSource forcefully closed/removed all connections.
    4. 2012-02-08 11:43:01 PooledDataSource forcefully closed/removed all connections.
    5. 2012-02-08 11:43:01 PooledDataSource forcefully closed/removed all connections.
    6. 2012-02-08 11:43:02 Created connection 17671659.
    7. 2012-02-08 11:43:02 ooo Connection Opened
    8. 2012-02-08 11:43:02 ==> Executing: select MYDWT_DM, MYDWT_MC, YXBZ, XYBZ from EASSEN.DM_MYDWT where MYDWT_DM = ?
    9. 2012-02-08 11:43:02 ==> Parameters: 1001(String)
    10. 2012-02-08 11:43:02 <== Columns: MYDWT_DM, MYDWT_MC, YXBZ, XYBZ
    11. 2012-02-08 11:43:02 <== Row: 1001, 您经常访问的网站包括:, Y, Y
    12. mybatis查询测试:=================================
    13. MYDWT_DM:1001
    14. MYDWT_MC:您经常访问的网站包括:
    15. mybatis保存测试:=================================
    16. 2012-02-08 11:43:02 ==> Executing: insert into EASSEN.DM_MYDWT (MYDWT_DM, MYDWT_MC, YXBZ, XYBZ) values (?, ?, ?, ?)
    17. 2012-02-08 11:43:02 ==> Parameters: aaaa(String), mybatis保存(String), Y(String), Y(String)
    18. mybatis更新测试:=================================
    19. 2012-02-08 11:43:02 ==> Executing: update EASSEN.DM_MYDWT set MYDWT_MC = ?, YXBZ = ?, XYBZ = ? where MYDWT_DM = ?
    20. 2012-02-08 11:43:02 ==> Parameters: mybatis保存(String), Y(String), Y(String), aaaa(String)
    21. mybatis删除测试:=================================
    22. 2012-02-08 11:43:02 ==> Executing: delete from EASSEN.DM_MYDWT where MYDWT_DM = ?
    23. 2012-02-08 11:43:02 ==> Parameters: 1001(String)
    24. 2012-02-08 11:43:02 xxx Connection Closed
    25. 2012-02-08 11:43:02 Returned connection 17671659 to pool.
     
       发表时间:8 小时前  

    关于Mybatis3的更新操作,常见有以下两种:

     

    第一种:

     

    Xml代码  收藏代码
    1. <update id="updateBySelected" parameterType="users">  
    2.     update users  
    3.     <set>  
    4.         <if test="name!=null">user_name = #{name}</if>  
    5.         <if test="addr!=null">user_addr = #{addr}</if>  
    6.     </set>  
    7.     where user_id=#{id}  
    8. </update>  

     

    第二种:

     

    Xml代码  收藏代码
    1. <update id="update" parameterType="users">  
    2.     update users set user_name = #{name}, user_addr = #{addr}  
    3.     where user_id=#{id}  
    4. </update>  

     

    个人认为有时候还是有需要将字段值设置为null的,比如清空日期字段。因此,针对只更新某一两个字段值的情况,我一般是这样操作的:

     

    Java代码  收藏代码
    1. // 只更新用户姓名(前台页面只传入id、name)  
    2. User user = userService.findById(id);//根据用户ID获取缓存的用户对象  
    3. user.setName(name);//更新用户姓名  
    4. userService.update(user);//同时更新User表所有字段值  

     

    不知道楼主是如何操作的(只更新某些字段)?

     

     

     

    0  请登录后投票
     

  • 相关阅读:
    5个最好用AngularJS构建应用程序框架
    5款最好的免费在线网站CSS验证器
    10款最优秀的开源移动开发工具
    10个最好的免费PS图象处理软件方案
    10个基本的HTML5动画工具设计
    6款最好的免费在线二维码生成器
    Redis配置文件参数说明
    Redis学习手册(主从复制)
    java.lang.OutOfMemoryError: PermGen space PermGen space & java.lang.OutOfMemoryError: Java heap space Heap siz
    TNSNAMES.ORA 配置
  • 原文地址:https://www.cnblogs.com/lexus/p/2346218.html
Copyright © 2011-2022 走看看