Rhythmk 一步一步学 JAVA(11)Ibatis 环境配置
2024-09-29 18:30:15
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
最新文章
- 【一周读书】All life is problem solving
- 为简单而努力:Android封装类详解
- xcode 创建项目 勾选 git 出现警告
- jQuery.form.js jQuery ajax异步提交form
- Calculating a bearing between points in location-aware apps
- Android-AttributeSet详解
- HDU 3660 Alice and Bob&#39;s Trip
- linux 下apache安装、启动和配置
- Div布局案例
- 于iOS跳转到应用程序AppStore
- kvm学习小计
- css的定位和浮动
- 03中间件mycat对pxc集群的分片处理
- Android Stdio的问题
- 了解 IP 地址,默认网关,子网掩码,DNS 的概念和作用。
- Git-git push -u为何第二次不用指定-u?
- 更改pip源至国内镜像
- 23种设计模式之策略模式(Strategy)
- JS给HTML5页面<;Select>;<;/Select>;绑定选中项
- .net core初试 --- 控制台程序
热门文章
- stark组件03
- js之10天内免登陆
- [pandas] SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
- [sklearn]性能度量之AUC值(from sklearn.metrics import roc_auc_curve)
- PageViewController和UIPageControl
- selenium 上传文件方法补充——SendKeys、win32gui
- [CF895E]Eyes Closed
- LeetCode 739. Daily Temperatures
- .ncx文件剖析
- 系列文章--Enterprise Library文章总结