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"; } }

相关推荐
jessecyj10 分钟前
SpringBoot详解
java·spring boot·后端
Flittly11 分钟前
【SpringAIAlibaba新手村系列】(2)Ollama 本地大模型调用
java·ai·springboot
_MyFavorite_13 分钟前
JAVA重点基础、进阶知识及易错点总结(10)Map 接口(HashMap、LinkedHashMap、TreeMap)
java·开发语言
qqty121719 分钟前
Spring Boot管理用户数据
java·spring boot·后端
Flittly23 分钟前
【SpringAIAlibaba新手村系列】(1)初识 Spring AI Alibaba 框架
java·spring
知识分享小能手30 分钟前
MongoDB入门学习教程,从入门到精通,MongoDB查询(4)
数据库·学习·mongodb
LSL666_1 小时前
MybatisPlus条件构造器(上)
java·数据库·mysql·mybatisplus
U-52184F691 小时前
深入理解“隐式共享”与“写时复制”:从性能魔法到内存深坑
java·数据库·算法
bearpping1 小时前
SpringBoot最佳实践之 - 使用AOP记录操作日志
java·spring boot·后端
一叶飘零_sweeeet1 小时前
线上故障零扩散:全链路监控、智能告警与应急响应 SOP 完整落地指南
java·后端·spring