1.项目文件分布。

2、example1.java:

package com.rhythmk.example1;

import java.io.IOException;
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; public class example1 { private static String resource = "com\\rhythmk\\example1\\SqlMapConfig.xml";
private static SqlMapClient sqlMapClient = null;
static {
System.out.println(System.getProperty("user.dir"));
try {
Reader reader = Resources.getResourceAsReader(resource);
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
} public static void main(String[] args) {
// TODO Auto-generated method stub GetAllUser(); GetUserById(1);
} /*
* 根据ID 获取User
*/
public static void GetUserById(Integer id) {
User user = null;
try { sqlMapClient.startTransaction(); user = (User) sqlMapClient.queryForObject("selectById",
new Integer("1"));
System.out.println(user.getName());
sqlMapClient.commitTransaction();
} catch (Exception e) {
e.printStackTrace();
}
} public static void GetAllUser() {
List<User> list = null;
try { list = (List<User>) sqlMapClient.queryForList("selectAll");
for (User user : list) {
System.out.println(user.getId() + "," + user.getName());
} } catch (Exception e) {
e.printStackTrace();
} } }

  

3、User.java:

package com.rhythmk.example1;

public class User {

	 public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return Age;
}
public void setAge(int age) {
Age = age;
}
private int Id;
private String name;
private int Age; }

  

4、SqlMap.properties:

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

5、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>
<!-- 数据库连接的属性文件 -->
<properties resource="com\rhythmk\example1\SqlMap.properties" /> <transactionManager type="JDBC"><!-- type:定义了ibatis的事务管理器有3种,JDBC、JTA、EXTERNAL -->
<dataSource type="SIMPLE"><!-- type属性指定了数据源的连接类型有3种,SIMPLE、DBCP、JNDI -->
<property value="${driver}" name="JDBC.Driver" />
<property value="${url}" name="JDBC.ConnectionURL" />
<property value="${username}" name="JDBC.Username" />
<property value="${password}" name="JDBC.Password" />
</dataSource>
</transactionManager> <!-- 实体类和数据库表的映射 -->
<sqlMap resource="com\rhythmk\example1\User.xml" /> </sqlMapConfig>

6.User.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>
<typeAlias alias="User" type="com.rhythmk.example1.User" />
<!-- 查询所有记录 -->
<select resultClass="User" id="selectAll">
SELECT id, name ,age from `user`
</select> <!-- 精确查询 按照条件查询记录 按照部门编号DEPTNO查询 -->
<select parameterClass="int" resultClass="User" id="selectById">
SELECT id, name ,age from `user` where id=#id#
</select>
</sqlMap>

SQL:

CREATE TABLE `user` (
`Id` int(11) NOT NULL,
`Name` varchar(32) CHARACTER SET utf8 NOT NULL,
`Age` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

http://files.cnblogs.com/rhythmK/IbatisExample.rar

最新文章

  1. 【一周读书】All life is problem solving
  2. 为简单而努力:Android封装类详解
  3. xcode 创建项目 勾选 git 出现警告
  4. jQuery.form.js jQuery ajax异步提交form
  5. Calculating a bearing between points in location-aware apps
  6. Android-AttributeSet详解
  7. HDU 3660 Alice and Bob&#39;s Trip
  8. linux 下apache安装、启动和配置
  9. Div布局案例
  10. 于iOS跳转到应用程序AppStore
  11. kvm学习小计
  12. css的定位和浮动
  13. 03中间件mycat对pxc集群的分片处理
  14. Android Stdio的问题
  15. 了解 IP 地址,默认网关,子网掩码,DNS 的概念和作用。
  16. Git-git push -u为何第二次不用指定-u?
  17. 更改pip源至国内镜像
  18. 23种设计模式之策略模式(Strategy)
  19. JS给HTML5页面&lt;Select&gt;&lt;/Select&gt;绑定选中项
  20. .net core初试 --- 控制台程序

热门文章

  1. stark组件03
  2. js之10天内免登陆
  3. [pandas] SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
  4. [sklearn]性能度量之AUC值(from sklearn.metrics import roc_auc_curve)
  5. PageViewController和UIPageControl
  6. selenium 上传文件方法补充——SendKeys、win32gui
  7. [CF895E]Eyes Closed
  8. LeetCode 739. Daily Temperatures
  9. .ncx文件剖析
  10. 系列文章--Enterprise Library文章总结