springbootjpa的大致使用及测试

项目结构:
springbootjpa的大致使用及测试_第1张图片
applicationcontext.xml配置文件:


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/data/jpa
		http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
    

    
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        
        <property name="dataSource" ref="dataSource"/>
        
        <property name="packagesToScan" value="domain"/>
        
        <property name="persistenceProvider">
            <bean class="org.hibernate.jpa.HibernatePersistenceProvider"/>
        property>
        
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                
                <property name="generateDdl" value="false" />
                
                <property name="database" value="MYSQL" />
                <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
                
                <property name="showSql" value="true" />
            bean>
        property>
        
        <property name="jpaDialect">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
        property>
    bean>

    
    <bean id="dataSource"  class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="root"/>
        <property name="password" value="totoro"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test2?useSSL=false&serverTimezone=UTC"/>
        <property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
    bean>

    
    
    <jpa:repositories base-package="dao" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"/>

    
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    bean>

    
    <context:component-scan base-package="java"/>
    
beans>

实体类的使用:

package domain;

import javax.persistence.*;

@Entity
@Table(name = "address")
public class Address {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;
    @Column(name = "description")
    private String des;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getDes() {
        return des;
    }

    public void setDes(String des) {
        this.des = des;
    }

    @Override
    public String toString() {
        return "Address{" +
                "id=" + id +
                ", des='" + des + '\'' +
                '}';
    }
}

持久层代码示例:

package dao;

import domain.Address;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

/**
 * 只需要编写dao接口层,不需要编写dao层接口的实体类
 * dao层接口规范
 *      1.需要继承两个接口JpaRepository和JpaSpecificationExecutor
 *      2.需要提供相应的泛型
 *          JpaRepository<操作的实体类类型,实体类中主键属性的类型>
 *              封装了基本的增删改查操作
 *          JpaSpecificationExecutor<操作的实体类类型>
 *              封装了复杂查询
 *      3.继承这两个接口以后,就可以实现数据的增删改查
 */
public interface AddressDao extends JpaRepository<Address,Integer>, JpaSpecificationExecutor<Address> {
}

测试类:

import dao.AddressDao;
import domain.Address;
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;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)//声明spring提供的单元测试环境
@ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置信息
public class DaoTest {
    @Autowired
    AddressDao addressDao;
    @Test
    public void findById(){
        System.out.println(addressDao.findOne(1).toString());
    }
    @Test
    public void AddA(){
        Address address = new Address();
        address.setDes("啦啦啦");
        addressDao.save(address);
    }
    @Test
    public void FindAll(){
        List<Address> list = addressDao.findAll();
        for (Address a:list){
            System.out.println(a.toString());
        }
    }
}

你可能感兴趣的:(随手记)