欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring data jpa 入门环境搭建

程序员文章站 2022-05-28 18:39:46
...

springdata -jpa模块入门环境搭建

1、 导入需要的jar包(这里以maven为准)
只需要导入三个maven坐标 mysql、hibernate、springdata -jpa

<!--MySQL Driver-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>

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

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.6.Final</version>
        </dependency>

2、xml文件的配置

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

<!--1:配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="username" value="root"/>
    <property name="password" value="12345"/>
    <property name="url" value="jdbc:mysql://localhost/demo"/>
</bean>

<!--2:配置EntityManagerFactory  通过这个类管理实体类与数据库-->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
    </property>
    <!-- 告诉实体类的位置 -->
    <property name="packagesToScan" value="com.mc"/>

    <property name="jpaProperties">
        <props>
            <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>

<!--3:配置事物管理器-->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!--4:配置支持注解的事务-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--5:配置spring data-->
<jpa:repositories base-package="com.mc" entity-manager-factory-ref="entityManagerFactory"/>
<!--对这个包下的类进行注解扫描-->
<context:component-scan base-package="com.mc"/>

3、编写实体类
package com.mc.entrty;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity // 告诉springdata 对这个类进行建表
public class User {

private Integer id;
private String name;
private  Integer age;

@aaa@qq.com // 这个字段是id 与自动增长
public Integer getId() {
    return id;
}

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

@Column(length = 20,nullable = false)
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Integer getAge() {
    return age;
}

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

@Override
public String toString() {
    return "User{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", age=" + age +
            '}';
}

}

4、编写一个接口并继承jpa的核心接口
package com.mc.repository;

import com.mc.entrty.User;
import org.springframework.data.repository.Repository;

public interface UserRepository extends Repository<User,Integer> {
public User findByName(String name);
}

5、测试就ok了

package com.mc.test;

import com.mc.entrty.User;
import com.mc.repository.UserRepository;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Demo {

@Test
public void testUserRepository()
{
    ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
    UserRepository userRepository = ctx.getBean(UserRepository.class);
    User user = userRepository.findByName("zs");
    System.out.println(user);
}

}

这是springdata -jpa的入门程序 、希望能帮助向我一样的初学者快速的人门、也同时希望和大家一起讨论进步,入门搭建好之后就可以拓展其他jpa的使用了

Spring data jpa 入门环境搭建

相关标签: Java