SSM搭建测试

resources下创建mapper

1.AccountDao.xml

<mapper namespace="com.qcby.dao.AccountDao"> <select id="findAll" resultType="com.qcby.model.Account"> select * from account; </select> </mapper>

2.AccountDao接口

public interface AccountDao { public List<Account> findAll(); }

3.AccountServiceImpl

package com.qcby.service.servicelmpl; import com.qcby.dao.AccountDao; import com.qcby.model.Account; import com.qcby.service.AccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class AccountServicelmpl implements AccountService { @Autowired private AccountDao accountDao;//注入Dao层 //查询所有 @Override public List<Account> findAll() { System.out.println("业务层:查询所有"); return this.accountDao.findAll();//返回查询所有 } }

4.AccountController

加入for循环打印数据

package com.qcby.controller; import com.qcby.model.Account; import com.qcby.service.AccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import java.util.List; @Controller public class AccountController { //依赖注入 @Autowired private AccountService accountService; /** * 处理超链接发送出来的请求 * @param model * @return */ @RequestMapping(path = "/hello") public String sayHello(Model model){ System.out.println("入门方法执行了2..."); List<Account> accounts = accountService.findAll(); // 向模型中添加属性msg与值,可以在html页面中取出并渲染 /*加入for循环打印数据*/ for (Account account: accounts) { System.out.println(account.toString()); } model.addAttribute("msg","hello,SpringMVC"); // 配置了视图解析器后,写法 return "suc"; } }

相关推荐
java1234_小锋4 分钟前
Spring AI 2.0 开发Java Agent智能体 - 结构化输出
java·人工智能·spring
asdfg12589635 分钟前
Java 大型项目设计的“内功心法”---面向对象和接口编程
java·开发语言
ch.ju6 分钟前
Java programming Chapter Three——Array
java·开发语言
努力努力再努力wz19 分钟前
【Qt入门系列】第一个 Qt Widgets 程序:项目创建、UI 文件、Hello World、对象树与 qDebug 日志
java·c语言·开发语言·数据结构·c++·qt·ui
东南门吹雪29 分钟前
Spring事务传播机制深度解析
java·数据库·spring
不甘先生30 分钟前
PostgreSQL 中的 JSONB 详解:从入门到实战
数据库·postgresql
XS03010632 分钟前
Java基础 List集合
java·windows·list
凤凰院凶涛QAQ32 分钟前
《C++转Java快速入手系列》抽象类和接口篇
java·开发语言·c++
Irene199135 分钟前
PL/SQL:异常处理补充
数据库·sql