Selaa lähdekoodia

2024年4月8日 14点51分

starlight_0208 5 kuukautta sitten
vanhempi
commit
45c12cd1e6

+ 15 - 0
pom.xml

@@ -24,6 +24,21 @@
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>druid</artifactId>
+            <version>1.1.16</version>
+        </dependency>
+        <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+            <version>8.0.33</version>
+        </dependency>
+        <dependency>
+            <groupId>c3p0</groupId>
+            <artifactId>c3p0</artifactId>
+            <version>0.9.1.2</version>
+        </dependency>
     </dependencies>
     <dependencyManagement>
         <dependencies>

+ 5 - 24
src/main/java/org/starter/learning/AppTest.java

@@ -1,35 +1,16 @@
 package org.starter.learning;
 
 
-import org.springframework.context.ApplicationContext;
+import com.alibaba.druid.pool.DruidDataSource;
+import com.mchange.v2.c3p0.ComboPooledDataSource;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
-import org.starter.learning.dao.BookDao;
-import org.starter.learning.pojo.Book;
-import org.starter.learning.service.BookService;
-
-import java.util.List;
 
 public class AppTest {
     public static void main(String[] args) {
-        // 获取IoC容器
-        // ApplicationContext 是一个接口需要使用new创建一个对应的实现类对象
-        // ClassPathXmlApplicationContext 是一个ApplicationContext的实现,用于从Classpath中的xml获取应用配置
         ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
-        // 获取Bean
-//        BookDao bookDao = (BookDao) ctx.getBean("bookDao");
-//        List<Book> bookList = bookDao.getBooks();
-//        BookService bookService = (BookService) ctx.getBean("bookService");
-//        List<Book> bookList1 = bookService.listBook();
-//        BookService bookService1 = (BookService) ctx.getBean("bookService");
-//        bookService1.listBook();
-//        System.out.println(BookService.class);
-//        BookDao bookDao1 = (BookDao) ctx.getBean("bookDao");
-//        BookDao bookDao2 = (BookDao) ctx.getBean("bookDao");
-//        System.out.println(bookDao1);
-//        System.out.println(bookDao2);
         ctx.registerShutdownHook();
-        BookDao bookDao = (BookDao) ctx.getBean("bookDao");
-        bookDao.getBooks();
-//        ctx.close();
+//        DruidDataSource dataSource = (DruidDataSource) ctx.getBean("dataSource");
+        ComboPooledDataSource dataSource = (ComboPooledDataSource) ctx.getBean("dataSource");
+        System.out.println(dataSource);
     }
 }

+ 5 - 0
src/main/java/org/starter/learning/test/TestBase.java

@@ -0,0 +1,5 @@
+package org.starter.learning.test;
+
+public interface TestBase {
+    void output();
+}

+ 40 - 0
src/main/java/org/starter/learning/test/TestBaseImpl.java

@@ -0,0 +1,40 @@
+package org.starter.learning.test;
+
+import java.util.*;
+
+public class TestBaseImpl implements TestBase {
+    private int[] array;
+    private List<String> list;
+    private Set<String> set;
+    private Map<String, String> map;
+    private Properties properties;
+
+    public void setArray(int[] array) {
+        this.array = array;
+    }
+
+    public void setList(List<String> list) {
+        this.list = list;
+    }
+
+    public void setSet(Set<String> set) {
+        this.set = set;
+    }
+
+    public void setMap(Map<String, String> map) {
+        this.map = map;
+    }
+
+    public void setProperties(Properties properties) {
+        this.properties = properties;
+    }
+
+    @Override
+    public void output() {
+        System.out.println("遍历数组:" + Arrays.toString(array));
+        System.out.println("遍历list:" + list);
+        System.out.println("遍历set:" + set);
+        System.out.println("遍历map:" + map);
+        System.out.println("遍历Properties:" + properties);
+    }
+}

+ 51 - 1
src/main/resources/applicationContext.xml

@@ -3,6 +3,56 @@
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
     <bean id="bookDao" class="org.starter.learning.dao.impl.BookDaoImpl"/>
-    <bean id="bookService" class="org.starter.learning.service.impl.BookServiceImpl" autowire="byType" />
+    <bean id="bookService" class="org.starter.learning.service.impl.BookServiceImpl" />
     <bean id="bookDao2" class="org.starter.learning.dao.impl.BookDaoImpl" />
+    <bean id="testBase" class="org.starter.learning.test.TestBaseImpl" >
+        <property name="array">
+            <array>
+                <value>100</value>
+                <value>200</value>
+                <value>300</value>
+            </array>
+        </property>
+        <property name="list">
+            <list>
+                <value>string1</value>
+                <value>string2</value>
+                <value>string3</value>
+            </list>
+        </property>
+        <property name="set">
+            <set>
+                <value>value1</value>
+                <value>value2</value>
+                <value>value2</value>
+                <value>value3</value>
+            </set>
+        </property>
+        <property name="map">
+            <map>
+                <entry key="country" value="China" />
+                <entry key="province" value="Shanxi" />
+                <entry key="city" value="Datong" />
+            </map>
+        </property>
+        <property name="properties">
+            <props>
+                <prop key="country" >China</prop>
+                <prop key="province">ShanXi</prop>
+                <prop key="city">Datong</prop>
+            </props>
+        </property>
+    </bean>
+    <bean id="dataSourceDeprecated" class="com.alibaba.druid.pool.DruidDataSource">
+        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
+        <property name="url" value="jdbc:mysql://localhost:3306/testdb" />
+        <property name="username" value="root" />
+        <property name="password" value="123456" />
+    </bean>
+    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
+        <property name="driverClass" value="com.mysql.cj.jdbc.Driver" />
+        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/testdb" />
+        <property name="user" value="root" />
+        <property name="password" value="123456" />
+    </bean>
 </beans>

+ 4 - 0
src/main/resources/jdbc.properties

@@ -0,0 +1,4 @@
+jdbc.driver = com.mysql.cj.jdbc.Driver
+jdbc.url = jdbc:mysql://localhost:3306/testdb
+jdbc.username = root
+jdbc.password = 123456