前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >spring开发_Spring_DataSource

spring开发_Spring_DataSource

作者头像
Hongten
发布2018-09-13 16:51:37
4710
发布2018-09-13 16:51:37
举报
文章被收录于专栏:Hongten

项目结构:

http://www.cnblogs.com/hongten/gallery/image/112745.html

/spring_2100_spring_dataSource/src/com/b510/db/SpringDataSource.java

代码语言:javascript
复制
 1 package com.b510.db;
 2 
 3 import java.sql.Connection;
 4 import java.sql.ResultSet;
 5 import java.sql.SQLException;
 6 import java.sql.Statement;
 7 import java.util.ArrayList;
 8 import java.util.List;
 9 
10 import javax.annotation.Resource;
11 import javax.sql.DataSource;
12 
13 import com.b510.domain.Person;
14 
15 public class SpringDataSource {
16 
17     private DataSource dataSource;
18 
19     public DataSource getDataSource() {
20         return dataSource;
21     }
22 
23     @Resource
24     public void setDataSource(DataSource dataSource) {
25         this.dataSource = dataSource;
26     }
27 
28     /**
29      * 保存一条记录
30      * 
31      * @param p
32      *            Person的一个实例对象
33 */
34     public void save(Person p) {
35         String sql = "insert into person(name,age,sex)values(" + "'"
36                 + p.getName() + "'" + "," + "'" + p.getAge() + "'" + "," + "'"
37                 + p.getSex() + "'" + ")";
38         Connection conn = null;
39         try {
40             conn = dataSource.getConnection();
41             Statement statement = conn.prepareStatement(sql);
42             statement.executeUpdate(sql);
43         } catch (SQLException e) {
44             e.printStackTrace();
45         } finally {
46             try {
47                 conn.close();
48             } catch (SQLException e) {
49                 e.printStackTrace();
50             }
51         }
52     }
53 
54     /**
55      * 获取所有记录
56      * 
57      * @return 返回一个list对象
58 */
59     public List<Person> getPerson() {
60         String sql = "select * from person";
61         Connection conn = null;
62         List<Person> list = new ArrayList<Person>();
63         try {
64             conn = dataSource.getConnection();
65             Statement statement = conn.prepareStatement(sql);
66             ResultSet rs = statement.executeQuery(sql);
67             Person person = null;
68             while (rs.next()) {
69                 person = new Person();
70                 person.setId(rs.getInt("id"));
71                 person.setName(rs.getString("name"));
72                 person.setAge(rs.getInt("age"));
73                 person.setSex(rs.getString("sex"));
74                 list.add(person);
75             }
76         } catch (SQLException e) {
77             e.printStackTrace();
78         } finally {
79             try {
80                 conn.close();
81             } catch (SQLException e) {
82                 e.printStackTrace();
83             }
84         }
85         return list;
86     }
87 }

/spring_2100_spring_dataSource/src/com/b510/domain/Person.java

代码语言:javascript
复制
 1 package com.b510.domain;
 2 
 3 import org.springframework.stereotype.Component;
 4 
 5 /**
 6  * Person实体类
 7  * 
 8  * @author Hongten
 9  * 
10  */
11 @Component
12 public class Person implements java.io.Serializable {
13 
14     // Fields
15 
16     /**
17      * 版本号
18 */
19     private static final long serialVersionUID = -47270870639923184L;
20     /**
21      * id号
22 */
23     private Integer id;
24     /**
25      * 姓名
26 */
27     private String name;
28     /**
29      * 年龄
30 */
31     private Integer age;
32     /**
33      * 性别
34 */
35     private String sex;
36 
37     // Constructors
38 
39     /** default constructor */
40     public Person() {
41     }
42 
43     /** minimal constructor */
44     public Person(String name) {
45         this.name = name;
46     }
47 
48     /** full constructor */
49     public Person(String name, Integer age, String sex) {
50         this.name = name;
51         this.age = age;
52         this.sex = sex;
53     }
54 
55     // Property accessors
56 
57     public Integer getId() {
58         return this.id;
59     }
60 
61     public void setId(Integer id) {
62         this.id = id;
63     }
64 
65     public String getName() {
66         return this.name;
67     }
68 
69     public void setName(String name) {
70         this.name = name;
71     }
72 
73     public Integer getAge() {
74         return this.age;
75     }
76 
77     public void setAge(Integer age) {
78         this.age = age;
79     }
80 
81     public String getSex() {
82         return this.sex;
83     }
84 
85     public void setSex(String sex) {
86         this.sex = sex;
87     }
88 
89 }

/spring_2100_spring_dataSource/src/com/b510/test/SpringTest.java

代码语言:javascript
复制
 1 package com.b510.test;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.context.ApplicationContext;
 6 import org.springframework.context.support.ClassPathXmlApplicationContext;
 7 
 8 import com.b510.db.SpringDataSource;
 9 import com.b510.domain.Person;
10 
11 public class SpringTest {
12 
13     /**
14      * @param args
15 */
16     public static void main(String[] args) {
17         ApplicationContext act = new ClassPathXmlApplicationContext("bean.xml");
18         SpringDataSource sds = (SpringDataSource) act.getBean("sds");
19         Person person = new Person("hw", 20, "M");
20         sds.save(person);
21 
22         List<Person> list = sds.getPerson();
23         for (Person p : list) {
24             System.out.println("id=" + p.getId() + ",name=" + p.getName()
25                     + ",age=" + p.getAge() + ",sex=" + p.getSex());
26         }
27     }
28 
29 }

/spring_2100_spring_dataSource/src/bean.xml

代码语言:javascript
复制
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans
 6            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 7            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
 8            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
 9            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
10     <context:annotation-config />
11 
12     <!-- 读取jdbc.properties配置文件 -->
13     <context:property-placeholder location="classpath:jdbc.properties" />
14 
15     <!-- 配置数据源 -->
16     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
17         destroy-method="close">
18         <property name="driverClassName" value="${driverClassName}" />
19         <property name="url" value="${url}" />
20         <property name="username" value="${username}" />
21         <property name="password" value="${password}" />
22         <!-- 连接池启动时的初始值 -->
23         <property name="initialSize" value="${initialSize}" />
24         <!-- 连接池的最大值 -->
25         <property name="maxActive" value="${maxActive}" />
26         <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
27         <property name="maxIdle" value="${maxIdle}" />
28         <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
29         <property name="minIdle" value="${minIdle}" />
30     </bean>
31 
32     <bean id="sds" class="com.b510.db.SpringDataSource"></bean>
33 </beans>

/spring_2100_spring_dataSource/src/jdbc.properties

代码语言:javascript
复制
1 driverClassName=org.gjt.mm.mysql.Driver
2 url=jdbc\:mysql\://localhost\:3307/spring?useUnicode\=true&characterEncoding\=UTF-8
3 username=root
4 password=root
5 initialSize=1
6 maxActive=300
7 maxIdle=2
8 minIdle=1

 运行结果:

代码语言:javascript
复制
 1 2012-3-14 17:23:22 org.springframework.context.support.AbstractApplicationContext prepareRefresh
 2 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1a05308: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1a05308]; startup date [Wed Mar 14 17:23:22 CST 2012]; root of context hierarchy
 3 2012-3-14 17:23:22 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
 4 信息: Loading XML bean definitions from class path resource [bean.xml]
 5 2012-3-14 17:23:22 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
 6 信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1a05308]: org.springframework.beans.factory.support.DefaultListableBeanFactory@74c3aa
 7 2012-3-14 17:23:22 org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
 8 信息: Loading properties file from class path resource [jdbc.properties]
 9 2012-3-14 17:23:22 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
10 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@74c3aa: defining beans [org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,dataSource,sds]; root of factory hierarchy
11 id=2,name=hanyuan,age=21,sex=男
12 id=3,name=hongten,age=21,sex=男
13 id=4,name=hongten,age=21,sex=男
14 id=5,name=hello,age=12,sex=M
15 id=6,name=hongten,age=12,sex=M
16 id=7,name=hongten2,age=12,sex=M
17 id=8,name=hw,age=20,sex=M
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2012-03-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档