入门请看http://blog.csdn.net/liumengcheng/article/details/34409783
首先创建表
create table T_USERS
(
ID NUMBER not null,
NAME VARCHAR2(30),
SEX VARCHAR2(3),
BIRS DATE,
MESSAGE CLOB
);
create sequence SEQ_T_USERS_ID
minvalue 1
maxvalue 99999999
start with 1
increment by 1
cache 20;
package com.southgis.scout.demo;
import java.util.Date;
/**
* 用户类
*
* @author lmc
*
*/
public class User {
private String name;
private String sex;
private Integer id;
private Date birs;
private String message;
public String getMessage() {
return message;
}
public void setMessage(String pMessage) {
this.message = pMessage;
}
public Date getBirs() {
return birs;
}
public void setBirs(Date pbirs) {
this.birs = pbirs;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String psex) {
this.sex = psex;
}
public Integer getId() {
return id;
}
public void setID(Integer pid) {
this.id = pid;
}
public User() {
}
}
package com.southgis.scout.demo;
import java.util.List;
import com.southgis.scout.demo.User;
public interface UsersMapper {
public void add(User t);
public void update(User t);
public void updateBySelective(User t);
public void delete(Object id);
public User queryById(Object id);
public List queryBySelective(User t);
public int queryByCount(User t);
public List queryByList(User t);
}
package com.southgis.scout.demo;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.sql.CLOB;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
public class OracleClobTypeHandler implements TypeHandler
package com.southgis.scout.demo;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Date;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.southgis.scout.demo.UsersMapper;
import com.southgis.scout.demo.User;
public class main {
/**
* 获得MyBatis SqlSessionFactory
* SqlSessionFactory负责创建SqlSession,一旦创建成功,就可以用SqlSession实例来执行映射语句,commit,rollback,close等方法。
* @return
*/
private static SqlSessionFactory getSessionFactory() {
SqlSessionFactory sessionFactory = null;
String resource = "configuration.xml";
try {
sessionFactory = new SqlSessionFactoryBuilder().build(Resources
.getResourceAsReader(resource));
} catch (IOException e) {
e.printStackTrace();
}
return sessionFactory;
}
public static void main(String[] args) {
SqlSession sqlSession = getSessionFactory().openSession();
UsersMapper dao = sqlSession.getMapper(UsersMapper.class);
//删除表中所有信息
User nullBean = new User();
List delList = dao.queryByList(nullBean);
if(delList != null) {
for(User user : delList) {
dao.delete(user.getId());
}
}
DateFormat dd=new SimpleDateFormat("yyyy-MM-dd");
Date date=null;
try {
date = dd.parse("1985-01-01");
} catch (ParseException e) {
e.printStackTrace();
}
//新增
User bean = new User();
bean.setName("ding");
bean.setSex("男");
bean.setBirs(date);
bean.setMessage("This is Clob!");
dao.add(bean);
List list = dao.queryByList(nullBean);
if(list != null) {
for(User user : list) {
System.out.println(user.getName()+user.getMessage());
}
}
//查询并更新
bean = new User();
bean.setName("ding");
List queList = dao.queryByList(bean);
if(queList != null) {
for(User user : list) {
user.setSex("女");
dao.updateBySelective(user);
}
}
list = dao.queryByList(nullBean);
if(list != null) {
for(User user : list) {
System.out.println(user.getSex());
}
}
//查询并更新
bean = new User();
bean.setName("ding");
List queList2 = dao.queryByList(bean);
if(queList != null) {
for(User user : queList2) {
user.setSex("男");
user.setMessage("");
dao.update(user);
}
}
list = dao.queryByList(nullBean);
if(list != null) {
for(User user : list) {
System.out.println(user.getSex());
}
}
int num = dao.queryByCount(nullBean);
System.out.println("num=" + num);
//默认是不提交的,所以这里手工提交
sqlSession.commit();
}
}
t_users
id,name,sex,birs,message
where 1=1
and id = #{id}
and name like concat(concat('%', '${name}'), '%')
and sex like concat(concat('%', '${sex}'), '%')
and birs = #{birs}
and message = #{message}
select seq_t_users_id.nextval as id from dual
insert into t_users(id,name,sex,birs,message) values(#{id},#{name},#{sex},#{birs},#{message,jdbcType=CLOB})
update t_users set name=#{name},sex=#{sex},birs=#{birs},message=#{message} where id=#{id}
update t_users set
name=#{name},
sex=#{sex},
birs=#{birs},
message=#{message},
where id=#{id}
delete from t_users where id = #{id}