Chinaunix首页 | 论坛 | 博客
  • 博客访问: 342503
  • 博文数量: 329
  • 博客积分: 2633
  • 博客等级: 少校
  • 技术积分: 3633
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-22 15:43
文章分类

全部博文(329)

文章存档

2013年(244)

2012年(46)

2011年(39)

我的朋友

分类: 系统运维

2012-02-26 19:52:04

Spring应用

本章涉及两个例子,用到的包有:

commons-dbcp-1.2.1.jarcommons-logging-1.0.4.jarcommons-logging-1.1.1.jarcommons-logging.jarcommons-pool-1.4.jardom4j-1.6.1.jarmysql-connector-java-3.1.14-bin.jarspring-aop.jarspring-beans.jarspring-context.jarspring-core.jarspring-dao.jarspring-hibernate.jarspring-jdbc.jarspring-mock.jarspring-orm.jarspring-remoting.jarspring-support.jarspring-web.jarspring-webmvc.jarspring.jar

 

 

1.第一个例子:

1).HelloWorld.java

package hello.spring;

public class HelloWorld {

    private String hello;

    public String getHello() {

       return hello;

    }

    public void setHello(String hello) {

       this.hello = hello;

    }

    public void show(){

       System.out.println("---message---"+getHello());

    }

}

2).TestHelloWorld.java

package hello.spring;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

public class TestHelloWorld {

    public static void main(String[] args) {

       ApplicationContext ctx = new FileSystemXmlApplicationContext("src/myspring1.xml");

       HelloWorld hw = (HelloWorld) ctx.getBean("myBean");

       hw.show();

    }

}

3).myspring1.xml

xml version="1.0" encoding="UTF-8"?>

DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "">

<beans>

    <bean id="myBean" class="hello.spring.HelloWorld">

       <property name="hello">

           <value>Hello Spring!value>

       property>

    bean>

beans>

 

2.第二个例子:

1).SpringtoResultSetInfo.java

//和数据库映射的类

package test;

public class SpringtoResultSetInfo {

    private int id;

    private String templatename;

    public int getId() {

       return id;

    }

    public void setId(int id) {

       this.id = id;

    }

    public String getTemplatename() {

       return templatename;

    }

    public void setTemplatename(String templatename) {

       this.templatename = templatename;

    }

}

2). SqlStatement.java

package test;

//新建一个抽象类

public abstract class SqlStatement {

    final public static String createSql = "create table mytable(id int,cname varchar(50))";

    final public static String insertData1 = "insert into mytable values(1,'name1')";

    final public static String insertData2 = "insert into mytable values(2,'test2')";

    final public static String insertData3 = "insert into mytable values(3,'test3')";

    final public static String selectSql = "select * from mytable";

}

3). Test.java

//测试类

package test;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import org.springframework.beans.factory.BeanFactory;

import org.springframework.beans.factory.xml.XmlBeanFactory;

import org.springframework.core.io.ClassPathResource;

import org.springframework.core.io.Resource;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.jdbc.core.RowCallbackHandler;

import org.springframework.jdbc.core.RowCountCallbackHandler;

public class Test {

    public static void main(String[] args) {

       Resource resource = new ClassPathResource("myspring.xml");

       BeanFactory factory = new XmlBeanFactory(resource);

       JdbcTemplate jt = (JdbcTemplate) factory.getBean("dbtest");

       // 创建数据库

       jt.execute(SqlStatement.createSql);

       // 插入数据

       jt.execute(SqlStatement.insertData1);

       jt.execute(SqlStatement.insertData2);

       jt.execute(SqlStatement.insertData3);

       // 获得数据表信息

       RowCountCallbackHandler rcch = new RowCountCallbackHandler();

       jt.query(SqlStatement.selectSql, rcch);

       System.out.println("结果集中的列数量:" + rcch.getColumnCount());

       System.out.println("结果集中的行数量:" + rcch.getRowCount());

       System.out.println("结果集中的结果:");

       // 取得列名

       String[] str = rcch.getColumnNames();

       for (int i = 0; i < str.length; i++) {

           System.out.print(str[i] + " ");

       }

       // 取得数据表中数据

       final ArrayList list = new ArrayList();

       jt.query(SqlStatement.selectSql, new RowCallbackHandler() {

           public void processRow(ResultSet rs) throws SQLException {

              // TODO Auto-generated method stub

              SpringtoResultSetInfo sri = new SpringtoResultSetInfo();

              sri.setId(rs.getInt(1));

              sri.setTemplatename(rs.getString(2));

              list.add(sri);

           }

       });

       for (int i = 0; i < list.size(); i++) {

           SpringtoResultSetInfo sri = (SpringtoResultSetInfo) list.get(i);

           System.out.print("\n" + " " + sri.getId());

           System.out.print("  " + sri.getTemplatename());

       }

       System.out.println("\n完成");

    }

}

4).myspring.xml

xml version="1.0" encoding="UTF-8"?>

DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "">

<beans>

    <bean id="dbtest" class="org.springframework.jdbc.core.JdbcTemplate">

       <property name="dataSource">

           <ref local="dataSource" />

       property>

    bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"

       destroy-method="close">

       <property name="driverClassName">

           <value>org.gjt.mm.mysql.Drivervalue>

       property>

       <property name="url">

           <value>jdbc:mysql://127.0.0.1/mytestvalue>

       property>

       <property name="username">

           <value>rootvalue>

       property>

       <property name="password">

           <value>rootvalue>

       property>

    bean>

beans>

 

阅读(666) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~