由于JDBC驱动程序报告的JDBC版本[3]小于4

时间:2019-09-17 07:37:43

标签: java spring hibernate jdbc jdbctemplate

我正在尝试通过使用休眠Tamplet在Mysql数据库表中进行插入操作,详细信息如下:

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


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName"  value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/db"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>

    <bean id="mysessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>

        <property name="mappingResources">
        <list>
        <value>employee.hbm.xml</value>
        </list>
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>

            </props>
        </property>
    </bean>

    <!-- <bean id="template" class="org.springframework.orm.hibernate4.HibernateTemplate">
    <property name="sessionFactory" ref="mysessionFactory"></property>
    </bean> -->

    <bean id="template" class="org.springframework.orm.hibernate4.HibernateTemplate">  
        <property name="sessionFactory" ref="mysessionFactory"></property>
        <property name="checkWriteOperations" value="false"></property>
        </bean>

    <bean id="d" class="com.java.com.EmployeeDao">
    <property name="template" ref="template"></property>
    </bean>


    </beans>

employee.hbm.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

          <hibernate-mapping>
          <class name="com.java.com.Employee" table="employeehb">
          <id name="id">
          <generator class="assigned"></generator>
          </id>

          <property name="name"></property>
          <property name="salary"></property>
          </class>

          </hibernate-mapping>

EmployeeDao类

package com.java.com;
import org.springframework.orm.hibernate4.HibernateTemplate;

public class EmployeeDao {
HibernateTemplate template;
public void setTemplate(HibernateTemplate template) {
    this.template = template;
}

public void saveEmployee(Employee e){
    template.save(e);
}

public void updateEmployee(Employee e){
    template.update(e);
}

public void deleteEmployee(Employee e){
    template.delete(e);
}
}

员工阶层

package com.java.com;

public class Employee {
private int id;
private String name;
private float salary;

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public float getSalary() {
    return salary;
}
public void setSalary(float salary) {
    this.salary = salary;
}


}

测试类

package com.java.com;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.java.com.EmployeeDao;

public class InsertTest {
public static void main(String[] args) {

    Resource r=new ClassPathResource("applicationContext.xml");
    BeanFactory factory=new XmlBeanFactory(r);

    System.out.println("BeanFactory created ..");
    EmployeeDao dao=(EmployeeDao)factory.getBean("d");

    System.out.println("Employee Dao:"+dao);

    Employee e=new Employee();
    e.setId(147);
    e.setName("kumar");
    e.setSalary(70000);

      dao.saveEmployee(e);

    System.out.println("Employee has been saved in DB..");  
    //dao.updateEmployee(e);
}
}

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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.java</groupId>
  <artifactId>HbStringIntigration</artifactId>
  <version>0.0.1-SNAPSHOT</version>

    <dependencies>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.1.0.Final</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.1.5.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.1.5.RELEASE</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>5.1.5.RELEASE</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-annotations -->
    <!-- <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.5.6-Final</version>
    </dependency> -->

    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
    <!-- <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>3.6.3.Final</version>
    </dependency> -->


    <!-- <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.4.2.Final</version>
    </dependency> -->
    <!-- Hibernate 4 uses Jboss logging, but older versions slf4j for logging -->
     <!--    <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.5</version>
        </dependency> -->
       <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.0.5</version>
        </dependency>


       <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.0.3.RELEASE</version>
        </dependency>


       <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.2.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
  </dependencies>
</project>

如果我们观察到控制台,则该应用程序似乎运行良好,但显示以下警告消息:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
BeanFactory created ..
Sep 17, 2019 12:44:38 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.1.0.Final}
Sep 17, 2019 12:44:38 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Sep 17, 2019 12:44:38 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Sep 17, 2019 12:44:38 PM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-mapping. Use namespace http://www.hibernate.org/dtd/hibernate-mapping instead.  Support for obsolete DTD/XSD namespaces may be removed at any time.
Sep 17, 2019 12:44:41 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Sep 17, 2019 12:44:42 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Sep 17, 2019 12:44:42 PM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
Employee Dao:com.java.com.EmployeeDao@611a990b
Employee has been saved in DB.. 

其次是在数据库中没有数据插入。 有人可以请我帮忙吗?

1 个答案:

答案 0 :(得分:0)

也许您需要添加一行 <property name="connection.autocommit">true </property> 进入休眠配置文件<props>