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
相关文章:
点击打开链接
【推荐】腾讯云新用户域名抢购1元起,抓紧抢购
· 阮一峰:加密货币的本质
· ofo被曝订单较峰值跌六成 账户现金仅能支撑一个月
· 途牛宣布一亿美元股票回购计划及CTO任命
· 我们帮你划了一份微信公开课PRO的重点
· iPhone 4S起死回生,可降级至iOS 6.1.3
» 更多新闻...
· 以操作系统的角度述说线程与进程
· 软件测试转型之路
· 门内门外看招聘
· 大道至简,职场上做人做事做管理