一、所需工具

  安装jdk、IntelliJ IDEA和mysql数据库。

二、SpringDataJpa快速起步

  开发环境的搭建:

          ①、在IDEA软件上添加依赖包:在http://mvnrepository.com/网址里面搜索spring-data-jpa,选择1.8.0版本。复制依赖包添加到IDEA的pom.xml文件上面。

            

  <dependencies>
<!--MySQL Driver-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</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>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>
</dependencies>

          ②、在resource文件夹下建立一个beans.xml文件。配置内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!--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="062988"/>
<property name="url" value="jdbc:mysql://localhost/spring_test"/>
</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.example"/> <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.example" entity-manager-factory-ref="entityManagerFactory"/>
<context:component-scan base-package="com.example"/>
</beans>

  在java文件夹下面建立一个包repository,在该文件夹下建立一个接口EmployeeRepository

/**
* 通过注解方式实现
*/ @RepositoryDefinition(domainClass = Employee.class,idClass = Integer.class)
public interface EmployeeRepository //extends Repository<Employee,Integer> {
{
public Employee findByName(String name);
public Employee findByAge(Integer age);
public Employee findById(Integer id); //where name like ?% and age <?
public List<Employee> findByNameStartingWithAndAgeLessThan(String name,Integer age);
//where name like ?% and age <?
public List<Employee> findByNameEndingWithAndAgeLessThan(String name,Integer age);
//where name in (?,?....) or age<?
public List<Employee> findByNameInOrAgeLessThan(List<String> names,Integer age); //where name in (?,?....) and age<?
public List<Employee> findByNameInAndAgeLessThan(List<String> names,Integer age); @Query("select o from Employee o where id=(select max(id) from Employee t1)")
public Employee getEmployeeByMaxId(); @Query("select o from Employee o where o.name=?1 and o.age=?2")
public List<Employee> queryParams1(String name,Integer age); @Query("select o from Employee o where o.name=:name and o.age=:age")
public List<Employee> queryParams2(@org.springframework.data.repository.query.Param("name") String name, @org.springframework.data.repository.query.Param("age") Integer age); @Query("select o from Employee o where o.name like %?1%")
public List<Employee> queryLike1( String name); @Query("select o from Employee o where o.name like %:name%")
public List<Employee> queryLike2(@org.springframework.data.repository.query.Param("name")String name); @Query(nativeQuery = true,value = "select count(1) from employee")
public long getCount(); @Modifying
@Query("update Employee o set o.age= :age where o.id = :id")
public void updata(@org.springframework.data.repository.query.Param("id")Integer id, @org.springframework.data.repository.query.Param("age")Integer age); @Modifying
@Query("delete from Employee o where o.id = :id")
public void delete(@org.springframework.data.repository.query.Param("id")Integer id); } /***
* 通过继承Repository接口实现
*/
//public interface EmployeeRepository extends Repository<Employee,Integer> {
// public Employee findByName(String name);
// public Employee findByAge(Integer age);
// public Employee findById(Integer id);
//}

  在test文件夹下建立一个包repository,在该文件夹下建立一个测试类EmployeeRepositoryTest.java。

public class EmployeeRepositoryTest {

    private ApplicationContext ctx = null;
private EmployeeRepository employeeRepository = null; @Before
public void setup() {
ctx = new ClassPathXmlApplicationContext("beans-new.xml");
employeeRepository = ctx.getBean(EmployeeRepository.class);
System.out.println("setup");
} @After
public void tearDown() {
ctx = null;
System.out.println("tearDown");
} @Test
public void testFindByName() {
Employee employee = employeeRepository.findByName("zhangsan");
System.out.println("id:"+employee.getId()+",name:"+employee.getName()+",age:"+employee.getAge());
}
@Test
public void testFindByAge() {
Employee employee = employeeRepository.findByAge(21);
System.out.println("id:"+employee.getId()+",name:"+employee.getName()+",age:"+employee.getAge());
}
@Test
public void testFindById() {
Employee employee = employeeRepository.findById(1);
System.out.println("id:"+employee.getId()+",name:"+employee.getName()+",age:"+employee.getAge());
} @Test
public void testFindByNameStartingWithAndAgeLessThan() {
List<Employee> employees = employeeRepository.findByNameStartingWithAndAgeLessThan("test",22);
for(Employee employee:employees) { System.out.println("id:"+employee.getId()+"," +
"name:"+employee.getName()+
",age:"+employee.getAge());
}
} @Test
public void testFindByNameEndingWithAndAgeLessThan() {
List<Employee> employees = employeeRepository.findByNameEndingWithAndAgeLessThan("6",23);
for(Employee employee:employees) { System.out.println("id:"+employee.getId()+"," +
"name:"+employee.getName()+
",age:"+employee.getAge());
}
} @Test
public void testFindByNameInOrAgeLessThan() {
List<String> names=new ArrayList<String>();
names.add("test1");
names.add("test2");
names.add("test3");
List<Employee> employees = employeeRepository.findByNameInOrAgeLessThan(names,22);
for(Employee employee:employees) {
System.out.println("id:"+employee.getId()+"," +
"name:"+employee.getName()+
",age:"+employee.getAge());
}
} @Test
public void testFindByNameInAndAgeLessThan() {
List<String> names=new ArrayList<String>();
names.add("test1");
names.add("test2");
names.add("test3");
List<Employee> employees = employeeRepository.findByNameInAndAgeLessThan(names,22);
for(Employee employee:employees) {
System.out.println("id:"+employee.getId()+"," +
"name:"+employee.getName()+
",age:"+employee.getAge());
}
} @Test
public void testGetEmployeeByMaxId() {
Employee employee = employeeRepository.getEmployeeByMaxId();
System.out.println("id:"+employee.getId()+"," +
"name:"+employee.getName()+
",age:"+employee.getAge());
} @Test
public void testQueryParams1() {
List<Employee> employees = employeeRepository.queryParams1("lisi",21);
for (Employee employee:employees) {
System.out.println("id:"+employee.getId()+"," +
"name:"+employee.getName()+
",age:"+employee.getAge());
}
}
@Test
public void testQueryParams2() {
List<Employee> employees = employeeRepository.queryParams2("lisi",21);
for (Employee employee:employees) {
System.out.println("id:"+employee.getId()+"," +
"name:"+employee.getName()+
",age:"+employee.getAge());
}
} @Test
public void testQueryLike1() {
List<Employee> employees = employeeRepository.queryLike1("test");
for (Employee employee:employees) {
System.out.println("id:"+employee.getId()+"," +
"name:"+employee.getName()+
",age:"+employee.getAge());
}
} @Test
public void testQueryLike2() {
List<Employee> employees = employeeRepository.queryLike2("test1");
for (Employee employee:employees) {
System.out.println("id:"+employee.getId()+"," +
"name:"+employee.getName()+
",age:"+employee.getAge());
}
} @Test
public void testGetCount() {
long count = employeeRepository.getCount();
System.out.println("count:"+count);
}
}

  点击testFindByName右键运行,控制台将打印出根据名字查询到的数据库信息。

SpringDataJpa开发环境的搭建以及使用的更多相关文章

  1. SpringDataJPA开发环境的搭建

    这里简单的介绍一下使用maven工程创建SpringDataJPA的开发环境的搭建 首先引入依赖 <dependencies> <!-- junit单元测试 --> <d ...

  2. 总结:Mac前端开发环境的搭建(配置)

    新年新气象,在2016年的第一天,我入手了人生中第一台自己的电脑(大一时好友赠送的电脑在一次无意中烧坏了主板,此后便不断借用别人的或者网站的).macbook air,身上已无分文...接下来半年的房 ...

  3. Idea开发环境中搭建Maven并且使用Maven打包部署程序

    1.配置Maven的环境变量 a.首先我们去maven官网下载Maven程序,解压到安装目录,如图所示: b.配置M2_HOME的环境变量,然后将该变量添加到Path中 备注:必须要有JAVA_HOM ...

  4. Linux学习心得之 Linux下命令行Android开发环境的搭建

    作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 Linux学习心得之 Linux下命令行Android开发环境的搭建 1. 前言2. Jav ...

  5. Java开发环境的搭建以及使用eclipse从头一步步创建java项目

    一.java 开发环境的搭建 这里主要说的是在windows 环境下怎么配置环境. 1.首先安装JDK java的sdk简称JDK ,去其官方网站下载最近的JDK即可..http://www.orac ...

  6. Linux环境下Android开发环境的搭建

    本文主要介绍在Ubuntu下Android开发环境的搭建,所使用的IDE为Eclipse(Android Studio同理,且可省去配置SDK以及安装adt插件的步骤). 1.安装JDK (1)JDK ...

  7. Apache+MySQL+PHP开发环境的搭建(二)

    通过自主选择相应的apache,mysql,php等软件,根据自己的应用开发需求进行安装.此方法搭建的环境自主性较强,搭建过程较为复杂,繁琐. 1.所需软件: Apache: http-2.2.22- ...

  8. Apache+MySQL+PHP开发环境的搭建(一)

    通过套件来安装和配置php开发环境. 1.所需软件:AppServ(因为是开源,任何网站都能下载) 安装本软件基本上就是下一步. 2.进行安装 选择一个盘安装该软件点击next继续: 根据自己的实际情 ...

  9. windows上JSP开发环境全搭建

    JSP开发环境全搭建 最近需要用到JSP做项目,所以要配置JSP的开发环境,总结一下配置步骤以备以后再配置需要. 配置JAVA开发环境,配置JDK 下载JDK,在这里下载开发所需的JDK,可以根据自己 ...

随机推荐

  1. VSCode之快捷键和常用插件

    前言 介绍一下我在VSCode中常用的一些快捷方式: ctrl+上下箭头 上下滚动页面 Ctrl+Shift+K 删除某一行 Alt+ ↑ / ↓ 移动某一行 Shift+Alt + ↓ / ↑ 复制 ...

  2. MySQL 千万 级数据量根据(索引)优化 查询 速度

    一.索引的作用 索引通俗来讲就相当于书的目录,当我们根据条件查询的时候,没有索引,便需要全表扫描,数据量少还可以,一旦数据量超过百万甚至千万,一条查询sql执行往往需要几十秒甚至更多,5秒以上就已经让 ...

  3. exists oracle 的用法

    CREATE TABLE `A` ( `id` ) NOT NULL AUTO_INCREMENT, `name` ) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGIN ...

  4. div左右居中css

    l_btn{ font-size: 1.2rem; width: 190px; height: 50px; border: 1px solid #fff; border-radius: 25px; c ...

  5. 4.express 框架

    express express 官网:http://expressjs.com/ 中文:http://www.expressjs.com.cn/ 库:jquery 一堆 的属性和方法 框架:expre ...

  6. Ubantu 好玩以及有用的命令

    1.cowsay 图像 Dell:~$ sudo apt-get install cowsay Dell:~$ cowsay -f elephant-in-snake 00 Dell:~$ cowsa ...

  7. HTML5 - Canvas【申明:来源于网络】

    HTML5 - Canvas 地址:http://hwaphon.site/

  8. linux crontab详解 php开发相关

    vi vi /etc/crontab 注意不是这么直接干的! 下面是内容 SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root ...

  9. Linux3.10.0块IO子系统流程(0)-- 块IO子系统概述

    前言:这个系列主要是记录自己学习Linux块IO子系统的过程,其中代码分析皆基于Linux3.10.0版本,如有描述错误或不妥之处,敬请指出! 参考书籍:存储技术原理分析--基于Linux 2.6内核 ...

  10. Java学习之路-Hessian学习

    Hessian是基于HTTP的轻量级远程服务解决方案,Hessian像Rmi一样,使用二进制消息进行客户端和服务器端交互.但与其他二进制远程调用技术(例如Rmi)不同的是,它的二进制消息可以移植其他非 ...