Browse Source

jdbc template操作测试

zhang 2 years ago
parent
commit
0475640871

+ 4 - 1
springMvc/pom.xml

@@ -47,7 +47,10 @@
47
       </plugin>
47
       </plugin>
48
     </plugins>
48
     </plugins>
49
   </build>
49
   </build>
50
-
50
+  <properties>
51
+    <maven.compiler.source>8</maven.compiler.source>
52
+    <maven.compiler.target>8</maven.compiler.target>
53
+  </properties>
51
   <dependencies>
54
   <dependencies>
52
     <!--dependency>
55
     <!--dependency>
53
       <groupId>org.example</groupId>
56
       <groupId>org.example</groupId>

+ 59 - 0
springMvc/src/main/java/com/controller/JdbcTemplateController.java

@@ -0,0 +1,59 @@
1
+package com.controller;
2
+
3
+import com.pojo.entity.AEntity;
4
+import org.springframework.beans.factory.annotation.Autowired;
5
+import org.springframework.jdbc.core.BeanPropertyRowMapper;
6
+import org.springframework.jdbc.core.JdbcTemplate;
7
+import org.springframework.stereotype.Controller;
8
+import org.springframework.web.bind.annotation.RequestMapping;
9
+import org.springframework.web.bind.annotation.ResponseBody;
10
+
11
+import java.util.HashMap;
12
+import java.util.List;
13
+import java.util.Map;
14
+
15
+@Controller
16
+@RequestMapping("jdbc")
17
+public class JdbcTemplateController {
18
+    @Autowired
19
+    JdbcTemplate jdbcTemplate;
20
+    @RequestMapping("jdbc1")
21
+    @ResponseBody
22
+    public String jdbc1() {
23
+        return String.valueOf(jdbcTemplate.update("insert into `wb_tbl_a`(company_id,status) values (?,?)", 100, 100));
24
+    }
25
+    /**
26
+     * jdbc查询所有
27
+     */
28
+    @RequestMapping("queryAll")
29
+    @ResponseBody
30
+    public Map jdbc2() {
31
+        HashMap<String, String> stringStringHashMap = new HashMap<>();
32
+        stringStringHashMap.put("111","zxy");
33
+        List<AEntity> query = jdbcTemplate.query("select * from wb_tbl_a",
34
+                new BeanPropertyRowMapper<AEntity>(AEntity.class));
35
+        return stringStringHashMap;
36
+    }
37
+    /**
38
+     * 查询单个
39
+     *
40
+     */
41
+    @RequestMapping("queryOne")
42
+    @ResponseBody
43
+    public AEntity queryOne() {
44
+       return jdbcTemplate.queryForObject("select * from wb_tbl_a where id =?",
45
+                new BeanPropertyRowMapper<AEntity>(AEntity.class),
46
+                "10"
47
+        );
48
+    }
49
+    /**
50
+     * 聚合查询 查询总数
51
+     */
52
+    @RequestMapping("queryCount")
53
+    @ResponseBody
54
+    public Long queryCount() {
55
+        return jdbcTemplate.queryForObject("select count(*) from wb_tbl_a ",
56
+                Long.class
57
+        );
58
+    }
59
+}

+ 14 - 0
springMvc/src/main/java/com/pojo/entity/AEntity.java

@@ -0,0 +1,14 @@
1
+package com.pojo.entity;
2
+
3
+import lombok.Data;
4
+
5
+import java.util.Date;
6
+
7
+@Data
8
+public class AEntity {
9
+    private Integer id;
10
+    private Integer companyId;
11
+    private Integer status;
12
+    private Date utime;
13
+    private Date ctime;
14
+}

+ 13 - 14
springMvc/src/main/java/com/service/impl/UserServiceImpl.java

@@ -13,22 +13,21 @@ import java.beans.PropertyVetoException;
13
 public class UserServiceImpl implements UserService {
13
 public class UserServiceImpl implements UserService {
14
     @Autowired
14
     @Autowired
15
     private UserDao userDao;
15
     private UserDao userDao;
16
-    private JdbcTemplate jdbcTemplate;
17
     @Override
16
     @Override
18
     public void saveUser() {
17
     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
-        }
18
+//        jdbcTemplate = new JdbcTemplate();
19
+//        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
20
+//        try {
21
+//            comboPooledDataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
22
+//            comboPooledDataSource.setJdbcUrl("jdbc:mysql://47.110.156.18:3306/sczn_eshop_test?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai");
23
+//            comboPooledDataSource.setUser("sczn");
24
+//            comboPooledDataSource.setPassword("sczn159357");
25
+//            this.jdbcTemplate.setDataSource(comboPooledDataSource);
26
+//            int row = this.jdbcTemplate.update("insert into `wb_tbl_a`(company_id,status) " +
27
+//                    "values (?,?)", 100, 100);
28
+//        } catch (Exception e) {
29
+//            e.printStackTrace();
30
+//        }
32
         userDao.save();
31
         userDao.save();
33
     }
32
     }
34
 }
33
 }

+ 11 - 8
springMvc/src/main/resources/context.xml

@@ -10,14 +10,17 @@
10
     <!--    <bean id="userService1" class="com.service.impl.UserServiceImpl">-->
10
     <!--    <bean id="userService1" class="com.service.impl.UserServiceImpl">-->
11
     <!--&lt;!&ndash;        <constructor-arg name="userDao" ref="UserDao"></constructor-arg>&ndash;&gt;-->
11
     <!--&lt;!&ndash;        <constructor-arg name="userDao" ref="UserDao"></constructor-arg>&ndash;&gt;-->
12
     <!--    </bean>-->
12
     <!--    </bean>-->
13
-    <!--    加载外部 properties文件-->
14
-    <!--    <context:property-placeholder location="jdbc.properties"></context:property-placeholder>-->
15
-    <!--    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">-->
16
-    <!--        <property name="driverClassName" value="${jdbc.driver}"></property>-->
17
-    <!--        <property name="url" value="${jdbc.url}"></property>-->
18
-    <!--        <property name="username" value="${jdbc.username}"></property>-->
19
-    <!--        <property name="password" value="${jdbc.password}"></property>-->
20
-    <!--    </bean>-->
13
+<!--        加载外部 properties文件-->
14
+        <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
15
+        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
16
+            <property name="driverClassName" value="${jdbc.driver}"></property>
17
+            <property name="url" value="${jdbc.url}"></property>
18
+            <property name="username" value="${jdbc.username}"></property>
19
+            <property name="password" value="${jdbc.password}"></property>
20
+        </bean>
21
+        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
22
+            <property name="dataSource" ref="dataSource"></property>
23
+        </bean>
21
     <!--    组件扫描-->
24
     <!--    组件扫描-->
22
     <context:component-scan base-package="com"></context:component-scan>
25
     <context:component-scan base-package="com"></context:component-scan>
23
     <context:component-scan base-package="testCom"></context:component-scan>
26
     <context:component-scan base-package="testCom"></context:component-scan>

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

@@ -0,0 +1,4 @@
1
+jdbc.driver=com.mysql.cj.jdbc.Driver
2
+jdbc.url=jdbc:mysql://47.110.156.18:3306/sczn_eshop_test?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
3
+jdbc.username=sczn
4
+jdbc.password=sczn159357

+ 1 - 1
springMvc/src/main/webapp/WEB-INF/applicationContext.xml

@@ -11,7 +11,7 @@
11
   <!--&lt;!&ndash;        <constructor-arg name="userDao" ref="UserDao"></constructor-arg>&ndash;&gt;-->
11
   <!--&lt;!&ndash;        <constructor-arg name="userDao" ref="UserDao"></constructor-arg>&ndash;&gt;-->
12
   <!--    </bean>-->
12
   <!--    </bean>-->
13
   <!--    加载外部 properties文件-->
13
   <!--    加载外部 properties文件-->
14
-  <!--    <context:property-placeholder location="jdbc.properties"></context:property-placeholder>-->
14
+<!--      <context:property-placeholder location="jdbc.properties"></context:property-placeholder>-->
15
   <!--    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">-->
15
   <!--    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">-->
16
   <!--        <property name="driverClassName" value="${jdbc.driver}"></property>-->
16
   <!--        <property name="driverClassName" value="${jdbc.driver}"></property>-->
17
   <!--        <property name="url" value="${jdbc.url}"></property>-->
17
   <!--        <property name="url" value="${jdbc.url}"></property>-->