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();
}
}
}
分享到:
相关推荐
ibatis ,入门例子
一个是纯ibatis的例子,还有一个是和spring2.5整合的例子。第二个例子主要代码测试在CopyOfUserDaoTest.java这个类里面,全部使用的是最新注解方式来注入和测试的。欢迎大家拍砖,如有不懂,请电邮:xnxqs@163.com ...
采用的框架是struts2.1.8,ibatis3,guice2.0,完整的产品模块和权限模块(部分功能)...自我认为是学习Ibatis3比较好的例子,覆盖到Ibatis3的各个方面。 最近很穷,所以资源分高了些。没有分的朋友,留下email我发给你。
Spring ibatis dwr2 extjs例子2,接例子第一部分
开始学习iBATIS一段时间了,这是第一次做iBATIS和spring整合的例子,测试运行成功,含源代码,代码中有规范的注释,希望能帮助读者。
第一篇 数据库基础篇 第1章 Java和数据库 1.1 Java概述 1.2 Java的开发和运行环境 1.3 数据库概述 1.4 数据持久层 1.5 本章小结 第2章 SQL语句基础之DDL 2.1 DDL基础 2.2 DDL操作视图 2.3 本章...
第一篇的根据id查询学生的例子中,稍做修改: 建立StudentDAO接口 package cn.pf.ibatis.dao; import java.util.List; import cn.pf.ibatis.domain.Student; public interface StudentDAO { public Student ...
查询出列表,也就是返回list, 在我们这个例子中也就是 List<User> , 这种方式返回数据,需要在User.xml 里面配置返回的类型 resultMap, 注意不是 resultType, 而这个resultMap 所对应的应该是我们自己配置的 ...
9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. <tx:advice/> 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 插入事务操作 9.5.8. 结合AspectJ使用 @Transactional 9.6. 编程...
9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. <tx:advice/> 有关的设置 9.5.6. 使用 @Transactional 9.5.6.1. @Transactional 有关的设置 9.5.7. 插入事务操作 9.5.8. 结合AspectJ...
9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. <tx:advice/> 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 插入事务操作 9.5.8. 结合AspectJ使用 @Transactional 9.6. 编程...
9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. <tx:advice/> 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 事务传播 9.5.8. 通知事务操作 9.5.9. 结合AspectJ使用 @...
9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. <tx:advice/> 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 事务传播 9.5.8. 通知事务操作 9.5.9. 结合AspectJ使用 @...