胸腺胸骨不做对象的打印列表(Spring JPA)

发布于 2025-01-24 22:34:52 字数 1505 浏览 4 评论 0原文

我有5个不同的课堂游览对象。我希望我的主页显示了带有其字段的对象。 写了conrtoller:

        @Controller
        public class MainController {
            @Autowired
            private TourRepository tourRepository;
        
            @GetMapping("/")
            public String mainPage(Model model) {    
               
            List<Tour> tourList=tourRepository.findAll();    
            model.addAttribute("tours",tourList);
        
            return "home/main";
            }    

            @RequestMapping("/main")
            public String main() {
       
            return "redirect:/";}
    }

repository

public interface TourRepository extends JpaRepository<Tour,Integer> {  
}

应用程序。

# hibernate configurations
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update


# thumeleaf configurations
spring.thymeleaf.mode= HTML
spring.thymeleaf.cache=false

<div>

    <table>
        <thead>
            <tr>
                <th>Title</th>
                <th>Price</th>
            </tr>
        </thead>
        <tbody>
            <tr th:each ="tour : ${tours}">
                <td th:utext="${tour.title}">...</td>
                <td th:utext="${tour.price}">...</td>
            </tr>
        </tbody>
    </table>
</div>

和 如何更改控制器\ view?

I have 5 different object of class Tour with some fields.I want that my main page shows this object with their fields.
I wrote conrtoller:

        @Controller
        public class MainController {
            @Autowired
            private TourRepository tourRepository;
        
            @GetMapping("/")
            public String mainPage(Model model) {    
               
            List<Tour> tourList=tourRepository.findAll();    
            model.addAttribute("tours",tourList);
        
            return "home/main";
            }    

            @RequestMapping("/main")
            public String main() {
       
            return "redirect:/";}
    }

And Repository

public interface TourRepository extends JpaRepository<Tour,Integer> {  
}

application.properties:

# hibernate configurations
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update


# thumeleaf configurations
spring.thymeleaf.mode= HTML
spring.thymeleaf.cache=false

Now I'm trying to pass variable values to the page with Thymeleaf,

<div>

    <table>
        <thead>
            <tr>
                <th>Title</th>
                <th>Price</th>
            </tr>
        </thead>
        <tbody>
            <tr th:each ="tour : ${tours}">
                <td th:utext="${tour.title}">...</td>
                <td th:utext="${tour.price}">...</td>
            </tr>
        </tbody>
    </table>
</div>

but I get five identical duplicates of first record in db.
How can I change controller\view?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺渁∝ 2025-01-31 22:34:52

我的主要键在DB中的实体Tout中名为“代码”。注释@id没有用不同名称作为主键的列。我明确添加了:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "code")
private int id;

My primary key named "code" in entity tout in db. Annotation @Id didn't a column with a different name as a primary key. I explicitly added:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "code")
private int id;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文