瀏覽代碼

jdbc template

zhang 2 年之前
父節點
當前提交
33471ca054

+ 16 - 0
springMvc/pom.xml

@@ -101,6 +101,22 @@
101 101
       <artifactId>commons-fileupload</artifactId>
102 102
       <version>1.3.1</version>
103 103
     </dependency>
104
+    <dependency>
105
+      <groupId>org.springframework</groupId>
106
+      <artifactId>spring-jdbc</artifactId>
107
+      <version>5.2.3.RELEASE</version>
108
+    </dependency>
109
+    <dependency>
110
+      <groupId>org.springframework</groupId>
111
+      <artifactId>spring-tx</artifactId>
112
+      <version>5.2.3.RELEASE</version>
113
+    </dependency>
114
+    <dependency>
115
+      <groupId>c3p0</groupId>
116
+      <artifactId>c3p0</artifactId>
117
+      <version>0.9.1.2</version>
118
+    </dependency>
119
+
104 120
   </dependencies>
105 121
 
106 122
 </project>

+ 18 - 0
springMvc/src/main/java/com/service/impl/UserServiceImpl.java

@@ -1,16 +1,34 @@
1 1
 package com.service.impl;
2 2
 
3 3
 import com.dao.UserDao;
4
+import com.mchange.v2.c3p0.ComboPooledDataSource;
4 5
 import com.service.UserService;
5 6
 import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.jdbc.core.JdbcTemplate;
6 8
 import org.springframework.stereotype.Service;
7 9
 
10
+import java.beans.PropertyVetoException;
11
+
8 12
 @Service
9 13
 public class UserServiceImpl implements UserService {
10 14
     @Autowired
11 15
     private UserDao userDao;
16
+    private JdbcTemplate jdbcTemplate;
12 17
     @Override
13 18
     public void saveUser() {
19
+        jdbcTemplate = new JdbcTemplate();
20
+        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
21
+        try {
22
+            comboPooledDataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
23
+            comboPooledDataSource.setJdbcUrl("jdbc:mysql://47.110.156.18:3306/sczn_eshop_test?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai");
24
+            comboPooledDataSource.setUser("sczn");
25
+            comboPooledDataSource.setPassword("sczn159357");
26
+            this.jdbcTemplate.setDataSource(comboPooledDataSource);
27
+            int row = this.jdbcTemplate.update("insert into `wb_tbl_a`(company_id,status) " +
28
+                    "values (?,?)", 100, 100);
29
+        } catch (Exception e) {
30
+            e.printStackTrace();
31
+        }
14 32
         userDao.save();
15 33
     }
16 34
 }

+ 1 - 1
springMvc/src/main/java/com/web/UserServlet.java

@@ -23,7 +23,7 @@ public class UserServlet extends HttpServlet {
23 23
 //        ApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
24 24
         WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
25 25
         AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
26
-        Object ssFilter = annotationConfigApplicationContext.getBean("ssFilter");
26
+      //  Object ssFilter = annotationConfigApplicationContext.getBean("ssFilter");
27 27
         UserService bean = webApplicationContext.getBean(UserService.class);
28 28
         bean.saveUser();
29 29
         // Hello