ibatisの簡単な削除の変更

7169 ワード

これは簡単なibatis追加削除の例です.初心者の参考にする
このプログラムを使用する場合、ibatisのjarパッケージをダウンロードして実行します.

package com.test;

import java.io.Serializable;

/***
*
* @author doudou
*
*/
public class Person implements Serializable {

private int id;

private String name;

private String password;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}
}

package com.test;

import java.io.Reader;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class MySqlMapClient {

private static SqlMapClient sqlMapClient;

// ibatis
private static String path = "com/test/SqlMapConfig.xml";
static {
Reader reader = null;
try {
// ,
reader = Resources.getResourceAsReader(path);
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
} catch (Exception ex) {
ex.printStackTrace();
}
}

public static SqlMapClient getSqlMapClient() {
return sqlMapClient;
}

}


/******SqlMapConfig.xml***/



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




















/****jdbc.properties***/

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/icinfo
username=root
password=root




/***Person.xml***/


br>"http://www.ibatis.com/dtd/sql-map-2.dtd">



//










insert into person(name,password) values(#name#,#password#)



delete from person where id = #id#



update person set name=#name#,password=#password# where id=#id#






select * from person



select id,name,password from person








/**DAO ***/

package com.test;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.ibatis.sqlmap.client.SqlMapClient;

/***
*
* @author doudou
* Person.xml 。 。
*/
public class DAO {

private SqlMapClient sqlMapClient = MySqlMapClient.getSqlMapClient();

/**
*
* @param person
* @throws SQLException
*/
public void insertPerson(Person person) throws SQLException {

/* insert("insertPerson",person)
* insertPerson id
*
insert into person(name,password) values(#name#,#password#)

*
* person Person !
* */
sqlMapClient.insert("insertPerson", person);
}

public void deletePerson(Person person) throws SQLException {
// 。。。
sqlMapClient.delete("deletePerson", person);
}

public void updatePerson(Person person) throws SQLException {
// 。。。
sqlMapClient.update("updatePerson", person);
}

public Person getPerson(Person person) throws Exception {
/*
*
* Person.xml 。
* */
return (Person) sqlMapClient.queryForObject("getPerson", person);
}

public List getAllPerson() throws Exception {
/*
* List 。 , ,
* queryForList("getAllPerson", "name", 2,4);
* "getAllPerson" , id
* "name" name "" 。
* 2,4 hibernate setFirstResult(int arg0) & setMaxResults(int arg1);
* , 。 。
*
* queryForList("getAllPerson", "name"); 。
* */
return sqlMapClient.queryForList("getAllPerson", "name", 2,4);
//return sqlMapClient.queryForList("getAllPerson", "name");
}

public Map getAllPersonByMap() throws Exception {
/*
* queryForMap("getAllPersonByMap", null, "id"); Map
* null 。 。
* "id" Map key id 。
*
* */
return sqlMapClient.queryForMap("getAllPersonByMap", null, "id");
}

//
public int getCount() throws SQLException {
/*
* int 。
* */
return (Integer)sqlMapClient.queryForObject("getCount", null);
}
public Map get() throws Exception {
/*
* Map,
* */
return sqlMapClient.queryForMap("get", null, "count");
}