zoukankan      html  css  js  c++  java
  • MyBatis入门实例-包括实体类与数据库字段对应&CLOB字段处理

    1、我的开发环境是 jdk1.7+ecplise+oracle 11g

         用到的jar包:mybatis-3.1.1.jar 

    ojdbc6.jar            


    2、项目整体结构 

     
     


    3、首先配置conf.xml文件

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">  
    3. <configuration>  
    4.     <environments default="development">  
    5.         <environment id="development">  
    6.             <transactionManager type="JDBC" />  
    7.             <!-- 配置数据库连接信息 -->  
    8.             <dataSource type="POOLED">  
    9.                 <property name="driver" value="oracle.jdbc.driver.OracleDriver" />  
    10.                 <property name="url" value="jdbc:oracle:thin:@xxx.xxx.xxx.xxx:1521:orcl" />  
    11.                 <property name="username" value="xxx" />  
    12.                 <property name="password" value="xxx" />  
    13.             </dataSource>  
    14.         </environment>  
    15.     </environments>  
    16.   
    17. </configuration>  


    4、数据库表创建

    [sql] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. -- Create table  
    2. create table DIM_LANE_AREA  
    3. (  
    4.   lane_id       NUMBER(4) not null,  
    5.   lane_name     VARCHAR2(100) not null,  
    6.   direction     CHAR(2) not null,  
    7.   facility_list CLOB not null,  
    8.   account_list  CLOB not null,  
    9.   description   VARCHAR2(100)  
    10. )  
    11. tablespace USERS  
    12.   pctfree 10  
    13.   initrans 1  
    14.   maxtrans 255  
    15.   storage  
    16.   (  
    17.     initial 64K  
    18.     next 1M  
    19.     minextents 1  
    20.     maxextents unlimited  
    21.   );  
    22. -- Add comments to the columns   
    23. comment on column DIM_LANE_AREA.lane_id  
    24.   is '单行道编号';  
    25. comment on column DIM_LANE_AREA.lane_name  
    26.   is '单行道名称';  
    27. comment on column DIM_LANE_AREA.direction  
    28.   is '单行道方向';  
    29. comment on column DIM_LANE_AREA.facility_list  
    30.   is '设备列表';  
    31. comment on column DIM_LANE_AREA.account_list  
    32.   is '账户列表';  
    33. comment on column DIM_LANE_AREA.description  
    34.   is '描述';  
    35. -- Create/Recreate indexes   
    36. create unique index DIM_LANE_AREA_1 on DIM_LANE_AREA (LANE_ID)  
    37.   tablespace USERS  
    38.   pctfree 10  
    39.   initrans 2  
    40.   maxtrans 255  
    41.   storage  
    42.   (  
    43.     initial 64K  
    44.     next 8K  
    45.     minextents 1  
    46.     maxextents unlimited  
    47.   );  
    48. create index DIM_LANE_AREA_2 on DIM_LANE_AREA (DIRECTION)  
    49.   tablespace USERS  
    50.   pctfree 10  
    51.   initrans 2  
    52.   maxtrans 255  
    53.   storage  
    54.   (  
    55.     initial 64K  
    56.     next 8K  
    57.     minextents 1  
    58.     maxextents unlimited  
    59.   );  
    60. -- Create/Recreate primary, unique and foreign key constraints   
    61. alter table DIM_LANE_AREA  
    62.   add constraint DIM_LANE_AREA primary key (LANE_ID);  


    5、编写实体类LaneArea.java

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. package me.gacl.po;  
    2.   
    3. /** 
    4.  * 单行道配置信息PO 
    5.  * @author wqq 
    6.  * @time 2016-09-21 
    7.  */  
    8. public class LaneArea   
    9. {  
    10.     /** 
    11.      * 单行道编号 
    12.      */  
    13.     private Integer laneId;  
    14.       
    15.     /** 
    16.      * 单行道名称 
    17.      */  
    18.     private String laneName;  
    19.       
    20.     /** 
    21.      * 单行道方向 
    22.      */  
    23.     private String direction;  
    24.       
    25.     /** 
    26.      * 设备列表<span style="font-family: Arial, Helvetica, sans-serif;">(该字段为CLOB类型,以逗号分隔)</span> 
    27.      */  
    28.     private String facilityList;  
    29.       
    30.     /** 
    31.      * 账户列表(该字段为CLOB类型,以逗号分隔) 
    32.      */  
    33.     private String accountList;  
    34.       
    35.     /** 
    36.      * 描述 
    37.      */  
    38.     private String description;  
    39.   
    40.     public Integer getLaneId() {  
    41.         return laneId;  
    42.     }  
    43.   
    44.     public void setLaneId(Integer laneId) {  
    45.         this.laneId = laneId;  
    46.     }  
    47.   
    48.     public String getLaneName() {  
    49.         return laneName;  
    50.     }  
    51.   
    52.     public void setLaneName(String laneName) {  
    53.         this.laneName = laneName;  
    54.     }  
    55.   
    56.     public String getDirection() {  
    57.         return direction;  
    58.     }  
    59.   
    60.     public void setDirection(String direction) {  
    61.         this.direction = direction;  
    62.     }  
    63.   
    64.     public String getFacilityList() {  
    65.         return facilityList;  
    66.     }  
    67.   
    68.     public void setFacilityList(String facilityList) {  
    69.         this.facilityList = facilityList;  
    70.     }  
    71.   
    72.     public String getAccountList() {  
    73.         return accountList;  
    74.     }  
    75.   
    76.     public void setAccountList(String accountList) {  
    77.         this.accountList = accountList;  
    78.     }  
    79.   
    80.     public String getDescription() {  
    81.         return description;  
    82.     }  
    83.   
    84.     public void setDescription(String description) {  
    85.         this.description = description;  
    86.     }  
    87.   
    88.     @Override  
    89.     public String toString()   
    90.     {  
    91.         return "LaneArea [lane_id=" + laneId + ", lane_name=" + laneName + ", facility_list=" + facilityList + "]";  
    92.     }  
    93. }  
     
    注意:1、我之前没有在这个实体类里加入toString(),返回了一个对象,虽然也对,但是没有直观效果,这个toString()可以直观的看到你返回的结果。
     

    6、配置LaneAreaMapper.xml文件

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <?xml version="1.0" encoding="UTF-8" ?>  
    2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
    3. <mapper namespace="me.gacl.mapping.laneAreaMapper">  
    4.     <!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为getUser,id属性值必须是唯一的,  
    5.     不能够重复 使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型   
    6.         resultType="me.gacl.po.LaneArea"就表示将查询结果封装成一个LaneArea类的对象返回 LaneArea类  
    7.         就是dim_lane_area表所对应的实体类 -->  
    8.     <!-- 根据id查询得到一个LaneArea对象 -->  
    9.     <select id="getLaneAreaResultMap" parameterType="int" resultMap="LaneAreaResultMap">  
    10.         select * from dim_lane_area where lane_id = #{lane_id}  
    11.     </select>  
    12.     <!--这里因为实体类的属性与数据库字段不对应,所以要加上resultMap-->  
    13.     <resultMap type="me.gacl.po.LaneArea" id="LaneAreaResultMap">  
    14.          <id property="laneId" column="lane_id"/>  
    15.          <result property="laneName" column="lane_name"/>  
    16.          <result property="facilityList" column="facility_list" javaType="String" jdbcType="VARBINARY"/>  
    17.     </resultMap>  
    18. </mapper>  

    7、在conf.xml中注册LaneAreaMapper.xml

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">  
    3. <configuration>  
    4.     <environments default="development">  
    5.         <environment id="development">  
    6.             <transactionManager type="JDBC" />  
    7.             <!-- 配置数据库连接信息 -->  
    8.             <dataSource type="POOLED">  
    9.                 <property name="driver" value="oracle.jdbc.driver.OracleDriver" />  
    10.                 <property name="url" value="jdbc:oracle:thin:@192.168.1.40:1521:ETL" />  
    11.                 <property name="username" value="lane" />  
    12.                 <property name="password" value="123" />  
    13.             </dataSource>  
    14.         </environment>  
    15.     </environments>  
    16.       
    17.     <mappers>  
    18.          <!-- 注册LaneAreaMapper.xml文件,   
    19.          LaneAreaMapper.xml位于me.gacl.mapping这个包下,所以resource写成me/gacl/mapping/LaneAreaMapper.xml-->  
    20.          <mapper resource="me/gacl/mapping/laneAreaMapper.xml"/>  
    21.     </mappers>  
    22.   
    23. </configuration>  


    8、编写测试类

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. package me.gacl.test;  
    2.   
    3. import java.io.IOException;  
    4. import java.io.InputStream;  
    5. import me.gacl.po.LaneArea;  
    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 Test1 {  
    11.   
    12.     public static void main(String[] args) throws IOException {  
    13.         //mybatis的配置文件  
    14.         String resource = "conf.xml";  
    15.         //使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)  
    16.         InputStream is = Test1.class.getClassLoader().getResourceAsStream(resource);  
    17.         //构建sqlSession的工厂  
    18.         SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);  
    19.         //使用MyBatis提供的Resources类加载mybatis的配置文件(它也加载关联的映射文件)  
    20.         //Reader reader = Resources.getResourceAsReader(resource);   
    21.         //构建sqlSession的工厂  
    22.         //SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);  
    23.         //创建能执行映射文件中sql的sqlSession  
    24.         SqlSession session = sessionFactory.openSession();  
    25.         /** 
    26.          * 映射sql的标识字符串, 
    27.          * me.gacl.mapping.userMapper是userMapper.xml文件中mapper标签的namespace属性的值, 
    28.          * getUser是select标签的id属性值,通过select标签的id属性值就可以找到要执行的SQL 
    29.          */  
    30.         String statement = "me.gacl.mapping.laneAreaMapper.getLaneAreaResultMap";//映射sql的标识字符串  
    31.   
    32.         LaneArea laneArea = session.selectOne(statement, 8);  
    33.         System.out.println(laneArea);  
    34.     }  
    35. }  

    启动Tomact,运行Test1,返回结果如下:
     
    至此大工告成。
     
    9、最后推荐一个自动生成实体类,Mapper.xml,DAO的工具:Mybatis-Generator
    相关文章:点击打开链接
  • 相关阅读:
    【分享】HTML5附件拖拽上传drop & google.gears
    【分享】return false,对阻止事件默认动作的一些测试
    【记录】随笔分类汇总
    【分享】微博 @ 符号的用户名提示效果。(想@到谁?)
    【记录】File, FileReader 和 Ajax 文件上传
    【动态】简单的JS动态加载单体
    【分享】简单页面提示插件第二版表单验证很简单
    【记录】GIT 常用命令
    【分享】jQuery animate自定义动画的简单实现
    【分享】 封装js操作textarea 方法集合(兼容很好)。
  • 原文地址:https://www.cnblogs.com/soundcode/p/6531852.html
Copyright © 2011-2022 走看看