个人做商机网站如何盈利,公司注册网站及流程,北京官网首页,移动端网站怎么做一、ibatis的简介 ibatis是什么东西就不介绍了#xff0c;自己去找谷老师。 这里讲下自己的使用体会。之前自己学过Hibernate#xff0c;是看尚学堂的视频教学的#xff0c;看完以后发现Hibernate体系中的内容真的很多#xff0c;什么N-N关联、HSQL、缓存管理等等#xff… 一、ibatis的简介 ibatis是什么东西就不介绍了自己去找谷老师。 这里讲下自己的使用体会。之前自己学过Hibernate是看尚学堂的视频教学的看完以后发现Hibernate体系中的内容真的很多什么N-N关联、HSQL、缓存管理等等看得我真的很晕想要一时间掌握还真不可能反正我现在也不是特别明白。后来新的公司是用ibatis所以自己回去研究了下结果发现ibatis真的很简单体系结构相比Hibernate易化多了。在自己搭建了一个简单的示例后体会更深一个ibatis的配置文件、domain对象及其SQLMapper即可搞定而且在SQLMapper中你只要专注与你的SQL即可。所以以后要是有人问我怎么去学ORM层的框架我会推荐他去学ibatis而不是Hibernate因为这样简单上手的框架能让你一开始就对ORM的理解更加快速和易于掌握。 二、ibatis的下载 The original ibatis project team has moved to MyBatis hosted at Google Code. Seehttp://www.mybatis.org/ for more 下载地址http://code.google.com/p/mybatis/downloads/list mybatis3.1.1- ralease 三、简单的示例应用 下图是我的搭建图其中log4j是用来记录操作记录日志的可以参见官方的pdf文档readMe.txt是本人的自己添加的说明文档没有这两个文档也没关系。 1 既然是ORM层的框架首先我们应该建立数据库及表SQL如下 CREATE TABLE goods (id int(11) NOT NULL DEFAULT 0,category_id int(11) DEFAULT NULL,name varchar(100) DEFAULT NULL,price decimal(10,0) DEFAULT NULL,description varchar(100) DEFAULT NULL,acount int(11) DEFAULT NULL,update_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,PRIMARY KEY (id)
)导入两条数据 INSERT INTO goods VALUES (1,1,Nokia-L900,3010,Windows7 Mobile Phone,1,2012-07-15);
INSERT INTO goods VALUES (2,1,Moto-525,1800,Andriod 2.2,15,2012-007-15); 2 创建表结构所对应的domain对象 package com.csdn.kane.domain;import java.sql.Timestamp;public class Goods {private int id;private int categoryId;private String name;private float price;private String description;private int acount;private Timestamp updateTime;public int getId() {return id;}public void setId(int id) {this.id id;}public int getCategoryId() {return categoryId;}public void setCategoryId(int categoryId) {this.categoryId categoryId;}public String getName() {return name;}public void setName(String name) {this.name name;}public float getPrice() {return price;}public void setPrice(float price) {this.price price;}public String getDescription() {return description;}public void setDescription(String description) {this.description description;}public int getAcount() {return acount;}public void setAcount(int acount) {this.acount acount;}public Timestamp getUpdateTime() {return updateTime;}public void setUpdateTime(Timestamp updateTime) {this.updateTime updateTime;}
} 3 配置ibatis的配置文件 ?xml version1.0 encodingUTF-8 ?
!DOCTYPE configuration
PUBLIC -//mybatis.org//DTD Config 3.0//EN
http://mybatis.org/dtd/mybatis-3-config.dtd
configurationtypeAliasestypeAlias aliasGoods typecom.csdn.kane.domain.Goods//typeAliasesenvironments defaultdevelopmentenvironment iddevelopmenttransactionManager typeJDBC /dataSource typePOOLEDproperty namedriver valuecom.mysql.jdbc.Driver /property nameurl valuejdbc:mysql://127.0.0.1:3306/XiaoqingTest /property nameusername valueroot /property namepassword value08073440 //dataSource/environment/environmentsmappersmapper classcom.csdn.kane.dao.GoodsMapper//mappers
/configuration 4 建立GoodsMapper接口 package com.csdn.kane.dao;import org.apache.ibatis.annotations.Select;import com.csdn.kane.domain.Goods;public interface GoodsMapper {Select(SELECT * FROM Goods WHERE id#{id})public Goods selectGoods(int id);
} 5 接下来就可以自己写一个主方法来测试下了 package com.csdn.kane.test;import java.io.IOException;
import java.io.InputStream;import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;import com.csdn.kane.dao.GoodsMapper;
import com.csdn.kane.domain.Goods;public class TestMybitas {public static void main(String[] args) throws IOException {//最基本的mybitas示例方法TestMybitas.testMethod();}public static void testMethod() throws IOException{String resource mybitasConfigration.xml;InputStream inputStream Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);SqlSession session sqlSessionFactory.openSession();try {GoodsMapper mapper session.getMapper(GoodsMapper.class);Goods goods mapper.selectGoods(1);System.out.println(good description:goods.getDescription());} finally {session.close();}}} 运行后你就可以看到结果了。这就是一个最简单的mybatis应用。 从上面的流程大家可以看到这其实和Hibernate是比较相似的或者说ORM层的框架大多也是这样的1.首先你要建立数据库及表结构2.然后创建表结构所对应的domain对象(当然有些会自动生成表结构)3.接着你要配置你的ORM框架的配置文件(包括与数据库的链接配置还有其他内容),4.你要创建domain对象与表结构的映射关系或者sql操作(比如在Hibernate中是XXX.hbm.xml在mybatis中是XXXMapper.java接口文件或者是XXXMapper.xml配置文件)5.最后你就可以写个测试类了。 四、总结 这样你就知道什么是ORM层框架了就是这么简单当然还有很多深入的内容这就得靠你自己去学习了。 推荐学习的时候多看看官方的文档或是API比如在mybatis的下载文件中有mybatis-3.1.1.pdf文档里面有很多讲解及简单的学习示例是很不错的我在学的时候就是这样看文档学的。 转载于:https://www.cnblogs.com/java-source/archive/2012/07/15/2604336.html