Spring data jpa 入门环境搭建
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的使用了
推荐阅读
-
Android开发入门环境快速搭建实战教程
-
序列化表单为json对象,datagrid带额外参提交一次查询 后台用Spring data JPA 实现带条件的分页查询 多表关联查询
-
spring data jpa碰到的坑
-
python框架flask入门之环境搭建及开启调试
-
使用Spring Data JPA进行数据分页与排序
-
Redis入门(介绍、搭建)——Windows、Centos环境
-
spring-data-jpa实现增删改查以及分页操作方法
-
详解Spring Data JPA使用@Query注解(Using @Query)
-
Spring Data JPA使用Sort进行排序(Using Sort)
-
详解Spring Data JPA系列之投影(Projection)的用法