论坛首页 Java企业应用论坛

ibatis经典示例

浏览 9986 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (1)
作者 正文
   发表时间:2014-03-27  

最近学习ibatis,动手写了点代码

 

 

PoJo对象

package com.huasoft.common.domain;

 

import java.util.Date;

 

public class Employee

{

private String id;

private String loginname;

private String name; 

private String age;

private Date birthday;

private String dept_id;

private String position;

private String  gender;

private String  married;

private String salary;

private String educational;

private String country;

private String city;

private String remarks;

private String school;

private Date createtime;

private String email;

 

 

public Date getBirthday() {

return birthday;

}

public void setBirthday(Date birthday) {

this.birthday = birthday;

}

public String getCity() {

return city;

}

public void setCity(String city) {

this.city = city;

}

public String getCountry() {

return country;

}

public void setCountry(String country) {

this.country = country;

}

public Date getCreatetime() {

return createtime;

}

public void setCreatetime(Date createtime) {

this.createtime = createtime;

}

public String getDept_id() {

return dept_id;

}

public void setDept_id(String dept_id) {

this.dept_id = dept_id;

}

public String getEducational() {

return educational;

}

public void setEducational(String educational) {

this.educational = educational;

}

public String getEmail() {

return email;

}

public void setEmail(String email) {

this.email = email;

}

 

 

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getLoginname() {

return loginname;

}

public void setLoginname(String loginname) {

this.loginname = loginname;

}

 

 

public String getAge() {

return age;

}

public void setAge(String age) {

this.age = age;

}

public String getGender() {

return gender;

}

public void setGender(String gender) {

this.gender = gender;

}

public String getMarried() {

return married;

}

public void setMarried(String married) {

this.married = married;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPosition() {

return position;

}

public void setPosition(String position) {

this.position = position;

}

public String getRemarks() {

return remarks;

}

public void setRemarks(String remarks) {

this.remarks = remarks;

}

public String getSalary() {

return salary;

}

public void setSalary(String salary) {

this.salary = salary;

}

public String getSchool() {

return school;

}

public void setSchool(String school) {

this.school = school;

}

}

sqlMap映射文件

 

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >

<sqlMap namespace="Employee">

  <resultMap id="Employee.EmployeeResult" class="com.huasoft.common.domain.Employee" >

    <result column="ID" property="id" jdbcType="VARCHAR" />

    <result column="LOGINNAME" property="loginname" jdbcType="VARCHAR" />

    <result column="NAME" property="name" jdbcType="VARCHAR" />

    <result column="AGE" property="age" jdbcType="VARCHAR" />

    <result column="BIRTHDAY" property="birthday" jdbcType="TIMESTAMP" />

    <result column="DEPT_ID" property="dept_id" jdbcType="VARCHAR" />

    <result column="POSITION" property="position" jdbcType="VARCHAR" />

    <result column="GENDER" property="gender" jdbcType="VARCHAR" />

    <result column="MARRIED" property="married" jdbcType="VARCHAR" />

    <result column="SALARY" property="salary" jdbcType="VARCHAR" />

    <result column="EDUCATIONAL" property="educational" jdbcType="VARCHAR" />

    <result column="COUNTRY" property="country" jdbcType="VARCHAR" />

    <result column="CITY" property="city" jdbcType="VARCHAR" />

    <result column="REMARKS" property="remarks" jdbcType="VARCHAR" />

    <result column="SCHOOL" property="school" jdbcType="VARCHAR" />

    <result column="CREATETIME" property="createtime" jdbcType="TIMESTAMP" />

    <result column="EMAIL" property="email" jdbcType="VARCHAR" />

    

  </resultMap>

 

  <select id="Employee.selectListByCondition"

parameterClass="java.util.HashMap"  

resultMap="Employee.EmployeeResult">

select id, loginname, age, birthday, dept_id, position, gender, 

married, salary, educational, country, city, remarks, school, createtime, email, 

name from t_employee +"条件自己写"

 

 

 

</select>

 

 

<!--  

<select id="Employee.selectListByConditionCount"

   resultClass="java.lang.Integer">

select count(*)

from t_employee

</select>-->

<!-- 

  <delete id="T_YW_TEST.deleteByPrimaryKey" parameterClass="com.sjq.dljxkp.bean.test.TYwTest" >

    delete from T_YW_TEST

    where BH = #BH:VARCHAR#

  </delete>

  

  <insert id="T_YW_TEST.insert" parameterClass="com.sjq.dljxkp.bean.test.TYwTest" >

    insert into T_YW_TEST (BH, XM, XB, AB, ZATZDM, ZATZMX, XZQH, JYAQ, CSRQ, SSJZ, DJDWJGDM, DJRDM,

      DJSJ, DQQSJG, DQQSMJ, DJBGSJ, DJBGBS, SHCJBS, JLMJ, SJJZBS)

    values (#BH:VARCHAR#, #XM:VARCHAR#, #XB:VARCHAR#, #AB:VARCHAR#, #ZATZDM:VARCHAR#,

      #ZATZMX:VARCHAR#, #XZQH:VARCHAR#,<isNull property="JYAQ">NULL,</isNull>

<isNotNull property="JYAQ">EMPTY_CLOB(),</isNotNull> #CSRQ:TIMESTAMP#, #SSJZ:DECIMAL#,

      #DJDWJGDM:VARCHAR#, #DJRDM:VARCHAR#, sysdate, #DQQSJG:VARCHAR#, #DQQSMJ:VARCHAR#,

      sysdate, #DJBGBS:VARCHAR#, #SHCJBS:VARCHAR#, #JLMJ:VARCHAR#, #SJJZBS:VARCHAR#)

  </insert>

  

  <update id="T_YW_TEST.updateByPrimaryKeySelective" parameterClass="com.sjq.dljxkp.bean.test.TYwTest" >

    update T_YW_TEST

    <dynamic prepend="set" >

      <isNotNull  property="XM" >

        XM = #XM:VARCHAR#,

      </isNotNull>

      <isNotNull  property="XB" >

        XB = #XB:VARCHAR#,

      </isNotNull>

      <isNotNull  property="AB" >

        AB = #AB:VARCHAR#,

      </isNotNull>

      <isNotNull  property="ZATZDM" >

        ZATZDM = #ZATZDM:VARCHAR#,

      </isNotNull>

      <isNotNull  property="ZATZMX" >

        ZATZMX = #ZATZMX:VARCHAR#,

      </isNotNull>

      <isNotNull  property="XZQH" >

        XZQH = #XZQH:VARCHAR#,

      </isNotNull>

       <isNotNull  property="JYAQ">

        JYAQ = EMPTY_CLOB(),

       </isNotNull>

      <isNotNull  property="CSRQ" >

        CSRQ = #CSRQ:TIMESTAMP#,

      </isNotNull>

      <isNotNull  property="SSJZ" >

        SSJZ = #SSJZ:DECIMAL#,

      </isNotNull>

      <isNotNull  property="DJDWJGDM" >

        DJDWJGDM = #DJDWJGDM:VARCHAR#,

      </isNotNull>

      <isNotNull  property="DJRDM" >

        DJRDM = #DJRDM:VARCHAR#,

      </isNotNull>

      <isNotNull  property="DJSJ" >

        DJSJ = #DJSJ:TIMESTAMP#,

      </isNotNull>

      <isNotNull  property="DQQSJG" >

        DQQSJG = #DQQSJG:VARCHAR#,

      </isNotNull>

      <isNotNull  property="DQQSMJ" >

        DQQSMJ = #DQQSMJ:VARCHAR#,

      </isNotNull>

      <isNotNull  property="DJBGBS" >

        DJBGBS = #DJBGBS:VARCHAR#,

      </isNotNull>

      <isNotNull  property="SHCJBS" >

        SHCJBS = #SHCJBS:VARCHAR#,

      </isNotNull>

      <isNotNull  property="JLMJ" >

        JLMJ = #JLMJ:VARCHAR#,

      </isNotNull>

      <isNotNull  property="SJJZBS" >

        SJJZBS = #SJJZBS:VARCHAR#,

      </isNotNull>

        DJBGSJ = sysdate

    </dynamic>

    where BH = #BH:VARCHAR#

  </update>

  -->

</sqlMap>

parameterClass是传入参数类型可以是对象、xml、基本类型、map

resultMap是返回类型可以是对象、xml、基本类型、map

 

ibatis可以自动映射,但前提是数据库的自动和实体对象属性要一致,不一致也可以用as '别名'

 

<?xml version="1.0" encoding="UTF-8" ?>

 

<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"

    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

 

<sqlMapConfig>

  <properties resource="examples/domain/database.properties"/>

  <transactionManager type="JDBC">

    <dataSource type="SIMPLE">

      <property value="${driver}" name="JDBC.Driver"/>

      <property value="${url}" name="JDBC.ConnectionURL"/>

      <property value="${username}" name="JDBC.Username"/>

      <property value="${password}" name="JDBC.Password"/>

    </dataSource>

  </transactionManager>

  

  <sqlMap resource="examples/domain/Person.xml"/> 

</sqlMapConfig>

 

 

ibatis配置文件

<?xml version="1.0" encoding="UTF-8" ?>

 

<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"

    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

 

<sqlMapConfig>

  <properties resource="examples/domain/database.properties"/>

  <transactionManager type="JDBC">

    <dataSource type="SIMPLE">

      <property value="${driver}" name="JDBC.Driver"/>

      <property value="${url}" name="JDBC.ConnectionURL"/>

      <property value="${username}" name="JDBC.Username"/>

      <property value="${password}" name="JDBC.Password"/>

    </dataSource>

  </transactionManager>

  

  <sqlMap resource="路径需要根据自己项目的实际情况填写"/> 

</sqlMapConfig>

 

数据库连接信息

####################################

# Database Connectivity Properties

####################################

driver=oracle.jdbc.driver.OracleDriver

url=jdbc:oracle:thin:@数据库IP地址:sid

username=数据库用户名

password=数据库密码

 

 

 

测试类test

package 项目包名;

 

import java.util.*;

import java.io.IOException;

 

import java.io.Reader;

 

import java.sql.SQLException;

 

import java.util.List;

 

import java.util.Date;

 

import com.ibatis.common.resources.Resources;

import com.ibatis.common.util.PaginatedList;

import com.ibatis.sqlmap.client.SqlMapClient;

import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import com.ibatis.sqlmap.client.event.RowHandler;

import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;

import com.ibatis.sqlmap.engine.impl.SqlMapClientImpl;

import com.ibatis.sqlmap.engine.mapping.statement.RowHandlerCallback;

 

public class Test {

 

 

 

public static void main(String[] args) throws Exception

{

String resource="路径根据自己项目的实际情况";

Reader reader=Resources.getResourceAsReader(resource);

SqlMapClient client=SqlMapClientBuilder.buildSqlMapClient(reader);

        List list=client.queryForList(statementId,"输入参数");

                循环遍历List

   }

 

http://shopsale.sinaapp.com

   

   

  

 

   

 

   

 

  

 

 

 

 

   发表时间:2014-04-01  
经典你麻痹啊
0 请登录后投票
   发表时间:2014-04-02  
恳请楼主自己删帖!
0 请登录后投票
   发表时间:2014-05-16  
楼主,请删了吧~
0 请登录后投票
   发表时间:2014-09-27  
我这样急于入门的,想快速上手的还行
0 请登录后投票
   发表时间:2014-09-28  
不是早就已经mybatis了吗
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics