【汇智学堂】-SSM整合入门(根据条件查询记录-2)
作者:互联网
index.jsp
<%--
Created by IntelliJ IDEA.
User: soft
Date: 2019/7/10
Time: 8:14
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>SSM框架整合入门-查询</title>
</head>
<body>
查询数据库一条记录的测试
</body>
</html>
input.jsp
<%--
Created by IntelliJ IDEA.
User: soft
Date: 2019/7/13
Time: 13:31
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>查询测试</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/select" method="post">
输入用户名:<input type="text" name="uname"/><br>
<input type="submit" value="提交" />
</form>
</body>
</html>
userList.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: soft
Date: 2019/7/13
Time: 13:54
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
用户信息<br>
<c:forEach items="${userList}" var="user">
${user.uid}
${user.uname}
${user.usex} <br>
</c:forEach>
</body>
</html>
MyUser类
package com.po;
public class MyUser {
private Integer uid;
private String uname;
private String usex;
set get略
@Override
public String toString(){//为了方便查看结果,重写了toString方法
return "User [uid="+uid+",uname="+uname+",usex="+usex+"]";
}
}
UserController
package com.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.po.MapUser;
import com.po.SelectUserParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.dao.UserDao;
import com.po.MyUser;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
@Autowired
private UserDao userDao;
@RequestMapping("/select")
public String select(MyUser user, Model model){
List<MyUser> list=userDao.selectUserByUname(user);
model.addAttribute("userList",list);
return "userList";
}
}
UserDao
package com.dao;
import com.po.MapUser;
import com.po.MyUser;
import com.po.SelectUserParam;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
@Repository("userDao")//Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的开发。@Repository注解便属于最先引入的一批,它用于将数据访问层 (DAO 层 ) 的类标识为 Spring Bean。具体只需将该注解标注在 DAO类上即可。同时,为了让 Spring 能够扫描类路径中的类并识别出 @Repository 注解,需要在 XML 配置文件中启用Bean 的自动扫描功能
@Mapper
/*
使用Spring自动扫描MyBatis的接口并装配
*/
public interface UserDao {
/*
接口方法对应SQL映射文件UserMapper.xml中的ID
*/
public MyUser selectUserById(Integer uid);
/*
使用Map的查询方法
*/
/* public List<MyUser> selectAllUser(Map<String,Object> param);*/
/*
直接查询
*/
/*public List<MyUser> selectAllUser();*/
/*
使用JavaBean查询
*/
public List<MyUser> selectAllUser(SelectUserParam param);
public int addUser(MyUser user);
public int updateUser(MyUser user);
public int deleteUser(Integer integer);
public List<MapUser> selectResultMap();
public List<MyUser> selectUserByUname(MyUser user);
}
标签:入门,MyUser,List,汇智,SSM,nbsp,import,com,public 来源: https://blog.csdn.net/weixin_39593940/article/details/95774478