Need example of INNER JOIN for 2 tables – Entity and EntityCategory – for MVC architecture

huangapple go评论154阅读模式
英文:

Need example of INNER JOIN for 2 tables - Entity and EntityCategory - for MVC architecture

问题

我需要一个关于MVC架构的工作示例,使用Java Web应用程序的样例。所需技术仅限于:JDBC、JSP和Servlet,最好使用MySQL数据库。***我不需要Hibernate和Spring。***示例数据库可能仅包含两个表,如Users和UserRoles,或者(Products和ProductCategories等),这些表可以通过INNER JOIN连接起来。

尽管提供一个示例项目的链接将会非常有帮助,但我也希望您就以下问题提供建议:

  1. 对于示例项目,是使用class还是enum来表示UserRole?

  2. 在User类中是引用“private UserRole userRole;”还是“private int userRoleId;”?

  3. 作为“ListAllRecords”方法的示例,如何利用查询结果,该查询连接了usersuser_roles两个表:

    SELECT users.*, user_roles.name FROM users INNER JOIN user_roles ON user_role_id = user_roles.id;

以下是我正在努力理清思路的项目细节:

我的设计是:

Need example of INNER JOIN for 2 tables – Entity and EntityCategory – for MVC architecture

实体类:

package com.project.entity.temp;

public class User {
    private int id;
    private String name;
    private String password;
    private UserRole userRole;
}

public class UserRole {
    private int id;
    private String name;
}

SQL查询:

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `mydb` ;

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;

-- -----------------------------------------------------
-- Table mydb.user_roles
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`user_roles` ;

CREATE TABLE IF NOT EXISTS `mydb`.`user_roles` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `id_UNIQUE` (`id` ASC) VISIBLE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table mydb.users
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`users` ;

CREATE TABLE IF NOT EXISTS `mydb`.`users` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NOT NULL,
  `password` VARCHAR(45) NOT NULL,
  `user_role_id` INT NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `id_UNIQUE` (`id` ASC) VISIBLE,
  INDEX `fk_users_user_roles_idx` (`user_role_id` ASC) VISIBLE,
  CONSTRAINT `fk_users_user_roles`
    FOREIGN KEY (`user_role_id`)
    REFERENCES `mydb`.`user_roles` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;
英文:

I need a working example of the sample working Java web application for the MVC architecture. The technologies which are needed are only: JDBC with JSP and Servlet on, preferably MySQL. I don't need Hybernate and Spring. The sample DB may include only 2 tables like Users and UserRoles or (Products and ProductCategories, etc) which are joined on INNER JOIN.

Although a link to the sample project would be greatly appreciated, I'd also appreciate recommendations on what should I use:

  1. UserRole class or enum for the sample project?

  2. reference "private UserRole userRole;" or "private int userRoleId;" in User class?

  3. as an example of the ListAllRecords method, how to utilize results of the query which joins users and user_roles tables:

    SELECT users.*, user_roles.name FROM users INNER JOIN user_roles ON user_role_id = user_roles.id;

Here are details of my project which I'm trying to wrap my head around:

My design is:

Need example of INNER JOIN for 2 tables – Entity and EntityCategory – for MVC architecture

Entity classes:

package com.project.entity.temp;
public class User {
private int id;
private String name;
private String password;
private UserRole userRole;
}
public class UserRole {
private int id;
private String name;
}

SQL queries:

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `mydb` ;
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`user_roles`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`user_roles` ;
CREATE TABLE IF NOT EXISTS `mydb`.`user_roles` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `id_UNIQUE` (`id` ASC) VISIBLE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`users`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`users` ;
CREATE TABLE IF NOT EXISTS `mydb`.`users` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`password` VARCHAR(45) NOT NULL,
`user_role_id` INT NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `id_UNIQUE` (`id` ASC) VISIBLE,
INDEX `fk_users_user_roles_idx` (`user_role_id` ASC) VISIBLE,
CONSTRAINT `fk_users_user_roles`
FOREIGN KEY (`user_role_id`)
REFERENCES `mydb`.`user_roles` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

答案1

得分: 1

我遵循了这种方法,但我想知道是否这是实现目标的正确方法:

  1. 使用以下查询获取结果集
    SELECT users.*,user_roles.name FROM users INNER JOIN user_roles ON user_role_id = user_roles.id;
  2. 循环遍历记录并从 ResultSet 数据填充 UserRoles 对象
    user = new User();
userRole = new UserRole();
user.setId(resultSet.getInt("id"));
user.setName(resultSet.getString("name"));
user.setPassword(resultSet.getString("password"));
userRole.setId(resultSet.getInt("user_role_id"));
userRole.setName(resultSet.getString("user_roles.name"));
user.setUserRole(userRole);
  1. 从 JSP 表单获取数据时将使用隐藏字段。
英文:

I follow that approach, but was wondering whether it is the proper way to accomplish the goal:

  1. Got result set using this query
    SELECT users.*, user_roles.name FROM users INNER JOIN user_roles ON user_role_id = user_roles.id;
  2. Looped through the records and populated UserRoles object from the ResultSet data
    user = new User();
userRole = new UserRole();
user.setId(resultSet.getInt("id"));
user.setName(resultSet.getString("name"));
user.setPassword(resultSet.getString("password"));
userRole.setId(resultSet.getInt("user_role_id"));
userRole.setName(resultSet.getString("user_roles.name"));
user.setUserRole(userRole);
  1. When getting data from the JSP form will use the hidden fields.

答案2

得分: 0

尝试以下内容

Maven 依赖 -

> <dependency>   <groupId>javax.persistence</groupId>  
> <artifactId>javax.persistence-api</artifactId>  
> <version>2.2</version> </dependency>

或者

> <dependency>
>     <groupId>javax.persistence</groupId>
>     <artifactId>persistence-api</artifactId>
>     <version>1.0.2</version> </dependency>


package com.project.entity.temp;

import javax.persistence.Entity;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class User {
    @Id
    private int id;
    private String name;
    private String password;

    @OneToOne
    private UserRole userRole;
}


@Entity
public class UserRole {
    

    @Id 
    private int id;

    @OneToOne
    private User user; 

    private String name;
}
英文:

Try below

Maven dependency -

> <dependency> <groupId>javax.persistence</groupId>
> <artifactId>javax.persistence-api</artifactId>
> <version>2.2</version> </dependency>

Or

> <dependency>
> <groupId>javax.persistence</groupId>
> <artifactId>persistence-api</artifactId>
> <version>1.0.2</version> </dependency>

 package com.project.entity.temp;
import javax.persistence.Entity;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class User {
@Id
private int id;
private String name;
private String password;
@OneToOne
private UserRole userRole;
}
@Entity
public class UserRole {
@Id 
private int id;
@OneToOne
private User user; 
private String name;
}

huangapple
  • 本文由 发表于 2020年10月27日 02:56:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64543332.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定