`
liangfeng366
  • 浏览: 76658 次
  • 性别: Icon_minigender_1
  • 来自: 安徽
社区版块
存档分类
最新评论

iBATIS一对一查询操作

阅读更多
iBATIS配置
1。webapp/web-info/spring/appServlet/datasource.xml中配置数据源 事务Aop 日志Aop等信息
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.appress.jdbctemplate"/>
<!-- 数据数的配置信息 -->
<util:properties id="dbconfig" location="classpath:dbconfig.properties"/>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="#{dbconfig.driverClassName}" />
<property name="username" value="#{dbconfig.username}"/>
<property name="password" value="#{dbconfig.password}"/>
<property name="url" value="#{dbconfig.url}"/>
<property name="initialSize" value="2"/>
<property name="maxActive" value="3"/>
</bean>

<bean id="mySqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocations">
<list>
<value>classpath:SqlMapConfig.xml</value>
</list>
</property>
<property name="dataSource" ref="dataSource"/>
</bean>

<!-- 把 customerDao 注入  在 WEB中用注解 直接导入-->
<bean id="customerDao" class="com.appress.jdbctemplate.SqlMapClientCustomerDao">
<!-- <property name="sqlMapClient" ref="sqlMapClient"></property> -->
</bean>
</beans>
2.Resource/dbconfig.properites 配置数据库信息
#Created by JInto - www.guh-software.de
#Thu Jan 14 21:38:08 CST 2010
driverClassName=com.mysql.jdbc.Driver
password=123456
url=jdbc:mysql://127.0.0.1:3306/test
username=root
3。Resource/SqlMapconfig.xml 导入各个resource

<?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>
<!-- enhancementEnabled 是否使用cglib来提高性能,如果classpath下没有cglib则被禁用 -->
<!-- cacheModelsEnabled 是否开启高速缓存,默认为true -->
<!-- lazyLoadingEnabled 是否开启延时加载,默认为true -->
<settings enhancementEnabled="true" cacheModelsEnabled="true"
lazyLoadingEnabled="true" errorTracingEnabled="true"/>
<!--   
放所有的 具体的配置文件放在这
每一个 domain放在这
<sqlMap resource="1/appress/domain/CustomerInfo.xml"/>
</sqlMapConfig>
4.dao
package com.appress.jdbctemplate;

import java.util.List;

public interface CustomerDao {
public CustomerInfo getById(int id);
}
5.
package com.appress.jdbctemplate;

import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ibatis.sqlmap.client.SqlMapClient;

public class SqlMapClientCustomerDao extends SqlMapClientDaoSupport implements CustomerDao {

// 在web中 直接注入
@Autowired
private SqlMapClient mySqlMapClient;

@PostConstruct
public void injectSqlMapClient(){
setSqlMapClient(mySqlMapClient);
System.out.println("******injectSqlMapClient()********"+mySqlMapClient.getClass().getName());
}

@SuppressWarnings("unchecked")
public List<CustomerInfo> getAll() {
return getSqlMapClientTemplate().queryForList("getAllCustomer");
}

public CustomerInfo getById(int id){
CustomerInfo customer=(CustomerInfo)this.getSqlMapClientTemplate().queryForObject("getCustomerById", id);
return customer;
}

}
6测试类
package com.appress.jdbctemplate;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {

ApplicationContext context=new FileSystemXmlApplicationContext("src/main/webapp/WEB-INF/spring/appServlet/datasourcetest.xml");
SqlMapClientCustomerDao customerDao=(SqlMapClientCustomerDao)context.getBean("customerDao");
//SqlMapClientCustomerDao customerDao=new SqlMapClientCustomerDao();
System.out.println(customerDao.getById(1000));

}

}
7。两个domain
(1)
package com.appress.jdbctemplate;

public class GoodsInfo {
private int id;
private  String name;
private double price;

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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString(){
return "GoodsInfo{"+"id:"+id+"name:"+name+"price:"+price+"}";
}
}
(2)
package com.appress.jdbctemplate;

import java.sql.Date;

public class CustomerInfo {

private int id;
private String first_name;
private String last_name;
private Date last_login;
private CustomerDetail customerDetail;

public CustomerDetail getCustomerDetail() {
return customerDetail;
}
public void setCustomerDetail(CustomerDetail customerDetail) {
this.customerDetail = customerDetail;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String firstName) {
first_name = firstName;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String lastName) {
last_name = lastName;
}

public Date getLast_login() {
return last_login;
}
public void setLast_login(Date lastLogin) {
last_login = lastLogin;
}

public String toString(){
return "{"+"id:"+id+" first_name:"+first_name+" last_name:"+last_name+" last_login:"
+last_login+customerDetail;
}


}
8 最重要的customer.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="CustomerInfo" type="com.appress.jdbctemplate.CustomerInfo"/>
<!-- (1)种方式 -->
<typeAlias alias="customerDetail" type="com.appress.jdbctemplate.CustomerDetail"/>
<resultMap class="CustomerInfo" id="result">
<result property="id" column="id"/>
<result property="first_name" column="first_name"/>
<result property="last_name" column="last_name"/>
<result property="last_login" column="last_login"/>
</resultMap>
<!--  (2)种方式
<resultMap class="customerDetail" id="customerDetail2">
<result property="customerDetailId" column="id"/>
<result property="data" column="data0"/>
</resultMap>

<resultMap class="CustomerInfo" id="resultDetail" extends="result">
<result property="customerDetail.customerDetailId" column="customer_detail_id"/>
<result property="customerDetail.data" column="customer_detail_data"/>
</resultMap>-->
<!-- 
<resultMap class="" id=""></resultMap>
<select id="getAllCustomer" resultMap="result">
select * from t_customer
</select>


<select id="getCustomerById" resultMap="resultDetail" parameterClass="int">
select c.id as id,c.first_name as first_name ,c.last_name as last_name,
c.last_login as last_login ,
cd.id as customer_detail_id,
cd.data0 as customer_detail_data

from t_customer c inner join t_customer_detail cd on
c.customer_detail=cd.id
where c.id=#value#
</select>
-->
<!-- (1)中方式 -->
<resultMap class="customerDetail" id="detailResult">
<result property="customerDetailId" column="id"/>
<result property="data" column="data0"/>
</resultMap>
<resultMap class="CustomerInfo" id="resultDetail" extends="result">
<result property="customerDetail" column="customer_detail" select="getCustomerDetailById"/>
</resultMap>
<select id="getCustomerById" resultMap="resultDetail" parameterClass="int">
select * from t_customer where id=#value#
</select>
<select id="getCustomerDetailById" resultMap="detailResult" parameterClass="int">
select * from t_customer_detail where id=#value#
</select>

</sqlMap>

数据库表:
create table t_customer(
       id  int not null,     
       first_name varchar(50) not null,      
       last_name varchar(50) not null,      
       last_login date ,
       customer_detail int ,    
       CONSTRAINT pk_CustomerId PRIMARY key(id),      
       CONSTRAINT fk_CustomerDetail foreign key(customer_detail)      
       REFERENCES t_customer_detail(id)    
)

create table t_customer_detail(
id int not null,
data0 varchar(100) not null,
constraint pk_customerdetail primary key(id)
)
分享到:
评论
3 楼 haoluziqi 2013-04-01  
2 楼 haoluziqi 2013-04-01  
[flash=200,200][url][img][list]
[*]
引用
[/list][/img][/url][/flash]
1 楼 haoluziqi 2013-04-01  
文字

相关推荐

    一个最简单的IBatis增删改查操作例子

    众所周知,IBatis逐渐流行,为了让大家尽快掌握IBatis,特上传了一个最简单的IBatis增删改查操作例子,希望对大家有用!

    ibatis 自己学的一个ibatis项目(只是打通了Oracle) 非常适合入门

    数据库自己建一张简单的表就行了,特别说明 只适合新手入门 只有三个java文件和三个xml配置文件 非常简单 但是包含了crud操作 非常适合新手入门,因为项目经理让我熟悉一下ibatis 我就自学写了一个,希望对你有帮助

    iBATIS实战

    书的最后给出了一个设计优雅、层次清晰的示例程序JGameStore,该示例涵盖全书的大部分知识点,可以作为iBATIS学习和Web开发的经典案例,非常值得深入研究。 本书既可为广大的开发人员(不仅仅是Web应用程序开发人员)...

    ibatis对数据库进行增删改查操作的小例子

    一个关于ibatis对数据库进行增删改查操作的小例子,简单易学,方便大家快速掌握和运用。

    ibatis2.x 详细介绍

    一对一关联............................................................................................ 28 延迟加载........................................................................................

    iBatis2学习笔记

    6.iBatis2学习笔记:一对多映射(双向).doc 7.iBatis2学习笔记:多对多映射(双向) .doc 8.iBatis2学习笔记:总结与思考.doc 9.iBatis2实体状态图解.doc 10.iBatis insert操作陷阱.doc 每章都有小例子。 呵呵,希望...

    ibatis开发指南_夏昕

    一对一关联............................................................................................28 延迟加载..........................................................................................

    ibatis开发指南中文版

    一对一关联............................................................................................28 延迟加载..........................................................................................

    持久层框架ibatis学习笔记

    没有Hibernate 的功能强大,但也有很多优势,对数据库的复杂操作,sql 语言的效率更高, 这一点是Hibernate 是所不及的。 总之,学习iBatis 是非常容易上手的,有过sql 和Hibernate 的基础,我跟着传智播客的视频 学...

    struts2+spring+ibatis实例

    基于struts2+spring+ibatis做的一个例子。 包含: 1.项目源代码,及所需要用的jar包; 2.数据库脚本。 环境: myeclipse + mysql + tomcat 功能: 1.登录; 2.对另一张表的增删改查操作; 3.设有拦截器功能...

    iBATIS学习笔记

    使用iBATIS提供的ORM机制,对业务逻辑实现人员而言,面对的是纯粹的Java对象, 这一层与通过Hibernate实现ORM 而言基本一致,而对于具体的数据操作,Hibernate 会自动生成SQL 语句,而iBATIS则要求开发者编写具体的...

    iBATIS 2.0 开发指南

    相对Hibernate和Apache OJB 等“一站式”ORM解决方案而言,ibatis 是一种“半 自动化”的ORM实现。 所谓“半自动”,可能理解上有点生涩。纵观目前主流的ORM,无论Hibernate 还是 Apache OJB,都对数据库结构提供了...

    ibatis —— docs.zip

    MyBatis生成器(MBG)是MyBatis MyBatis 和iBATIS的代码生成器。...MBG试图对简单CRUD(创建,检索,更新,删除)的大部分数据库操作产生重大影响。您仍将需要手工编写SQL和对象代码以进行联接查询或存储过程。

    ibatis 开发指南(pdf)

    26 一对一关联............................................................................................ 28 延迟加载.............................................................................

    ibatis 开发指南

    使用ibatis 提供的ORM机制,对业务逻辑实现人员而言,面对的是纯粹的Java对象, 这一层与通过Hibernate 实现ORM 而言基本一致,而对于具体的数据操作,Hibernate 会自动生成SQL 语句,而ibatis 则要求...

    iBATIS技术教程PPT和代码.rar

    iBATIS是一种“半自动化”的ORM实现。 所谓“半自动”,可能理解上有点生涩。纵观目前主流的 ORM,无论 Hibernate 还是Apache OJB,都对数据库结构提供了较为完整的封装,提供了从 POJO 到数据库表的全套映射机制。...

    iBatis开发资料

    相对Hibernate和Apache OJB 等“一站式”ORM解决方案而言,ibatis 是一种“半 自动化”的ORM实现。 所谓“半自动”,可能理解上有点生涩。纵观目前主流的ORM,无论Hibernate 还是 Apache OJB,都对数据库结构提供了...

    ibatis实现过程

    相对Hibernate和Apache OJB 等“一站式”ORM解决方案而言,ibatis 是一种“半 自动化”的ORM实现。 所谓“半自动”,可能理解上有点生涩。纵观目前主流的ORM,无论Hibernate 还是 Apache OJB,都对数据库结构提供了...

    ibatis开发指南

    相对Hibernate和Apache OJB等“一站式”ORM解决方案而言,ibatis 是一种“半  自动化”的ORM实现。  所谓“半自动”,可能理解上有点生涩。纵观目前主流的 ORM,无论 Hibernate 还是  Apache OJB,都对数据库...

    Struts2+Ibatis+Spring例子

    这是一个完整的S2SI框架,附jar包和建表语句,里面有添、删、改、查通用查询方法,并且,加了log4j,所以对数据库操作SQL都会在控制台打印出来,加有最新的jQuery插件1.7.2.min.js,建好表,部署完工程直接就可以...

Global site tag (gtag.js) - Google Analytics