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());
        }
        
    }  
}


 

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

JAVA WEB中spring整合Ibatis总结

今儿这个问题让我着实纠结了一把,spring和ibatis整合的配置文件都配好了,然后运行报错,错误很简单就是返回值为空,让我百思不得其解。最后终于明白了,原来是我测试的实现类是java文件,而非ja...

SpringMVC+Spring+Ibatis开发环境搭建

今天尝试搭建了一下SpringMVC+Spring+Ibatis的开发环境,各种百度,终于是功夫不负有心人,搭建过程比较简单(吐槽一下:百度上各种资料一样的太多了,各种误导,终于是搭出来了),先看看我...
  • lohocc
  • lohocc
  • 2015-02-26 16:39
  • 1250

xfire+spring+ibatis(详细)

spring+xfire+ibatis搭建的项目.接触xfire没多久,但是查阅了很多资料. 下面贴下弄好的项目资料.以备以后可查.主要配置文件 ————————-applicationCont...

Ibatis,Spring整合(注解方式注入)

applicationContext.xmlxml version="1.0" encoding="UTF-8"?> beans xmlns="http://www.springframework.o...

spring 3.0 + ibatis 2 整合

一、需要用到的包 二、 Suppliers.java                    ...
  • w__yi
  • w__yi
  • 2012-04-24 17:19
  • 2199

flex3+struts 1.3+spring+ibatis 2.x整合代码实例

flex3+struts 1.3+spring+ibatis 2.x整合代码实例,代码下载地址:http://www.zuidaima.com/share/1778657261997056.htm

spring整合ibatis之使用配置文件和用annotation的简单说明

刚刚自学了ibatis,还不是很精通,自己写了一些小的例子,用spring整合ibatis用

spring+ibatis实现读写分离(分享我的开源项目)

先吐槽下博客园,每天都推荐水帖不说,正经的分享技术的博客还他妈的不让上首页,我在那里投入了那么多汗水,什么垃圾东西。spring+ibatis实现读写分离 特点 无缝结合spring+ibatis,...

ibatis+struts+spring配置和登录实现

最近换了家公司,用的ibatis,也就是SSI.,感觉跟hibernate比起来麻烦了不止一点两点,所有sql语句都要自己手敲,.但是也有一些优点,比如ibatis查询数据可以指定一些返回的结果,响应...

spring mvc+ibatis+mysql的组合框架入门实例demo源码下载

spring mvc+ibatis+mysql的组合框架入门实例demo源码下载 源代码下载地址:http://www.zuidaima.com/share/1550463678958592.htm
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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