Spring data integrates JPA (specifically implemented as (Hibernate));

Keywords: Spring Hibernate Database Oracle

Here, we take Maven as an example to send the map first, then the text information of Maven and configuration file (easy to copy and paste); the Oracle11g database jar package is in the lib directory of Oracle database installation, which is not specifically introduced here
Spring integrates Jpa configuration files: be careful, careful and careful;

      Step 2: create the mapping entity class according to the package pointed to by the above configuration information


Step 3: write DAO interface (note that all the interfaces used by Mybatis are clear)

Step 4: unit test automatically creates tables and inserts data

Step 5, test OK Oracle to automatically create tables and add data;

Dependencies required for pom files

<properties>
    <spring.version>4.2.4.RELEASE</spring.version>
    <hibernate.version>5.0.7.Final</hibernate.version>
    <slf4j.version>1.6.6</slf4j.version>
    <log4j.version>1.2.12</log4j.version>
    <c3p0.version>0.9.1.2</c3p0.version>
    <mysql.version>5.1.6</mysql.version>
</properties>
<dependencies>
    <!-- junit unit testing -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.9</version>
        <scope>test</scope>
    </dependency>

    <!-- spring beg -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.6.8</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- spring end -->

    <!-- hibernate beg -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.2.1.Final</version>
    </dependency>
    <!-- hibernate end -->

    <!-- c3p0 beg -->
    <dependency>
        <groupId>c3p0</groupId>
        <artifactId>c3p0</artifactId>
        <version>${c3p0.version}</version>
    </dependency>
    <!-- c3p0 end -->

    <!-- log end -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
    <!-- log end -->

    <!-- https://mvnrepository.com/artifact/oracle/ojdbc6 -->
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0.1.0</version>
    </dependency>


    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.9.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>

    <!-- el beg Use spring data jpa Must introduce -->
    <dependency>
        <groupId>javax.el</groupId>
        <artifactId>javax.el-api</artifactId>
        <version>2.2.4</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.web</groupId>
        <artifactId>javax.el</artifactId>
        <version>2.2.4</version>
    </dependency>
    <!-- el end -->
</dependencies>

applicationContext.xml configuration file

<?xml version="1.0" encoding="UTF-8"?>


<context:property-placeholder location="oracleDataConnection.properties"></context:property-placeholder>
<!--1,To configure DataSource (The test here is Oracle)Database connection pool-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${oracleDriver}"></property>
    <property name="jdbcUrl" value="${oracleUrl}"></property>
    <property name="user" value="${oracleUser}"></property>
    <property name="password" value="${oraclePassword}"></property>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <!--Configure database connection pool-->
   <property name="dataSource" ref="dataSource"/>
    <!--Configure scan package entity class-->
    <property name="packagesToScan" value="com.out.entity"/>
    <!--injection Hibernate Yes Jpa Implementation-->
    <property name="persistenceProvider" ref="hibernateId" />
    <!--Jpa Vendor adapter for-->
    <property name="jpaVendorAdapter">
      <bean  class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" >
          <!--Configure whether the corresponding connection information is automatically created in the adapter(If you don't have to specify it, you will report an error ) true It's a native configuration(update)-->
          <property name="generateDdl" value="true"/>
          <!--Selection database,Look at the source code. There are enumerations,MySQL,SQLService Yes, there are.-->
          <property name="database" value="ORACLE"/>
          <!--Show dialect or not-->
          <property name="showSql" value="true"/>

          <!--Specify database platform-->
       <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect"></property>
      </bean>
    </property>
    <!--To configure JPA Dialect information,There are also source codes,Mysql Of Oracle It's all visible-->
    <property name="jpaDialect" >
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"></bean>
    </property>
</bean>
<!--Here statement Hibernate  Can be configured directly to persistenceProvider in,But I didn't do it to make it clear-->
<bean id="hibernateId" class="org.hibernate.jpa.HibernatePersistenceProvider"></bean>
 <!--3,Configure transaction manager       The transaction manager is different now notice-->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!--Open declaration transaction,Of course, you can use the specified aspect to control the transaction-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--Start the integration operation. The scanning package here is scanning DAO layer,No need to write the implementation class to operate the database-->
<jpa:repositories base-package="com.out.dao"
                  transaction-manager-ref="transactionManager"
                     entity-manager-factory-ref="entityManagerFactory" />
<!--Assembly of other containers-->
<context:component-scan base-package="com.out"/> 
</beans>

Entity mapping class:
package com.out.entity;

import javax.persistence.*;

/**

  • @author misterWei

  • @mailbox @gmail.com
    */
    //Declaration mapping relationship
    @Entity
    @Table(name = "s_student")
    public class Student {
    //Configure primary key and other field names
    @Id / / primary key generation policy
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "s_id" )
    private Integer sid;
    @Column(name = "s_name",length = 20)
    private String sname;
    @Column(name = "s_gender",length = 1)
    private Integer gender;
    @Column(name="s_age",length = 3)
    private Integer age;

    public String getGrade() {
    return grade;
    }

    public void setGrade(String grade) {
    this.grade = grade;
    }

    @Column(name="s_grade",length = 10)
    private String grade;

    public Integer getSid() {
    return sid;
    }

    public void setSid(Integer sid) {
    this.sid = sid;
    }

    public String getSname() {
    return sname;
    }

    public void setSname(String sname) {
    this.sname = sname;
    }

    public Integer getAge() {
    return age;
    }

    public void setAge(Integer age) {
    this.age = age;
    }

    public Integer getGender() {
    return gender;
    }

    public void setGender(Integer gender) {
    this.gender = gender;
    }

    @Override
    public String toString() {
    return "Person{" +
    "sid=" + sid +
    ", sname='" + sname + ''' +
    ", age=" + age +
    ", gender=" + gender +
    '}';
    }

}

DAO interface
package com.out.dao;

import com.out.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

/**
*
*/
//At this point, the Dao layer is an interface, which only needs to inherit two interfaces to use the API
//JpaRepository is used for basic crud < entity class, corresponding primary key type >
//Jpaspecification executor is used to perform complex query paging operations, etc. < entity class >
public interface StudentDao extends
JpaRepository<Student,Long>,JpaSpecificationExecutor {
}

Unit test:
package com.out;

import com.out.dao.StudentDao;
import com.out.entity.Student;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**

*/
//Simple unit testing
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class JavaConnectionOrcleTest {
@Autowired
private StudentDao studentDao;
@Test
public void insertOne(){
Student student = new Student();
student.setSname("Xiaoming students");
student.setAge(18);
student.setGender(1);
student.setGrade("class 2, grade 3");
studentDao.save(student);
}
}

Posted by manyamile on Thu, 26 Dec 2019 13:40:04 -0800