`
isiqi
  • 浏览: 16351919 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

初学ibatis

阅读更多

1.下载ibatis-2.3.0.677.jar,数据库mysql-connector-java-5.1.5-bin.jar包

mysql下的sql语句:

use test;
create table User(
id int auto_increment primary key,
userName varchar(20),
userPwd varchar(20),
userAddr varchar(100)
);

insert into User values(null,'hello1','hello1pwd','address1');
insert into User values(null,'hello2','hello2pwd','address2');
insert into User values(null,'hello3','hello3pwd','address3');
commit;


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>

<!-- Configure a built-in transaction manager. If you're using an
app server, you probably want to use its transaction manager
and a managed datasource -->
<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/test"/>
<property name="JDBC.Username" value="root"/>
<property name="JDBC.Password" value="root"/>
</dataSource>
</transactionManager>

<!-- List the SQL Map XML files. They can be loaded from the
classpath, as they are here (com.domain.data...) -->
<sqlMap resource="wang/mydomain/User.xml"/>
<!-- List more here...
<sqlMap resource="com/mydomain/data/Order.xml"/>
<sqlMap resource="com/mydomain/data/Documents.xml"/>
-->

</sqlMapConfig>

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 namespace="User">

<!-- Use type aliases to avoid typing the full classname every time. -->
<typeAlias alias="User" type="wang.mydomain.User"/>

<!-- Result maps describe the mapping between the columns returned
from a query, and the class properties. A result map isn't
necessary if the columns (or aliases) match to the properties
exactly. -->
<resultMap id="AccountResult" class="User">
<result property="id" column="id"/>
<result property="userName" column="userName"/>
<result property="userPwd" column="userPwd"/>
<result property="userAddr" column="userAddr"/>
</resultMap>

<!-- Select with no parameters using the result map for Account class. -->
<select id="selectAllAccounts" resultMap="AccountResult">
select * from User
</select>

<!-- A simpler select example without the result map. Note the
aliases to match the properties of the target result class. -->
<select id="selectAccountById" parameterClass="int" resultClass="User">
select
id as id,
userName as userName,
userPwd as userPwd,
userAddr as userAddr
from User
where id = #id#
</select>

<!-- Insert example, using the Account parameter class -->
<insert id="insertAccount" parameterClass="User">
insert into User (
id,
userName,
userPwd,
userAddr
)
values (
#id#, #userName#, #userPwd#, #userAddr#
)
</insert>

<!-- Update example, using the Account parameter class -->
<update id="updateAccount" parameterClass="User">
update User set
id = #id#,
userName = #userName#,
userPwd = #userPwd#
where
id = #id#
</update>

<!-- Delete example, using an integer as the parameter class -->
<delete id="deleteAccountById" parameterClass="int">
delete from User where id = #id#
</delete>

</sqlMap>

User.java文件:

package wang.mydomain;

public class User {

private int id;
private String userName;
private String userPwd;
private String userAddr;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public String getUserAddr() {
return userAddr;
}
public void setUserAddr(String userAddr) {
this.userAddr = userAddr;
}




}

调用例子文件SimpleExample.java

package wang.mydomain;

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

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


/**
* This is not a best practices class. It's just an example
* to give you an idea of how iBATIS works. For a more complete
* example, see JPetStore 5.0 at http://www.ibatis.com.
*/
public class SimpleExample {

/**
* SqlMapClient instances are thread safe, so you only need one.
* In this case, we'll use a static singleton. So sue me. ;-)
*/
private static SqlMapClient sqlMapper;

/**
* It's not a good idea to put code that can fail in a class initializer,
* but for sake of argument, here's how you configure an SQL Map.
*/
static {
try {
Reader reader = Resources.getResourceAsReader("wang/mydomain/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 User selectAccountById (int id) throws SQLException {
return (User) sqlMapper.queryForObject("selectAccountById", id);
}

public static void insertAccount (User user) throws SQLException {
sqlMapper.insert("insertAccount", user);
}

public static void updateAccount (User user) throws SQLException {
sqlMapper.update("updateAccount", user);
}

public static void deleteAccount (int id) throws SQLException {
sqlMapper.delete("deleteAccountById", id); //修改过的
}


public static void main(String[] args) throws Exception{
//select all
List list = selectAllAccounts();
for(int i=0;i<list.size(); i++){
User user = (User)list.get(i);
System.out.println(""+user.getId()+" ;"+user.getUserName()+" ;"+user.getUserPwd()+" ;"+user.getUserAddr());
}

//select(int)
User user2 = selectAccountById(2);
System.out.println("查找到的user="+user2.getId()+" ;"+user2.getUserName()+" ;"+user2.getUserPwd()+" ;"+user2.getUserAddr());


//insert ok
// User user3 = new User();
// user3.setUserName("hello4");
// user3.setUserPwd("hello4pwd");
// user3.setUserAddr("address4");
//
// insertAccount(user3);

//update ok
User user4 = new User();
user4.setId(4);
user4.setUserName("upd_hello4");
user4.setUserPwd("upd_hello4pwd");
user4.setUserAddr("upd_address4");
updateAccount(user4);

//delete ok
deleteAccount(4);

}
}

备注:Oracle连接字符串:

一.连接oracle数据库
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@服务器ip:1521:数据库名称";
Connection conn=DriverManager.getConnection(url,"用户名","密码");

分享到:
评论

相关推荐

    ibatis 初学Demo

    这个例子是本人在初学ibatis 写的,对初学者来说是一个很好的例子,一看就明白, 如果不明白的地方请联系我,加入群:73624154

    ibatis框架所需的jar包

    对于初学ibatis者来说,百度上都只提到用ibatis-2.3.0.677.jar,然后在读取配置文件的时候引用不到Resources和SqlMapClient,那么就是少了一个ibatis-sqlmap-2.3.4.726.jar,我把这两个jar同时上传了,希望可以帮助...

    IBatis结合Struts开发初学文档及实例

    本文档集是为初学者设计的,旨在帮助他们了解如何将流行的Java框架IBatis与Struts进行整合,以构建一个数据库驱动的Web应用。"IBatis结合Struts开发初学文档及实例"包含了四个基础示例,它们提供了逐步的学习路径,...

    iBATIS初学经验.doc

    本文将基于作者的初学经验,分享iBATIS的一些核心概念、使用技巧以及学习过程中的常见问题。 首先,理解iBATIS的基本理念至关重要。它是一个基于Java的数据库访问框架,旨在解决对象关系映射(ORM)的问题,使得...

    webwork+spring+ibatis很适合初学者的实例

    WebWork、Spring 和 iBATIS 是三个非常重要的Java Web开发框架,它们的组合为初学者提供了丰富的学习资源。WebWork 是一个MVC(Model-View-Controller)框架,Spring 是一个全面的后端开发框架,而 iBATIS 是一个...

    ibatis+sql2005+etmv+exjs示例+源码

    给初学ibatis的同学,没有采用strus,sping.配置简单明了适合初学了解,数据库在DB文件下,需要恢复到本机,实现简单的权限管理,数据增加\删除\修改,采用exjs制作界面美观简洁.有不清楚可以发邮件 chenqian3@163.com ,...

    Ibatis入门例子,Ibatis教程

    Ibatis,全称为MyBatis,是一个优秀的Java持久层框架,它主要负责SQL映射,使得开发者能够将注意力集中在编写SQL语句上,而无需关注...因此,无论你是初学者还是有经验的开发者,掌握Ibatis都是提升自己技能的好选择。

    ibatis 相关使用文档及安装包ibatis 相关使用文档及安装包

    《iBATIS-SqlMaps-2-Tutorial.pdf》很可能是iBATIS SQL映射器的教程,对于初学者来说,这是快速上手的好资料。它可能涵盖了从安装iBATIS到创建第一个SQL Map,再到执行查询和更新操作的全过程。 《iBATIS_DBL-2.1.5...

    ibatis api 帮助文档+IBATIS 开发文档

    总的来说,IBATIS API和开发文档是学习和使用IBATIS不可或缺的资源,它们涵盖了IBATIS的所有关键特性和用法,无论你是初学者还是有经验的开发者,都能从中受益匪浅。通过深入理解和实践,可以充分利用IBATIS的灵活性...

    iBATIS初学者

    iBATIS 初学者资料

    C# IBatis IBatis基础 完整项目

    【描述】中提到的是一个基于C#的IBatis学习项目,适用于初学者。IBatis是一个流行的数据访问层框架,它允许开发者将SQL语句与应用程序代码分离,提高了代码的可维护性和灵活性。在.NET环境中,IBatis为C#开发者提供...

    IBATIS_IN_ACTION

    《IBATIS_IN_ACTION》是一本深度探讨iBATIS框架应用与实践的专业书籍,由Clinton Begin、Brandon Goodin和Larry Meadors...无论你是初学者还是有经验的开发者,都能从中获得有价值的见解和启示,提升自己的技能水平。

    ibatis小例子Demo

    这个"ibatis小例子Demo"是为了帮助初学者快速理解和掌握Ibatis的核心功能和基本用法。 Ibatis的主要特点包括: 1. **XML配置文件**:Ibatis通过XML配置文件来定义SQL语句、参数映射和结果映射,使得SQL与Java代码...

    Ibatis例子,很适合初学者

    对于初学者来说,掌握Ibatis的关键在于理解其核心概念和工作流程。首先,Ibatis的核心是SQL Map配置文件,其中包含了SQL语句和参数映射。SQL Map文件通常以XML格式存在,每个SQL语句被定义为一个元素,可以是SELECT...

    ibatis java初学例子

    对初学者非常有用: 本程序是一个简单ibatis(版本ibatis2.3.4.726)例子, 里面包含单表增,删,改,查。 SimpleExample.java 包含程序与测试. 我用到是sqlServer(其他的数据库,只需更改sqlMapConfig.xml里面的...

    ibatis

    3. **iBATIS-SqlMaps-2-Tutorial_cn.pdf**:这是iBATIS SQL Maps 2的中文教程,可能包含一系列逐步的教学章节,指导初学者从零开始学习iBATIS,包括安装环境、创建第一个SQL Map、处理结果集、使用动态SQL以及处理...

    01_传智播客ibatis教程_准备ibatis环境

    这通常意味着内容深入浅出,易于理解,对于初学者或希望提升iBatis技能的开发者来说极具价值。 【标签】"ibatis"直接指出了本教程的核心主题,即iBatis。iBatis是一个优秀的SQL映射框架,它允许开发者将SQL语句与...

    IBatis .NET框架实例

    本文将深入探讨如何在C# .NET环境中使用IBatis进行数据库操作,这对于初学者来说是一份宝贵的资源。 **一、IBatis .NET简介** IBatis(现在称为MyBatis)是一个轻量级的持久层框架,它允许开发者将SQL语句直接嵌入...

    Ibatis 练习Demo和笔记

    总之,这个"Ibatis 练习Demo和笔记"资源为初学者提供了全面的学习路径,从理论到实践,逐步掌握Ibatis这一强大持久层框架的使用。通过深入研究并实践,不仅可以提升数据库操作技能,还能为后续的Spring全家桶学习...

    iBatis文档

    iBatis 是一款开源的Java持久层框架,它允许开发者将SQL语句直接映射到Java对象,从而简化了数据库访问的...无论是初学者还是有经验的开发者,都应该深入阅读并理解这些文档,以便在实际项目中充分利用iBatis的优势。

Global site tag (gtag.js) - Google Analytics