Spring——JDBC

前言:最近回顾了一遍Spring全家桶,有几个在过程中遇到的问题记录一下。

  • JDBC:Java数据库连接(Java Database Connectivity)
  • C3P0:一个开源的JDBC连接池
  • DataSource:JDBC数据源接口
  • JDBCTemplate:为不同类型的JDBC操作提供模板方法

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>test</groupId>
  <artifactId>mavenDemo</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>mavenDemo Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <dependencies>
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.6</version>
</dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.8.RELEASE</version>
 </dependency>
   <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>
 <!-- c3p0 -->
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>mchange-commons-java</artifactId>
    <version>0.2.3.1</version>
</dependency>
<dependency>
    <groupId>c3p0</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.1.2</version>
</dependency>
<!-- c3p0 end -->
<!-- aspect -->
<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.8.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.8.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.3.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>4.3.8.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.10</version>
    </dependency>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.10</version>
    </dependency>
   <!-- aspect end -->
</dependencies>
  <build>
    <finalName>mavenDemo</finalName>
  </build>
</project>

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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

<!-- 导入资源文件 ,classpath.jdbc.properties 为类路径下的文件名为 jdbc.properties-->
    <context:property-placeholder location="classpath:db.properties"/>
<!-- c3p0 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
            <property name="user" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
            <property name="driverClass" value="${jdbc.driverClassName}"/>
            <property name="jdbcUrl" value="${jdbc.url}"/>
        </bean>
        
<!-- jdbcTemplate -->        
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
        </bean>

</beans>

db.properties

jdbc.username=root
jdbc.password=admin
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?serverTimezone=GMT&useUnicode=true&characterEncoding=utf-8

Main.java

package com.test09.jdbc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

public class Main {
    public static void main(String[] args) {
            ApplicationContext    ctx = new ClassPathXmlApplicationContext("beans-springjdbc.xml");
            JdbcTemplate    jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");
            String sql = "insert user(username,password) values('Tom','22')";
            int i = jdbcTemplate.update(sql);
            System.out.println(i);
    }
}

第一个坑:因为把数据库连接的参数都拿出来放到db.properties文件中,所以还需要导入资源文件,之前没导,一直报driverclass 找不到。在beansxml中加入 <context:property-placeholder location="classpath:db.properties"/>就好了

第二个坑:因为我在学的时候都是用一个jar包让pom里导一个jar包,所以导致我的jar包版本不一,潜在问题比较多,这不刚好发生一个:The server time zone value '?й???????' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.,导致这个问题的原因是我的mysql-connector-java的版本是6.0.5,这是由于数据库和系统时区差异所造成的,在jdbc连接的url后面加上serverTimezone=GMT即可解决问题。

所以啊,在用maven项目的时候,就得用它给我们提供的版本,这样的话产生的未知错误就会少很多。

第三个坑:在用注解的方式导入bean时,需要先在bean上加入注解

component表示受Spring管理的组件

Repository表示持久层组件

Service表示业务层组件

Controller表示表现层组件

在service类中加入@service注解,现在在Bean中就可以使用,它的id是service类名首字母小写。当然也可以自定义id

加完注解后还不行,还需要通过bean.xml扫包,配置如下:

<context:component-scan base-package="service所在的包名" />

<context> 去扫描组件,base-package属性指定一个需要扫描的基类包,Spring容器将会扫描这个基类包及其子包中的所有类。需要扫描多个包时,可以使用逗号分开。

第四个坑:代码别敲错,别敲错,别敲错,敲错痛苦的是自己个儿

猜你喜欢

转载自blog.csdn.net/sinat_39634657/article/details/81203875