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