zoukankan      html  css  js  c++  java
  • JPA 系列教程18-自动把firstName+lastName合并为name字段

    需求

    设计的国际化网站,页面需要输入firstName,lastName,后台数据库只需要存储name属性。
    页面获取的firstName,lastName持久化到数据库name属性,规则按照,分隔保存。
    防止错误修改,模型里面的name属性不提供getter,setter方法。

    ddl语句

    CREATE TABLE `t_user` (
      `id` bigint(20) NOT NULL AUTO_INCREMENT,
      `birthday` datetime DEFAULT NULL,
      `name` varchar(255) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

    User

    package com.jege.jpa.primary;
    
    import java.util.Date;
    
    import javax.persistence.Access;
    import javax.persistence.AccessType;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.PostLoad;
    import javax.persistence.PrePersist;
    import javax.persistence.PreUpdate;
    import javax.persistence.Table;
    import javax.persistence.Transient;
    
    /**
     * @author JE哥
     * @email 1272434821@qq.com
     * @description:自动把firstName+lastName合并为name
     */
    @Entity
    @Table(name = "t_user")
    @Access(AccessType.FIELD)
    public class User {
      @Id
      @GeneratedValue
      private Long id;
      @Column(name = "name", unique = true)
      private String name;
      private Date birthday;
      @Transient
      private String firstName;
      @Transient
      private String lastName;
    
      @PostLoad
      private void load() {
        if (name != null) {
          String[] names = name.split(",");
          firstName = names[0];
          lastName = names[1];
        }
      }
    
      @PrePersist
      @PreUpdate
      private void save() {
        if (firstName != null && !"".equals(firstName)) {
          name = firstName + ",";
        }
        if (lastName != null && !"".equals(lastName)) {
          name += lastName;
        }
      }
    
      public Long getId() {
        return id;
      }
    
      public void setId(Long id) {
        this.id = id;
      }
    
      public Date getBirthday() {
        return birthday;
      }
    
      public void setBirthday(Date birthday) {
        this.birthday = birthday;
      }
    
      public String getFirstName() {
        return firstName;
      }
    
      public void setFirstName(String firstName) {
        this.firstName = firstName;
      }
    
      public String getLastName() {
        return lastName;
      }
    
      public void setLastName(String lastName) {
        this.lastName = lastName;
      }
    
      @Override
      public String toString() {
        return "User [id=" + id + ", name=" + name + ", birthday=" + birthday + ", firstName=" + firstName + ", lastName="
        + lastName + "]";
      }
    
    }
    

    MainTest

    package com.jege.jpa.primary;
    
    import java.util.Date;
    
    import javax.persistence.EntityManager;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.Persistence;
    
    import org.junit.After;
    import org.junit.AfterClass;
    import org.junit.Before;
    import org.junit.BeforeClass;
    import org.junit.Test;
    
    /**
     * @author JE哥
     * @entityManagerail 1272434821@qq.com
     * @description:自动把firstName+lastName合并为name测试
     */
    public class MainTest {
      private static EntityManagerFactory entityManagerFactory = null;
      private EntityManager entityManager = null;
    
      @BeforeClass
      public static void setUpBeforeClass() throws Exception {
        entityManagerFactory = Persistence.createEntityManagerFactory("com.jege.jpa");
      }
    
      @Before
      public void setUp() throws Exception {
        entityManager = entityManagerFactory.createEntityManager();
      }
    
      @Test
      public void persist() throws Exception {
        User user = new User();
        user.setBirthday(new Date());
        user.setFirstName("西门");
        user.setLastName("吹雪");
    
        entityManager.getTransaction().begin();
        entityManager.persist(user);
        entityManager.getTransaction().commit();
      }
    
      @After
      public void tearDown() throws Exception {
        if (entityManager != null && entityManager.isOpen()) {
          entityManager.close();
        }
      }
    
      @AfterClass
      public static void tearDownAfterClass() throws Exception {
        if (entityManagerFactory != null && entityManagerFactory.isOpen()) {
          entityManagerFactory.close();
        }
      }
    }
    

    源码地址

    https://github.com/je-ge/jpa

    如果觉得我的文章对您有帮助,请打赏支持。您的支持将鼓励我继续创作!谢谢!
    微信打赏
    支付宝打赏

  • 相关阅读:
    jquery ui draggable,droppable 学习总结
    VSCode设置网页代码实时预览
    ionic3-修改APP应用图标(icon)和APP启动界面(Splash)
    Ionic3页面的生命周期
    videogular2 在ionic3项目里报错(rxjs_1.fromEvent is not a function)
    IDEA的maven项目的netty包的导入(其他jar同)
    maven的安装与项目的创建
    给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
    使用二分法查询二维整型数组的值(找到返回其坐标)
    乐观锁以及悲观锁
  • 原文地址:https://www.cnblogs.com/je-ge/p/6243276.html
Copyright © 2011-2022 走看看