美文网首页
(《深入实践Spring Boot》笔记2)在Spring Bo

(《深入实践Spring Boot》笔记2)在Spring Bo

作者: a6fc544968bb | 来源:发表于2018-02-10 11:12 被阅读0次

IDE软件:IntelliJ IDEA 15.0.2
操作系统:Win10
Spring boot版本:1.4.1 Release
Maven版本:apache-maven-3.5.2


一、引言

  使用数据库是开发基本应用的基础。借助于开发框架,我们已经不用编写原始的访问数据库的代码,也不用调用JDBC(Java Data Base Connectivity)或者连接池等诸如此类的被称作底层的代码,我们将在高级的层次上访问数据库。而Spring Boot更是突破了以前所有开发框架访问数据库的方法,在前所未有的更加高级的层次上访问数据库。因为Spring Boot包含一个功能强大的资源库,为使用Spring Boot的开发者提供了更加简便的接口进行访问。 本文学习怎样使用传统的关系型数据库,以及近期一段时间异军突起的NoSQL(Not Only SQL)数据库。 实例工程使用了分模块的方式构建,各模块的定义如表:

项目 工程 功能
MySql模块 mysql 使用MySql
Redis模块 redis 使用Redis
MongoDB模块 mongodb 使用MongoDB
Neo4j模块 neo4j 使用Neo4j

二、使用Mysql数据库

  对于传统关系型数据库来说,Spring Boot使用JPA(Java Persistence API)资源库来实现对数据库的操作,使用MySQL也是如此。简单地说,JPA就是为POJO(Plain Ordinary Java Object)提供持久化的标准规范,即将Java的普通对象通过对象关系映射(Object-Relational Mapping,ORM)持久化到数据库中。

  1. MySQL依赖配置
      为了使用JPA和MySQL,首先在工程中引入它们的Maven依赖,如代码清单所示。其中,指定了在运行时调用MySQL的依赖。
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
 </dependency>

2. 实体建模
  首先创建一些普通对象,用来与数据库的表建立映射关系,接着演示如何使用JPA对数据库进行增删查改等存取操作。 假如现在有三个实体:部门、用户和角色,并且它们具有一定的关系,即一个用户只能隶属于一个部门,一个用户可以拥有多个角色。Spring Boot的实体建模与使用Spring框架时的定义方法一样,同样比较方便的是使用了注解的方式来实现。
  注解@Table指定关联的数据库的表名,注解@Id定义一条记录的唯一标识,并结合注解@GeneratedValue将其设置为自动生成。部门实体只有两个字段:id和name。

部门实体的建模如代码Department.java:

package springboot.example.domain;

import javax.persistence.*;

/**
 * Created by zhang on 2018/2/10.
 */
@Entity
@Table(name="department")
public class Department {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String name;

    public Department() {
    }

    public long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

  用户实体包含三个字段:id、name和createdate,其中注解@ManyToOne定义它与部门的多对一关系,并且在数据库表中用字段did来表示部门的ID,注解@ManyToMany定义与角色实体的多对多关系,并且用中间表user_role来存储它们各自的ID,以表示它们的对应关系。日期类型的数据必须使用注解@DateTimeFormat来进行格式化,以保证它在存取时能提供正确的格式,避免保存失败。注解@JsonBackReference用来防止关系对象的递归访问。

用户实体的建模如代码User.java:

package springboot.example.domain;

import com.fasterxml.jackson.annotation.JsonBackReference;
import org.springframework.context.annotation.Role;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

/**
 * Created by zhang on 2018/2/10.
 */
@Entity
@Table(name="user")
public class User implements java.io.Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String name;
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createDate;

    @ManyToOne
    @JoinColumn(name="did")
    @JsonBackReference
    private Department department;

    @ManyToMany(cascade = {}, fetch = FetchType.EAGER)
    @JoinTable(name="user_role",
        joinColumns = {@JoinColumn(name = "user_id")},
        inverseJoinColumns = {@JoinColumn(name="roles_id")})
    private List<Role> roles;

    public User() {
    }

    public long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }
}

  角色实体建模比较简单,只要按设计的要求,定义id和name字段即可,当然同样必须保证id的唯一性并将其设定为自动生成。

角色实体的建模如代码Role.java:

package springboot.example.domain;

import javax.persistence.*;

/**
 * Created by zhang on 2018/2/10.
 */



@Entity
@Table(name="role")
public class Role implements java.io.Serializable{
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long id;
    private String name;
    public Role(){
    }

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
  1. 实体持久化
      通过上面三个实体的定义,实现了使用Java的普通对象(POJO)与数据库表建立映射关系(ORM),接下来使用JPA来实现持久化。 它是一个接口,并继承于JPA资源库JpaRepository接口,使用注解@Repository将这个接口也定义为一个资源库,使它能被其他程序引用,并为其他程序提供存取数据库的功能。 使用相同的方法,可以定义部门实体和角色实体的资源库接口。接口同样继承于JpaRepository接口,只要注意使用的参数是各自的实体对象即可。

用户实体使用JPA进行持久化的例子如代码UserRepository.java:

package springboot.example.Repository;

import org.springframework.data.jpa.repository.JpaRepository;
import springboot.example.domain.User;

/**
 * Created by zhang on 2018/2/10.
 */
public interface UserRepository extends JpaRepository<User,Long>{

}

  这样就实现存取数据库的功能了。现在可以对数据库进行增删查改、进行分页查询和指定排序的字段等操作。 或许你还有疑问,我们定义的实体资源库接口并没有声明一个方法,也没有对接口有任何实现的代码,甚至连一条SQL查询语句都没有写,使用JPA就是可以这么简单。我们来看看JpaRe-pository的继承关系,JpaRepository继承于PagingAndSortingRepository,它提供了分页和排序功能,是的,使用JPA就是可以这么简单。我们来看看JpaRe-pository的继承关系,你也许会明白一些。JpaRepository继承于PagingAndSortingRepository,它提供了分页和排序功能,PagingAndSortingRepository继承于Crud-Repository,它提供了简单的增删查改功能。因为定义的接口继承于JpaRepository,所以它传递性地继承上面所有这些接口,并拥有这些接口的所有方法,这样就不难理解为何它包含那么多功能了。这些接口提供的一些方法如下:

<S extends T> S save(S var1); 
T findOne(ID var1); 
long count(); 
void delete(ID var1); 
void delete(T var1); 
void deleteAll(); 
Page<T> findAll(Pageable var1); 
List<T> findAll(); 
List<T> findAll(Sort var1); 
List<T> findAll(Iterable<ID> var1); 
void deleteAllInBatch(); 
T getOne(ID var1); 
......

  JPA还提供了一些自定义声明方法的规则,例如,在接口中使用关键字findBy、readBy、getBy作为方法名的前缀,拼接实体类中的属性字段(首个字母大写),并可选择拼接一些SQL查询关键字来组合成一个查询方法。例如,对于用户实体,下列查询关键字可以这样使用:

·And,例如findByIdAndName(Long id,String name);
·Or,例如findByIdOrName(Long id,String name);
·Between,例如findByCreatedateBetween(Date start,Date end);
·LessThan,例如findByCreatedateLessThan(Date start);
·GreaterThan,例如findByCreatedateGreaterThan(Date start);
·IsNull,例如findByNameIsNull();
·IsNotNull,例如findByNameIsNotNull(); 
·NotNull,与IsNotNull等价;
·Like,例如findByNameLike(String name); 
·NotLike,例如findByNameNotLike(String name); 
·OrderBy,例如findByNameOrderByIdAsc(String name); 
·Not,例如findByNameNot(String name); 
·In,例如findByNameIn(Collection<String>nameList); 
·NotIn,例如findByNameNotIn(Collection<String>nameList)。

  又如下列对用户实体类自定义的方法声明,它们都是符合JPA规则的,这些方法也不用实现,JPA将会代理实现这些方法。

User findByNameLike(String name); 
User readByName(String name);
List<User> getByCreatedateLessThan(Date star);
  1. Mysql测试
      现在,为了验证上面设计的正确性,我们用一个实例来测试一下。 首先,增加一个使用JPA的配置类,其中@EnableTransac-tionManagement启用了JPA的事务管理;@EnableJpaRepositories启用了JPA资源库并指定了上面定义的接口资源库的位置;@EntityScan指定了定义实体的位置,它将导入我们定义的实体。注意,在测试时使用的JPA配置类可能与这个配置略有不同,这个配置的一些配置参数是从配置文件中读取的,而测试时使用的配置类把一些配置参数都包含在类定义中了。

配置类JpaConfiguration.java

package springboot.example.config;

import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
* Created by zhang on 2018/2/10.
*/
@Order(Ordered.HIGHEST_PRECEDENCE)
@Configuration
@EnableTransactionManagement(proxyTargetClass=true)
@EnableJpaRepositories(basePackages="dbdemo.**.repository")
@EntityScan(basePackages="dbdemo.**.entity")
public class JpaConfiguration {
   @Bean
   PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor(){
       return new PersistenceExceptionTranslationPostProcessor();
   } 
}

  其次,在MySQL数据库服务器中创建一个数据库test,然后配置一个可以访问这个数据库的用户及其密码。数据库的表结构可以不用创建,在程序运行时将会按照实体的定义自动创建。如果还没有创建一个具有完全权限访问数据库test的用户,可以在连接MySQL服务器的查询窗口中执行下面指令,这个指令假设你将在本地中访问数据库。

数据源和JPA配置application.yml:

spring:
    datasource:
         url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8
         username: root
         password: root
    jpa:
         database: MYSQL
         show-sql: true
         #Hibernate ddl auto (validate|create|create-drop|update)         
         hibernate:
                ddl-auto: update             
         properties:
                hibernate:                 
                dialect: org.hibernate.dialect.MySQL5Dialect

  配置中将ddl-atuo设置为update,就是使用Hibernate来自动更新表结构的,即如果数据表不存在则创建,或者如果修改了表结构,在程序启动时则执行表结构的同步更新。

  最后,编写一个测试程序,测试程序首先初始化数据库,创建一个部门,命名为“开发部”,创建一个角色,命名为admin,创建一个用户,命名为user,同时将它的所属部门设定为上面创建的部门,并将现有的所有角色都分配给这个用户。然后使用分页的方式查询所有用户的列表,并从查到的用户列表中,打印出用户的名称、部门的名称和第一个角色的名称等信息。

测试程序代码清单:MysqlTest.java

import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.util.Assert;
import springboot.example.Repository.DepartmentRepository;
import springboot.example.Repository.RoleRepository;
import springboot.example.Repository.UserRepository;
import springboot.example.config.JpaConfiguration;
import springboot.example.domain.Department;
import springboot.example.domain.Role;
import springboot.example.domain.User;
import java.util.Date;
import java.util.List;

/**
 * Created by zhang on 2018/2/10.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={JpaConfiguration.class})
public class MysqlTest{
    private static Logger logger = Logger.getLogger(MysqlTest.class);
    @Autowired
    private UserRepository userRepository;
    @Autowired
    DepartmentRepository departmentRepository;
    @Autowired
    RoleRepository roleRepository;
    @Before public void initData(){
        userRepository.deleteAll();
        roleRepository.deleteAll();
        departmentRepository.deleteAll();
        Department department = new Department();
        department.setName(" 开发部 ");
        departmentRepository.save(department);
        Assert.notNull(department.getId());
        Role role = new Role();
        role.setName("admin");
        roleRepository.save(role);
        Assert.notNull(role.getId());
        User user = new User();
        user.setName("user");
        user.setCreateDate(new Date());
        user.setDepartment(department);
        List<Role> roles = roleRepository.findAll();
        Assert.notNull(roles);
        user.setRoles(roles);
        userRepository.save(user);
        Assert.notNull(user.getId());
    }

    @Test
    public void findPage(){
        Pageable pageable = new PageRequest(0, 10, new Sort(Sort.Direction.ASC, "id"));
        Page<User> page = userRepository.findAll(pageable);
        Assert.notNull(page);
        for(User user : page.getContent()) {
            logger.info(user);
        }
    }
}

相关文章

网友评论

      本文标题:(《深入实践Spring Boot》笔记2)在Spring Bo

      本文链接:https://www.haomeiwen.com/subject/milctftx.html