JPA与Spring的简单整合

spring配置文件

<?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" 
       xmlns:tx="http://www.springframework.org/schema/tx"
       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.xsd 
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--spring包扫描-->
    <context:component-scan base-package="com.heiketu"/>

    <!--导入外部文件-->
    <context:property-placeholder location="classpath:db.properties"/>

    <!--配置数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.pass}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="driverClass" value="${jdbc.driver}"/>
    </bean>

    <!--配置entityManagerFactory-->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--配置 JPA 提供商的适配器. 可以通过内部 bean 的方式来配置-->
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
        <!--配置实体所在包-->
        <property name="packagesToScan" value="com.heiketu.pojo"/>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>

    <!--配置事物管理器-->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <!--配置支持注解的事物管理-->
    <tx:annotation-driven transaction-manager="transactionManager" />

</beans>

数据源的properties

jdbc.user=root
jdbc.pass=admin
jdbc.url=jdbc:mysql:///test?serverTimezone=GMT%2B8
# com.mysql.cj.jdbc.Driver -> MySQL7.0以上的版本驱动class 有所更改
jdbc.driver=com.mysql.jdbc.Driver

Spring中使用JPA:
在Dao层通过@PersistenceContext注解,获取到EntityManager即可使用JPA,代码如下:

@Repository
public class TestDao {

    @PersistenceContext
    private EntityManager entityManager;

    public void save(TestClass testClass){
        entityManager.persist(testClass);
    }

}

@Repository容器为Spring容器的注解,使用该注解将该Dao加入到spring容器,交由Spring来管理。

JPA与Spring整合使用到的测试Jar包示例如下:

hiberante的JPA实现所需Jar包:

required目录下

c3p0数据源

JPA规范包

Spring相关Jar包:

MySQL驱动包

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring JPASpring框架与Java Persistence API(JPA)的集成。它提供了一种简化数据库操作的方式,使得开发者可以通过面向对象的方式进行数据库的增删改查操作,而不需要编写繁琐的SQL语句。 要实现Spring JPA整合,首先需要添加相关的依赖。在Maven项目中,可以通过在pom.xml文件中添加以下依赖来引入Spring JPA: ```xml <dependencies> <!-- Spring Boot 和 Spring JPA 的核心依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- 数据库驱动依赖,根据自己使用的数据库选择相应的依赖 --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <!-- 其他依赖 --> </dependencies> ``` 接下来,在Spring Boot的主启动类上添加注解`@EnableJpaRepositories`和`@EntityScan`,分别用于启用JPA仓库和扫描实体类。通常情况下,主启动类位于根包下。 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @SpringBootApplication @EnableJpaRepositories(basePackages = "com.example.repository") // 指定JPA仓库的包路径 @EntityScan(basePackages = "com.example.entity") // 指定实体类的包路径 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 接下来,创建一个实体类,并使用JPA注解进行配置。例如,假设我们要操作一个名为`User`的实体类,可以按照以下方式进行配置: ```java import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String username; private String password; // 省略构造方法、Getter和Setter等 } ``` 然后,创建一个JPA仓库接口,继承自`JpaRepository`或其子接口。该接口将自动提供一组基本的增删改查方法。例如,我们可以创建一个名为`UserRepository`的接口: ```java import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository<User, Long> { // 可以在这里定义一些自定义的查询方法 } ``` 至此,Spring JPA整合工作就完成了。我们可以在服务类或控制器中注入`UserRepository`,并使用其提供的方法进行数据库操作。 以上是简单Spring JPA整合示例,实际应用中可能会涉及更复杂的配置和操作。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值