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

相关推荐
步行cgn7 分钟前
Spring Bean 的生命周期详解
java·后端·spring
ps酷教程15 分钟前
数据权限DataPersmission
java·学习
刘天远19 分钟前
Agent系统接入编排:评分模型、状态机与Python门禁
数据库·人工智能·python
驭渊的小故事28 分钟前
Java 项目-jckson序列化工具
java·开发语言
君顾128 分钟前
上海24小时自助健身房系统开发实战指南:从架构设计到功能实现
java·开发语言·健身房
DBA_G36 分钟前
GBase数据库安全“全牌照“技术解读:从等保四级到全密态计算的实践
数据库
用户37215742613537 分钟前
Java 拆分 Word 文档教程:按页、分页符和分节符拆分
java
liyinchi19881 小时前
微信小程序支付遇到“由于小程序违规,支付功能暂时无法使用” 解决办法
java·微信小程序·go
横木沉1 小时前
IntelliJ IDEA 无法识别 Git:Git is not installed 问题解决
java·git·github·intellij-idea