一、所需工具

  安装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. struts与servlet共存

    <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2 ...

  2. .net framework 项目 build 出现 未能加载文件或程序集“netfx.force.conflicts”或它的某一个依赖项

    问题描述 Severity Code Description Project File Line Suppression State Error 未能加载文件或程序集"netfx.force ...

  3. 一个自定义python分布式专用爬虫框架。支持断点爬取和确保消息100%不丢失,哪怕是在爬取进行中随意关停和随意对电脑断电。

    0.此框架只能用于爬虫,由框架来调度url请求,必须按照此方式开发,没有做到类似celery的通用分布式功能,也不方便测试.可以使用另外一个,基于函数式编程的,调度一切函数的分布式框架,做到了兼容任何 ...

  4. python中导入一个需要传参的模块

    最近跑实验,遇到了一个问题:由于实验数据集比较多,每次跑完一个数据集就需要手动更改文件路径,再将文件传到服务器,再运行实验,这样的话效率很低,必须要专门看着这个实验,啥时候跑完就手动修改运行下一个实验 ...

  5. MySQL设置密码复杂度

    MySQL5.6.6版本之后增加了密码强度验证插件validate_password,相关参数设置的较为严格.使用了该插件会检查设置的密码是否符合当前设置的强度规则,若不满足则拒绝设置. 本文采用测试 ...

  6. [BTS] BizTalk EDI character set and separator settings

    最近一个项目上遇到需要对EDI头.分隔符.小数点等配置项进行设置. 这里只记录一下结果,原理不记了,多看MSDN. UNB1中的信息在BizTalk中是不会体现在EDI输出文件中的,双方协商好即可. ...

  7. Thinkpad L440 无线驱动突然无法使用,无法搜索到无线上网

    问题描述: 环境:Thinkpad L440,不知道是什么版本的,找朋友买的,买的时候连系统都没有,自己装的Win7系统,驱动均为官方网站下载安装.电脑在使用过程中一直带着电池,连接电源使用. 问题: ...

  8. 一、K3 WISE 开发插件《K3 WISE常用数据表整理》

    在后台数据库ICClassType表中,字段FID<0的是老单,FID>0的是新单. ----------------系统设置------------------------FStatus ...

  9. [原]Jenkins(二十) jenkins再出发之Error: Opening Robot Framework log failed

    错误缘由:使用plugin [public robot framework test results] 生成的HTML文件都无法正常打开.   解决方案: Connect on your jenkin ...

  10. MapReduce Map数 reduce数设置

    JobConf.setNumMapTasks(n)是有意义的,结合block size会具体影响到map任务的个数,详见FileInputFormat.getSplits源码.假设没有设置mapred ...