spring+ibatis

原创 2012年03月22日 19:26:03

搭建环境

   1——————

      File -->new -->project-->java下的Tomcat Project -->next-->项目名Spring+Ibatis

     在lib下加入包,ibatis-common-2.jar,ibatis-dao-2.jar,ibatis-sqlmap-2.jar,ojdbc14.jar,commons-logging-1.0.4.jar,spring.jar几个

    右键项目Spring+Ibatis--->Properties--->Java Bulid  Path --->Libraries-->add JARS-->添加这些包---->ok

   2——————

   创建表

create table Test_String_64(
  id          varchar2(40)    not null,
  no          varchar2(40)    not null,
  name        varchar2(40)    not null,
  password    varchar2(40)    not null,
  age         varchar2(40)    not null
)

   3——————

  在项目下建两个文件夹

    ---------src/java

   ----------src/reource

例如图:

  

   在src/resource下加入三个文件

     1————————-applicationContext-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
        "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
        
    <beans>

    <bean id="person"
		class="com.neusoft.test.spib.pojo.Person" >
		<constructor-arg index="0" value="111"/>
		<constructor-arg index="1" value="111" />
		<constructor-arg index="2" value="韩超aa"/>
		<constructor-arg index="3" value="123456" />
		<constructor-arg index="4" value="123"/>
	</bean>
	<bean id="person1"
		class="com.neusoft.test.spib.pojo.Person" >
		<constructor-arg index="0" value="222"/>
		<constructor-arg index="1" value="222"/>
		<constructor-arg index="2" value="于海"/>
		<constructor-arg index="3" value="345"/>
		<constructor-arg index="4" value="123"/>
	</bean>
	<bean id="person2"
		class="com.neusoft.test.spib.pojo.Person" >
		<constructor-arg index="0" value="333"/>
		<constructor-arg index="1" value="333"/>
		<constructor-arg index="2" value="好啊"/>
		<constructor-arg index="3" value="555"/>
		<constructor-arg index="4" value="444"/>
	</bean>
	<!--静态工厂实例化-->
   <bean id="beanFactory" 
			class="com.neusoft.test.spib.pojo.Person" factory-method="StaticCreatePerson"/>
	<!--动态工厂实例化-->
		   	 <bean id="person4" class="com.neusoft.test.spib.pojo.Person"/>
             <bean id="person5" factory-bean="person4" factory-method="CreatePerson"/>    
             <bean id="persons" class=" com.neusoft.test.spib.pojo.Person">
    <property name="personList">
        <list>
            <ref bean="person"/>
            <ref bean="person1"/>
            <ref bean="person2"/>
            <ref bean="beanFactory"/>
            <ref bean="person5"/>
        </list>
    </property>
</bean>
	 </beans>
	   
				


 1————————-sql-map-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
	<settings 
		cacheModelsEnabled="true" 
		enhancementEnabled="true"
		lazyLoadingEnabled="true" 
		errorTracingEnabled="true" 
		maxRequests="32"
		maxSessions="10" 
		maxTransactions="5" 
		useStatementNamespaces="false" />
		
	<transactionManager type="JDBC">
		<dataSource type="SIMPLE">
			<property name="JDBC.Driver"
				value="oracle.jdbc.driver.OracleDriver" />
			<property name="JDBC.ConnectionURL"
				value="jdbc:oracle:thin:@192.168.137.23:1521:unieap" />
			<property name="JDBC.Username" value="tabuser" />
			<property name="JDBC.Password" value="tabuser" />
			<property name="Pool.MaximumActiveConnections" value="10" />
			<property name="Pool.MaximumIdleConnections" value="5" />
			<property name="Pool.MaximumCheckoutTime" value="120000" />
			<property name="Pool.TimeToWait" value="500" />
			<property name="Pool.PingQuery" value="select 1 from dual" />
			<property name="Pool.PingEnabled" value="false" />
			<property name="Pool.PingConnectionsOlderThan" value="1" />
			<property name="Pool.PingConnectionsNotUsedFor" value="1" />
		</dataSource>
	</transactionManager>

	<sqlMap resource="person.xml" />
</sqlMapConfig>




1——————————person.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="User">
	<typeAlias alias="person" type="com.neusoft.test.spib.pojo.Person" />
	
	<insert id="insertPerson" parameterClass="person">
		INSERT INTO Test_String_64 (id,no,name,password,age) VALUES (#id#,#no#, #name#,#password#,#age# )
	</insert>
	
	<select id="getPerson" parameterClass="java.lang.String" resultClass="person">
		<![CDATA[
			SELECT id,no,name,password,age FROM Test_String_64 
		]]>
	</select>
</sqlMap> 

    在dao下加入PersonDao.java接口

package com.neusoft.test.spib.dao;

import java.util.List;

import com.neusoft.test.spib.pojo.Person;

public interface PersonDao {
    public boolean insert(Person person);


    public List<Person> query();

    
    public boolean insertPersons(List<Person> personList);

}

  在impl下加入PersPersonImpl类    实现接口

package com.neusoft.test.spib.impl;


import java.io.Reader;
import java.util.List;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.neusoft.test.spib.dao.PersonDao;
import com.neusoft.test.spib.pojo.Person;


public class  PersonImpl implements PersonDao {


    public boolean insert(Person person) {
        
        String resource="sql-map-config.xml";
        SqlMapClient sqlMapClient=null;
        try{
            Reader reader=Resources.getResourceAsReader(resource);
            sqlMapClient=SqlMapClientBuilder.buildSqlMapClient(reader);
            sqlMapClient.startTransaction();
            sqlMapClient.insert("insertPerson", person);
            sqlMapClient.commitTransaction();
            return true;
        }catch (Exception e) {
            return false;
        }
        
    }

    public List<Person> query() {
        
        return null;
    }

    public boolean insertPersons(List<Person> personList) {
        String resource="sql-map-config.xml";
        SqlMapClient sqlMapClient=null;

        for(Person p:personList){
    try{
        Reader reader=Resources.getResourceAsReader(resource);
        sqlMapClient=SqlMapClientBuilder.buildSqlMapClient(reader);
        sqlMapClient.startTransaction();
        sqlMapClient.insert("insertPerson", p);
        sqlMapClient.commitTransaction();     
    }catch (Exception e) {
        e.printStackTrace();
    }
            
        }
              
        return false;
    }

}

    在pojo下加入person类

package com.neusoft.test.spib.pojo;

import java.util.List;

public class Person
{
    
    private String id;
    private String no;
    private String name;
    private String password;
    private String age;
    List<Person> personrList;
    public List<Person> getPersonList()
    {
        return personrList;
    }
    public void setPersonList(List<Person> personrList)
    {
        this.personrList = personrList;
    }
    public Person(){}
    public Person(String id,String no,String name,String password,String age){
        this.id = id;
        this.no = no;
        this.name= name;
        this.password = password;
        this.age = age;
    }
    public String getId()
    {
        return id;
    }

    public String getNo()
    {
        return no;
    }

    public void setNo(String no)
    {
        this.no = no;
    }

    public void setId(String id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }
    public String getAge()
    {
        return age;
    }

    public void setAge(String age)
    {
        this.age = age;
    }

    public void print(){
        System.out.println(this.getId()+" "+this.getNo()+" "+this.getName()+" "+this.getPassword()+" "+this.getAge());
    }
    public static Person StaticCreatePerson(){
        return new Person("333","3333","小李","123","30");
    }
    public  Person CreatePerson(){
        return new Person("444","444","小王","123","30");
    }
}

在main下加入Test类

package com.neusoft.test.spib.main;


import java.util.List;



import com.neusoft.test.spib.dao.PersonDao;
import com.neusoft.test.spib.impl.PersonImpl;
import com.neusoft.test.spib.pojo.Person;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test
{



    /**
     * {方法功能中文描述}
     * 
     * @param args
     * @author:韩超
     */

    public static void main(String[] args)
    { 
        
//      // 构造器和属性注入
//       ApplicationContext applicationContext=new ClassPathXmlApplicationContext(new String("applicationContext-web.xml"));
//       Person person=(Person)applicationContext.getBean("person");
//       Person person2=(Person)applicationContext.getBean("person1");
//       Person person3=(Person)applicationContext.getBean("person2");
//       person.print();
//       person2.print();
//       person3.print();
//       
//       //静态工厂实例化
//      // ApplicationContext applicationContext= new ClassPathXmlApplicationContext(new String("applicationContext-web.xml"));
//       Person person1 = (Person)applicationContext.getBean("beanFactory");
//       person1.StaticCreatePerson().print();
//       //动态工厂实例化
//       // ApplicationContext applicationContext= new ClassPathXmlApplicationContext(new String("applicationContext-web.xml"));
//        Person person5 = (Person)applicationContext.getBean("person5");
//        person5.CreatePerson().print();
        
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String( "applicationContext-web.xml"));
        Person person=(Person)applicationContext.getBean("persons");
        List<Person> personlist=person.getPersonList();
        PersonDao persondao = new PersonImpl();
        persondao.insertPersons(personlist);
        System.out.println("*****************");
        for(Person p: personlist){
            System.out.println(p.getId()+" "+p.getNo()+" "+p.getName()+" "+p.getPassword()+" "+p.getAge());
        }
        
    }  
}


 

构建开源的项目环境(Spring/ibatis/maven/nexus)

项目中用到了Spring、ibatisd

Spring+Ibatis集成开发实例

首先简历数据库demo(本文选mysql) 数据库脚本: CREATE TABLE `ibatis` (   `id` varchar(20) NOT NULL,   `na...
  • x1n1010
  • x1n1010
  • 2013年11月24日 17:57
  • 415

Java Web系统初始化时从数据库中加载数据到文本(ibatis,spring)

有一些特殊情况,需要在系统初始化时加载一些配置属性到本地文本中 web.xml org.springframework.web.context.ContextLoaderListen...
  • Duke147
  • Duke147
  • 2014年12月05日 17:20
  • 1139

Spring_ibatis_jta多数据源配置

Spring+iBatis+JOTM实现JTA事务       JOTM是个开源的JTA事务管理组件,可以让程序脱离J2EE容器而获得分布式事务管理的能力。       测试过程如下:   ...

JAVA开发:分享一些SpringMvc+Ibatis+spring的框架使用心得

近期不在做.net的项目,而是使用java作为开发语言,就想着要用springmvc开发了,由于前些年也用过struts1/2+hibernate/ibatis+spring开发过项目,因此是有些底子...

[项目实战] ibatis +spring+struts2+jquery.autocomplete实现产品自动补全功能(二) 附带源码

这两天忙公司的项目,没有把项目的demo写出来。趁现在空闲,偷偷的写一下。 先上demo结构目录图: 大家看到了吧,项目层次分得很清楚。 config  是 spring和itatis的配置文件。接...

Spring实现iBATIS事务回滚~

Spring 实现iBATIS事务回滚是怎么实现的呢?让我们开始这篇文章的学习,这篇文章将会想你介绍Spring 实现iBATIS事务回滚的相关消息 AD: Spr...
  • ffmoshu
  • ffmoshu
  • 2012年03月28日 22:30
  • 1489

spring+springmvc+ibatis整合注解方式实例。

转自:http://shaohan126448.iteye.com/blog/2033563 1.需求说明 实现用户通过数据库验证登录需求,采用Myeclipse+Tomcat 6.0+Mys...

iBatis批处理实现(Spring环境)

本文源自网络摘抄、总结; 从4个层面分析这部分实现:  iBatis的基本实现 基于事务的iBatis的基本实现 基于事务的Spring+iBatis实现 基于回调方式的Spring+iBat...

spring ibatis注解方式注入

内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:spring+ibatis
举报原因:
原因补充:

(最多只允许输入30个字)