Ibatis学习之第一个小例子

SqlMapConfig.xml

<?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>
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
errorTracingEnabled="true"
maxRequests="32"
maxSessions="10"
maxTransactions="5"
useStatementNamespaces="true"/>

<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/test"/>
<property name="JDBC.Username" value="root"/>
<property name="JDBC.Password" value="billsxm"/>
<property name="Pool.MaximumActiveConnections" value="10" />
<property name="Pool.MaximumIdleConnections" value="5" />
<property name="Pool.MaximumCheckoutTime" value="120000" />
<property name="Pool.TimeToWait" value="500" />
<property name="Pool.PingQuery"
value="select 1 from userinfo" />
<property name="Pool.PingEnabled" value="false" />
<property name="Pool.PingConnectionsOlderThan" value="1" />
<property name="Pool.PingConnectionsNotUsedFor" value="1" />
</dataSource>
</transactionManager>
<sqlMap resource="com/mydomain/data/UserInfo.xml"/>
</sqlMapConfig>

UserInfo.xml

<?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="UserInfo">
<typeAlias alias="UserInfo" type="com.mydomain.domain.UserInfo"/>
<resultMap id="UserInfoResult" class="UserInfo">
<result property="id" column="ID"/>
<result property="name" column="NAME"/>
<result property="password" column="PASSWORD"/>
<result property="msg" column="MSG"/>
</resultMap>

<select id="selectAllInfo" resultMap="UserInfoResult">
select * from USERINFO
</select>

<select id="selectInfoByid" parameterClass="int" resultClass="UserInfo">
select
ID as id,
NAME as name,
PASSWORD as password,
MSG as msg
from USERINFO
where ID = #id#
</select>
<insert id="insertInfo" parameterClass="UserInfo">
<selectKey keyProperty="id" resultClass="int">
SELECT LAST_INSERT_ID() as value
</selectKey>
insert into USERINFO
(
NAME,
PASSWORD,
MSG)
values(#name#,#password#,#msg#)
</insert>
<update id="updateInfo" parameterClass="UserInfo">
update USERINFO set
NAME = #name#,
PASSWORD = #password#,
MSG = #msg#
where ID = #id#
</update>
<delete id="deleteInfoByid" parameterClass="int">
delete from USERINFO where ID = #id#
</delete>
</sqlMap>




UserInfo.java

package com.mydomain.domain;

public class UserInfo {

private int id;
private String name;
private String password;
private String msg;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}


}


Example.java

package com.mydomain.data;

import java.io.Reader;
import java.sql.SQLException;
import java.util.List;

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

public class Example {

/**
* @param args
*/
private static SqlMapClient sqlMapper;
private static String resource = "com/mydomain/data/SqlMapConfig.xml";

/**
* load the SqlMapConfig.xml
*/
static {
try{
Reader reader = Resources.getResourceAsReader(resource);
sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
/**
* 查询所有信息
* @return
* @throws SQLException
*/
@SuppressWarnings("unchecked")
public static List<UserInfo> selectAllInfo() throws SQLException{
return sqlMapper.queryForList("selectAllInfo", UserInfo.class);
}

/**
* 根据id查找
* @return
* @throws SQLException
*/
@SuppressWarnings("unchecked")
public static List<UserInfo> selectInfoByid(int id) throws SQLException{
return sqlMapper.queryForList("selectInfoByid", id);
}

/**
* 插入信息
* @param userInfo
* @throws SQLException
*/
public static void insertInfo(UserInfo userInfo) throws SQLException{
sqlMapper.insert("insertInfo", userInfo);
}

/**
* 更新信息
* @param userInfo
* @throws SQLException
*/
public static void updateInfo(UserInfo userInfo) throws SQLException{
sqlMapper.update("updateInfo", userInfo);
}

public static void deleteInfoByid(int id) throws SQLException{
sqlMapper.delete("deleteInfoByid", id);
}
public static void main(String[] args) {
// UserInfo userInfo = new UserInfo();
// userInfo.setName("cici");
// userInfo.setPassword("ciiii");
// userInfo.setMsg("Hello CiCi");

try {
sqlMapper.startTransaction();
// List<UserInfo> info = selectInfoByid(2);
// for(int i=0;i<info.size();i++){
// System.out.println(info.get(i).getId());
// System.out.println(info.get(i).getName());
// System.out.println(info.get(i).getPassword());
// System.out.println(info.get(i).getMsg());
// }
//update
UserInfo userInfo = new UserInfo();
userInfo.setId(new Integer(1));
userInfo.setName("Bill.Zhang");
userInfo.setPassword("1234");
userInfo.setMsg("This is update message.");
updateInfo(userInfo);
sqlMapper.commitTransaction();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值