在MySQL数据库中正确创建并保留

时间:2017-10-03 13:49:07

标签: mysql spring jpa spring-data spring-data-jpa

我使用Spring启动应用程序,这是我工作的实体类,

@Entity
public class User {

    // form:hidden - hidden value
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    // form:input - textbox
    @Column(name = "name", columnDefinition = "VARCHAR(30)", nullable = false)
    String name;

    // form:input - textbox
    @Column(name = "email", columnDefinition = "VARCHAR(50)", nullable = false)
    String email;

    // form:input - password
    @Column(name = "password", columnDefinition = "VARCHAR(20)", nullable = false)
    String password;

    // form:textarea - textarea
    @Column(name = "address", columnDefinition = "VARCHAR(255)")
    String address;

    // form:input - password
    String confirmPassword;

    // form:checkbox - single checkbox
    @Column(name = "newsletter")
    boolean newsletter;

    // form:radiobutton - radio button
    @Column(name = "sex", columnDefinition = "VARCHAR(1)")
    String sex;

    // form:radiobuttons - radio button
    @Column(name = "number")
    Integer number;

    // form:select - form:option - dropdown - single select
    @Column(name = "country", columnDefinition = "VARCHAR(10)")
    String country;

    // form:checkboxes - multiple checkboxes
    @ElementCollection
    @NotNull
    List<String> framework;

    // form:select - multiple=true - dropdown - multiple select
    @ElementCollection
    List<String> skill;
}

我打算只在user表中创建以下列,

  id, 
  name,
  email,
  address,
  password,
  newsletter,
  framework,
  sex,
  Number,
  Country,
  Skill

例如,相应的SQL就像(虽然我不想明确写),

CREATE TABLE user (

  id LONG GENERATED BY DEFAULT AS IDENTITY(START WITH 1, INCREMENT BY 1) PRIMARY KEY,
  name VARCHAR(30),
  email  VARCHAR(50),
  address VARCHAR(255),
  password VARCHAR(20),
  newsletter BOOLEAN,
  framework VARCHAR(500),
  sex VARCHAR(1),
  Number INTEGER,
  Country VARCHAR(10),
  Skill VARCHAR(500)
);

启动控制器类

@Controller
public class UserController {

    private final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;


    private static List<User> populateDefaultUserValues() {

        List<User> users = new ArrayList<>();

        User user = new User();
        user.setName("Ella");
        user.setEmail("xyz@gmail.com");
        user.setPassword("df32d343H");
        user.setFramework(Arrays.asList("Spring MVC, GWT".split("\\s*,\\s*")));
        users.add(user);


        user = new User();
        user.setName("Alex");
        user.setEmail("alex@hotmail.com");
        user.setPassword("12HH2d343H");
        user.setFramework(Arrays.asList("Spring MVC, GWT".split("\\s*,\\s*")));
        users.add(user);


        user = new User();
        user.setName("Romanna");
        user.setEmail("romanna@hotmail.com");
        user.setPassword("Rommann343");
        user.setFramework(Arrays.asList("Spring MVC, GWT".split("\\s*,\\s*")));
        users.add(user);

        return users;
    }

    @GetMapping(value = "/")
    public String index() {
        return "redirect:/users";
    }

    @GetMapping(value = "/users")
    public String showAllUsers(Model model) {

        List<User> users = populateDefaultUserValues();

        users.forEach(user -> {
            userService.save(user);
        });

        model.addAttribute("users", userService.findAll());
        return "list";
    }
}

当我运行应用程序时,我发现这是在MYSQL数据库中创建的,

enter image description here

如何省略confirmed_password字段并添加skillframework的列?我希望它们是VARCHAR

1 个答案:

答案 0 :(得分:2)

  

如何省略confirmed_pa​​ssword字段

您应该使用@Transient注释。

  

添加技能和框架的列?

由于您已将其标记为@ElementCollection - user表中没有列,因为您无法在单个关系属性中保留一组值(它会违反1NF限制)。还有两个名为user_frameworkuser_skill的附加表,它们与user表和user_id列相关。

您可以阅读有关集合的Java Persistense wikiHibernate documentation,以找到正确使用的不同示例。虽然,您的代码很好,但在通常情况下,您不必担心数据库表,关系和数据一致性 - ORM框架(默认情况下为Hibernate)将为您处理所有这些事情。