1、我的開發環境是 jdk1.7+ecplise+oracle 11g
用到的jar包:mybatis-3.1.1.jar
ojdbc6.jar
2、項目整體結構

3、首先配置conf.xml文件
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
- <environments default="development">
- <environment id="development">
- <transactionManager type="JDBC" />
- <!-- 配置數據庫連接信息 -->
- <dataSource type="POOLED">
- <property name="driver" value="oracle.jdbc.driver.OracleDriver" />
- <property name="url" value="jdbc:oracle:thin:@xxx.xxx.xxx.xxx:1521:orcl" />
- <property name="username" value="xxx" />
- <property name="password" value="xxx" />
- </dataSource>
- </environment>
- </environments>
- </configuration>
4、數據庫表創建
- -- Create table
- create table DIM_LANE_AREA
- (
- lane_id NUMBER(4) not null,
- lane_name VARCHAR2(100) not null,
- direction CHAR(2) not null,
- facility_list CLOB not null,
- account_list CLOB not null,
- description VARCHAR2(100)
- )
- tablespace USERS
- pctfree 10
- initrans 1
- maxtrans 255
- storage
- (
- initial 64K
- next 1M
- minextents 1
- maxextents unlimited
- );
- -- Add comments to the columns
- comment on column DIM_LANE_AREA.lane_id
- is '單行道編號';
- comment on column DIM_LANE_AREA.lane_name
- is '單行道名稱';
- comment on column DIM_LANE_AREA.direction
- is '單行道方向';
- comment on column DIM_LANE_AREA.facility_list
- is '設備列表';
- comment on column DIM_LANE_AREA.account_list
- is '賬戶列表';
- comment on column DIM_LANE_AREA.description
- is '描述';
- -- Create/Recreate indexes
- create unique index DIM_LANE_AREA_1 on DIM_LANE_AREA (LANE_ID)
- tablespace USERS
- pctfree 10
- initrans 2
- maxtrans 255
- storage
- (
- initial 64K
- next 8K
- minextents 1
- maxextents unlimited
- );
- create index DIM_LANE_AREA_2 on DIM_LANE_AREA (DIRECTION)
- tablespace USERS
- pctfree 10
- initrans 2
- maxtrans 255
- storage
- (
- initial 64K
- next 8K
- minextents 1
- maxextents unlimited
- );
- -- Create/Recreate primary, unique and foreign key constraints
- alter table DIM_LANE_AREA
- add constraint DIM_LANE_AREA primary key (LANE_ID);
5、編寫實體類LaneArea.java
- package me.gacl.po;
- /**
- * 單行道配置信息PO
- * @author wqq
- * @time 2016-09-21
- */
- public class LaneArea
- {
- /**
- * 單行道編號
- */
- private Integer laneId;
- /**
- * 單行道名稱
- */
- private String laneName;
- /**
- * 單行道方向
- */
- private String direction;
- /**
- * 設備列表<span style="font-family: Arial, Helvetica, sans-serif;">(該字段為CLOB類型,以逗號分隔)</span>
- */
- private String facilityList;
- /**
- * 賬戶列表(該字段為CLOB類型,以逗號分隔)
- */
- private String accountList;
- /**
- * 描述
- */
- private String description;
- public Integer getLaneId() {
- return laneId;
- }
- public void setLaneId(Integer laneId) {
- this.laneId = laneId;
- }
- public String getLaneName() {
- return laneName;
- }
- public void setLaneName(String laneName) {
- this.laneName = laneName;
- }
- public String getDirection() {
- return direction;
- }
- public void setDirection(String direction) {
- this.direction = direction;
- }
- public String getFacilityList() {
- return facilityList;
- }
- public void setFacilityList(String facilityList) {
- this.facilityList = facilityList;
- }
- public String getAccountList() {
- return accountList;
- }
- public void setAccountList(String accountList) {
- this.accountList = accountList;
- }
- public String getDescription() {
- return description;
- }
- public void setDescription(String description) {
- this.description = description;
- }
- @Override
- public String toString()
- {
- return "LaneArea [lane_id=" + laneId + ", lane_name=" + laneName + ", facility_list=" + facilityList + "]";
- }
- }
注意:1、我之前沒有在這個實體類里加入toString(),返回了一個對象,雖然也對,但是沒有直觀效果,這個toString()可以直觀的看到你返回的結果。
6、配置LaneAreaMapper.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="me.gacl.mapping.laneAreaMapper">
- <!-- 在select標簽中編寫查詢的SQL語句, 設置select標簽的id屬性為getUser,id屬性值必須是唯一的,
- 不能夠重復 使用parameterType屬性指明查詢時使用的參數類型,resultType屬性指明查詢返回的結果集類型
- resultType="me.gacl.po.LaneArea"就表示將查詢結果封裝成一個LaneArea類的對象返回 LaneArea類
- 就是dim_lane_area表所對應的實體類 -->
- <!-- 根據id查詢得到一個LaneArea對象 -->
- <select id="getLaneAreaResultMap" parameterType="int" resultMap="LaneAreaResultMap">
- select * from dim_lane_area where lane_id = #{lane_id}
- </select>
- <!--這里因為實體類的屬性與數據庫字段不對應,所以要加上resultMap-->
- <resultMap type="me.gacl.po.LaneArea" id="LaneAreaResultMap">
- <id property="laneId" column="lane_id"/>
- <result property="laneName" column="lane_name"/>
- <result property="facilityList" column="facility_list" javaType="String" jdbcType="VARBINARY"/>
- </resultMap>
- </mapper>
7、在conf.xml中注冊LaneAreaMapper.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
- <environments default="development">
- <environment id="development">
- <transactionManager type="JDBC" />
- <!-- 配置數據庫連接信息 -->
- <dataSource type="POOLED">
- <property name="driver" value="oracle.jdbc.driver.OracleDriver" />
- <property name="url" value="jdbc:oracle:thin:@192.168.1.40:1521:ETL" />
- <property name="username" value="lane" />
- <property name="password" value="123" />
- </dataSource>
- </environment>
- </environments>
- <mappers>
- <!-- 注冊LaneAreaMapper.xml文件,
- LaneAreaMapper.xml位於me.gacl.mapping這個包下,所以resource寫成me/gacl/mapping/LaneAreaMapper.xml-->
- <mapper resource="me/gacl/mapping/laneAreaMapper.xml"/>
- </mappers>
- </configuration>
8、編寫測試類
- package me.gacl.test;
- import java.io.IOException;
- import java.io.InputStream;
- import me.gacl.po.LaneArea;
- import org.apache.ibatis.session.SqlSession;
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.apache.ibatis.session.SqlSessionFactoryBuilder;
- public class Test1 {
- public static void main(String[] args) throws IOException {
- //mybatis的配置文件
- String resource = "conf.xml";
- //使用類加載器加載mybatis的配置文件(它也加載關聯的映射文件)
- InputStream is = Test1.class.getClassLoader().getResourceAsStream(resource);
- //構建sqlSession的工廠
- SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
- //使用MyBatis提供的Resources類加載mybatis的配置文件(它也加載關聯的映射文件)
- //Reader reader = Resources.getResourceAsReader(resource);
- //構建sqlSession的工廠
- //SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
- //創建能執行映射文件中sql的sqlSession
- SqlSession session = sessionFactory.openSession();
- /**
- * 映射sql的標識字符串,
- * me.gacl.mapping.userMapper是userMapper.xml文件中mapper標簽的namespace屬性的值,
- * getUser是select標簽的id屬性值,通過select標簽的id屬性值就可以找到要執行的SQL
- */
- String statement = "me.gacl.mapping.laneAreaMapper.getLaneAreaResultMap";//映射sql的標識字符串
- LaneArea laneArea = session.selectOne(statement, 8);
- System.out.println(laneArea);
- }
- }
啟動Tomact,運行Test1,返回結果如下:

至此大工告成。
9、最后推薦一個自動生成實體類,Mapper.xml,DAO的工具:Mybatis-Generator
相關文章:
點擊打開鏈接