zoukankan      html  css  js  c++  java
  • SpringData——HelloWorld

    1、背景

    最开始了解SpringData的时候,以为他不就是ORM的一种实现方式嘛,还能有什么新的东西。从hibernate到ibatis、mybatis,也许他只不过是spring想整合一个更方便的orm实现吧。

    NoNoNo!!!

    SpringData是希望提供一个统一的数据访问层的编程模型。

    如何理解呢?

    就拿SpringData中一个典型项目Spring Data MongoDB来说吧。之前我们用的hibernate、mybatis都是对应的关系型数据库,CRUD的封装也只是在这个前提之下进行的。

    而对于非关系型数据库来说,比如MongoDB,它也有相应的CRUD的操作,但在项目中,我们还需要另外封装一套。这样做确实挺麻烦的,如果能够统一解决就最好了。Sprint Data MongoDB就是从这样的角度出发的,他不再区分关系型非关系型数据库,而是把数据访问这一部分封装成一个模式。

    2、实例

    1)、引入jar包

    <!-- spring data -->
             <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-releasetrain</artifactId>
                <version>Hopper-SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-jpa</artifactId>
                <version>1.10.1.RELEASE</version>
            </dependency>

    2)、根据实体生成表

    @Entity
    public class Employee {
        @Id
        @GeneratedValue
        private  Long id;
        private String firstName, lastName, description;
    
        private Employee() {
        }
    
        public Employee(String firstName, String lastName, String description) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.description = description;
        }
    
        //getter和setter方法略
    }

    添加配置信息:
    spring.jpa.show-sql= true
    spring.jpa.hibernate.ddl-auto=update

    3)、新建dao的接口

    public interface EmployeeRepository extends CrudRepository<Employee,Long>{
    
        Employee findByFirstName(String firstName);
    
    }

    EmployeeRepository 继承自CrudRepository,CrudRepository使用泛型的方式提供了基本的数据操作方法。

    public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {
        <S extends T> S save(S entity);
        boolean exists(ID id);
        Iterable<T> findAll();
        void delete(ID id);
        ……
    }

    4)、Service接口

    EmployeeService

    public interface EmployeeService  {
        Employee findByFirstName(String firstName);
        Employee saveEmployee(Employee employee);   
    }

    EmployeeServiceImpl

    @Service
    public class EmployeeServiceImpl implements EmployeeService {
    
        //注入dao
        @Autowired
        private EmployeeRepository employeeRepository;
    
        @Override
        public Employee findByFirstName(String firstName) {
            return employeeRepository.findByFirstName(firstName);   
        }
    
        @Override
        public Employee saveEmployee(Employee employee) {
            return employeeRepository.save(employee);    
        }
    }

    5)、测试
    在Controller中编写方法:

    //插入数据
        @RequestMapping("/add")
        public Employee addByData(){
            return employeeService.saveEmployee(new Employee("Sherry","yang","员工"));
        }
        //根据firstName查询
       @RequestMapping("/queryByfirstName")
        public List<Employee> query(){
           return employeeService.findByFirstName("Sherry");
        }

    3、关键字

    通过上面的小demo,可以看出,使用springData来实现简单的CRUD,比通用mapper还要方便呢,尤其在复杂查询上更为明显。这都要归功于SpringData的一大创新,查询关键字。

    以上面的findByFirstName为例,我们需要遵循一定的约定,这样才能更好的使用他。

    下面列举了一小部分关键字:

    Keyword Spring Data JPQL snippet
    And findByLastnameAndFirstname where x.lastname = ?1 and x.firstname = ?2
    Or findByLastnameOrFirstname where x.lastname = ?1 or x.firstname = ?2
    LessThan findByAgeLessThan where x.age < ?1
    OrderBy findByAgeOrderByLastnameDesc where x.age = ?1 order by x.lastname desc
    Is,Equals findByFirstnameIs,findByFirstnameEquals where x.firstname = ?1

    至于SpringData对于关系型和非关系型数据库的数据访问封装,我们下篇再继续聊。

  • 相关阅读:
    【BZOJ 3569】DZY Loves Chinese II
    【POJ3155】生活的艰辛Hard Life
    【SGU194】Reactor Cooling
    Elasticsearch 6.1.2 搭建及使用教程一
    Centos7安装Mysql-最方便、最快捷
    Realm_King 之 .NET 打包详细教程(A)
    C#程序员经常用到的10个实用代码片段
    使用VS2010再装VS2013不用再烦恼不兼容
    表变量和临时表的使用
    golang连接ftp服务器
  • 原文地址:https://www.cnblogs.com/saixing/p/6730192.html
Copyright © 2011-2022 走看看