Developer
Developer

Reputation: 45

Insert images in database

I'm making a real estate app in spring boot - thymeleaf and I really need to ask you how to store images. I was thinking about making a table just for images and anotate it manyToOne, and insert it into Property entity because one property can have 10,15,20 images and that is the part I know how to do.

But I'm not sure how to handle the rest of the code. How to make file upload in the /add method? And also howadd and show them in thymeleaf?

This is my /properties/add method without the option of inserting images:

    @RequestMapping("/add")
    public String add(Model theModel) {
        Property theProperty = new Property();

        User theUser = new User();
        theProperty.setUser(theUser);
        List<User> userList = userService.findAll();

        Address theAddress = new Address();
        theProperty.setAddress(theAddress);
        List<Address> addressList = addressService.findAll();

        theModel.addAttribute("property", theProperty);
        theModel.addAttribute("user", userList);
        theModel.addAttribute("address", addressList);

        return "properties/property-form";
    }

I don't expect you to type it but maybe somebody know the most similar project

Upvotes: 0

Views: 482

Answers (1)

Fabo137
Fabo137

Reputation: 50

I don't think you wanna store the images directly to the database but having a table as a reference to the image itself.

You can do it the same way as you are doing with the userList.

theProperty.addAttribute("images", imageList);

Upvotes: 1

Related Questions