由于某种原因我需要研究下ibatis,看了一下文件些了个入门示例共没有入门的同仁学习也算是自己做个笔记日后没准还能用上。
数据库:mysql5.5
ide:eclipse
首先在eclips的某个库下建个表
建标语句 写道
CREATE TABLE Account (
ACC_ID int ( 11 ) NOT NULL ,
ACC_FIRST_NAME varchar ( 50 ) default NULL ,
ACC_LAST_NAME varchar ( 50 ) default NULL ,
ACC_EMAIL varchar ( 80 ) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ACC_ID int ( 11 ) NOT NULL ,
ACC_FIRST_NAME varchar ( 50 ) default NULL ,
ACC_LAST_NAME varchar ( 50 ) default NULL ,
ACC_EMAIL varchar ( 80 ) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
其次domai对象
package com.mydomain.domain;
public class Account {
private int id;
private String firstName;
private String lastName;
private String emailAddress;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
}
其实对于入门的人来说这个才是最重要的:
<?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="Account"> <typeAlias alias="Account" type="com.mydomain.domain.Account"/> <resultMap id="AccountResult" class="Account"> <result property="id" column="ACC_ID"/> <result property="firstName" column="ACC_FIRST_NAME"/> <result property="lastName" column="ACC_LAST_NAME"/> <result property="emailAddress" column="ACC_EMAIL"/> </resultMap> <select id="selectAllAccounts" resultMap="AccountResult"> select * from ACCOUNT </select> <select id="selectAccountById" parameterClass="int" resultClass="Account"> select ACC_ID as id, ACC_FIRST_NAME as firstName, ACC_LAST_NAME as lastName, ACC_EMAIL as emailAddress from ACCOUNT where ACC_ID = #id# </select> <insert id="insertAccount" parameterClass="Account"> insert into ACCOUNT( ACC_ID, ACC_FIRST_NAME, ACC_LAST_NAME, ACC_EMAIL) values ( #id#, #firstName#, #lastName#, #emailAddress#) </insert> <update id="updateAccount" parameterClass="Account"> update ACCOUNT set ACC_FIRST_NAME = #firstName#, ACC_LAST_NAME = #lastName#, ACC_EMAIL = #emailAddress# where ACC_ID = #id# </update> <delete id="deleteAccountById" parameterClass="int"> delete from ACCOUNT where ACC_ID = #id# </delete> </sqlMap>
这个配置文件里边建立了Accout对象和数据库Account的关联关系,已经对象属性和表的字段之间的对应关系,同时定义了各种操作。如果之前接触过hibernate的话这个就不难理解了。
还有另外一个就是配置文件:
主要是设置连接的数据库的配置
<?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> <transactionManager type="JDBC" commitRequired="false"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/> <property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/eredg4?useUnicode=true&characterEncoding=UTF-8"/> <property name="JDBC.Username" value="root"/> <property name="JDBC.Password" value="admin"/> </dataSource> </transactionManager> <sqlMap resource="com/mydomain/data/Account.xml"/> </sqlMapConfig>
和Account.xml对应的各个操作的方法:
package com.mydomain.data;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.ibatis.common.resources.Resources;
import com.mydomain.domain.Account;
import java.io.Reader;
import java.io.IOException;
import java.util.List;
import java.sql.SQLException;
public class SimpleExample {
private static SqlMapClient sqlMapper;
static {
try {
Reader reader = Resources.getResourceAsReader("com/mydomain/data/SqlMapConfig.xml");
sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
// Fail fast.
throw new RuntimeException("Something bad happened while building the SqlMapClient instance." + e, e);
}
}
public static List selectAllAccounts () throws SQLException {
return sqlMapper.queryForList("selectAllAccounts");
}
public static Account selectAccountById (int id) throws SQLException {
return (Account) sqlMapper.queryForObject("selectAccountById", id);
}
public static void insertAccount (Account account) throws SQLException {
sqlMapper.insert("insertAccount", account);
}
public static void updateAccount (Account account) throws SQLException {
sqlMapper.update("updateAccount", account);
}
public static void deleteAccount (int id) throws SQLException {
sqlMapper.delete("deleteAccount", id);
}
}
现在就剩下测试文件了:
package com.mydomain.test;
import com.mydomain.data.SimpleExample;
import com.mydomain.domain.*;
public class AccountTest {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
/**
* 增
*/
/**
Account account =new Account();
account.setId(1);
account.setFirstName("Road");
account.setLastName("Horse");
account.setEmailAddress("hlh-ml@163.com");
SimpleExample.insertAccount(account);*/
/**
* 查
*/
Account account =new Account();
account=SimpleExample.selectAccountById(1);
System.out.println("LastName=="+account.getLastName());
}
}
到此ok,我把项目也传上来还没学过的可以当作入门的例子来学习。需要导入ibatis包和mysql数据库包。