spring_150805_datasource
实体类:
package com.spring.model; public class DogPet { private int id;
private String name;
private int age;
private String kind;
private String sex;
private String health;
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getHealth() {
return health;
}
public void setHealth(String health) {
this.health = health;
} public String toString()
{
return id+"--"+name+"--"+kind+"--"+age+"--"+health;
}
}
接口Service:
package com.spring.service; public interface DogPetService {
public void queryAllDogPets();
public void saveDogPet();
}
实现类ServiceImpl:
package com.spring.service.impl; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import com.spring.service.DogPetService;
import com.spring.dao.DogPetDAO;
import com.spring.model.DogPet; @Controller(value="DogPetService")
public class DogPetServiceImpl implements DogPetService{
private DogPetDAO dogPetDAO; public DogPetDAO getDogPetDAO() {
return dogPetDAO;
} @Resource(name="dogPetDAO2")
public void setDogPetDAO(DogPetDAO dogPetDAO) {
this.dogPetDAO = dogPetDAO;
} @Override
public void queryAllDogPets() {
List<DogPet> list = dogPetDAO.queryAllDogPets();
if(list != null)
{
for(DogPet d:list)
{
System.out.println(d.toString());
}
}
} public void saveDogPet()
{
DogPet d1 = new DogPet();
d1.setId(3333333);
d1.setName("dog1");
d1.setAge(4);
d1.setKind("buladuo");
d1.setSex("B");
d1.setHealth("good");
dogPetDAO.saveDogPet(d1);
} }
Service调用的DAO:
package com.spring.dao; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import javax.annotation.Resource;
import javax.sql.DataSource; import org.springframework.stereotype.Service; import com.spring.model.DogPet; @Service(value="dogPetDAO2")
public class DogPetDAO { private DataSource dataSource; public List<DogPet> queryAllDogPets()
{
List<DogPet> list = new ArrayList<DogPet>(); DogPet d1 = new DogPet();
d1.setId(1111);
d1.setName("dog1");
d1.setAge(4);
d1.setKind("buladuo");
d1.setSex("B");
d1.setHealth("good");
DogPet d2 = new DogPet();
d2.setId(2222);
d2.setName("dog2");
d2.setAge(3);
d2.setKind("buladuo");
d2.setSex("G");
d2.setHealth("good"); list.add(d1);
list.add(d2); return list;
} public void saveDogPet(DogPet dog)
{
Connection connection = null;
try
{
connection = dataSource.getConnection() ;
PreparedStatement pstmt = connection.prepareStatement("insert into t_dog values(?,?,?,?,?,?)");
pstmt.setInt(1, dog.getId());
pstmt.setString(2, dog.getName());
pstmt.setInt(3, dog.getAge());
pstmt.setString(4, dog.getKind());
pstmt.setString(5, dog.getSex());
pstmt.setString(6, dog.getHealth());
pstmt.execute();
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
} public DataSource getDataSource() {
return dataSource;
} @Resource
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
} }
配置文件beans.xml:
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config/> <context:component-scan base-package="com.spring"></context:component-scan> </beans>
配置文件dao.xml:
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${user}" />
<property name="password" value="${password}" />
</bean> </beans>
jdbc.properties:
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@10.32.145.254:1521:cspperf
user=icdwf
password=icdwf
test类:
package com.spring.test; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.spring.service.DogPetService; public class ServiceTest { @Test
public void saveDogPet()
{
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml","dao.xml"});
DogPetService dogPetService = (DogPetService)ctx.getBean("DogPetService");
dogPetService.saveDogPet();
} }
spring_150805_datasource的更多相关文章
随机推荐
- Jni中C++和Java的参数传递
Jni中C++和Java的参数传递 如何使用JNI的一些基本方法和过程在网上多如牛毛,如果你对Jni不甚了解,不知道Jni是做什么的,如何建立一个基本的jni程序,或许可以参考下面下面这些文章:利用V ...
- ios各种手势,很有意思
转自http://blog.csdn.net/likendsl/article/details/7554150 这哥们很厉害的 一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由U ...
- shell 函数
1 shell函数的定义及其调用 shell函数有两种格式: function name { commands } name() { commands } 其中,name为函数名,commands为函 ...
- C++ vector介绍
<span style="font-family: Arial; ">在此总结一下模板vector的使用介绍</span> 标准库vector类型使用需要的 ...
- JAVA数据结构-----枚举
枚举(Enumeration)接口虽然它本身不属于数据结构,但它在其他数据结构的范畴里应用很广. 枚举(The Enumeration)接口定义了一种从数据结构中取回连续元素的方式. 例如,枚举定义了 ...
- 在数据表中添加一个字段的SQL语句怎么写
如果要在数据表中添加一个字段,应该如何表示呢?下面就为您介绍表添加字段的SQL语句的写法,希望可以让您对SQL语句有更深的认识. 通用式: alter table [表名] add [字段名] 字 ...
- c++ _beginthread
c++多线程编程 #include <windows.h> #include <process.h> /* _beginthread, _endthread */ #inclu ...
- Java存储密码用字符数组
字符数组和字符串都可以用于存储文本数据,但是在选择具体哪一种时,如果你没有针对具体的情况是很难回答这个问题的.但是任何与字符串相关的问题一定有线索可以在字符串的属性里面找到,比如不可变性.他就用这种方 ...
- 用R语言对NIPS会议文档进行聚类分析
一.用R语言建立文档矩阵 (这里我选用的是R x64 3.2.2) (这里我取的是04年NIPS共计207篇文档做分析,其中文档内容已将开头的作者名和最后的参考文献进行过滤处理) ##1.Data I ...
- Dedication(转)
To all developers who strive for continuous self-improvement... Who are not satisfied with good enou ...