login.jsp
<a href="list.action">登陸</a>
userlist.jsp
<div>
<table>
<tr>
<th width="10%">編號</th>
<th width="15%">姓名</th>
<th width="15%">年齡</th>
<th width="15%">性別</th>
<th width="15">出生日期</th>
<th width="15%">修改</th>
<th width="15%">刪除</th>
</tr>
<c:forEach items="${allUser }" var="user">
<tr>
<td width="10%">${user.mid }</td>
<td width="15%">${user.name }</td>
<td width="15%">${user.age }</td>
<td width="15%">${user.sex }</td>
<td width="15%">${user.birthday }</td>
<td width="15%"><a href="">修改</a></td>
<td width="15%"><a href="">刪除</a></td>
</tr>
</c:forEach>
</table>
</div>
@Controller
@Scope(scopeName="prototype")
@RequestMapping("/pages/test/*")
public class UserAction {
@Resource
private IUserService userService;
@RequestMapping("list")
public ModelAndView list(ModelAndView mav) {
List<User> allUser = new ArrayList();
allUser = userService.findAll();
mav.addObject("allUser",allUser);
mav.setViewName("userlist");
return mav;
}
@RequestMapping("list2")
public String list2(Model model) {
List<User> allUser = new ArrayList();
allUser = userService.findAll();
model.addAttribute("allUser", allUser);
return "userlist";
}
}
public interface IUserService {
public List<User> findAll();
}
@Service
public class UserServiceImpl implements IUserService {
@Resource
private IUserDAO userDAO;
@Override
public List<User> findAll() {
return userDAO.findAll();
}
}
public interface IUserDAO {
public List<User> findAll();
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.mldn.dao.IUserDAO">
<resultMap type="User" id="UserMap">
<id column="mid" property="mid"/>
<result column="big_name" property="name"/>
<result column="age" property="age"/>
<result column="sex" property="sex"/>
<result column="birthday" property="birthday"/>
</resultMap>
<select id="findAll" resultMap="UserMap">
select mid,big_name,age,sex,birthday from users
</select>
</mapper>
private String mid ;
private String name ;
private Integer age ;
private String sex ;
private Date birthday ;
CREATE TABLE users(
mid VARCHAR(50) ,
big_name VARCHAR(50) ,
age INT ,
sex VARCHAR(20) ,
birthday DATETIME ,
CONSTRAINT PK_mid PRIMARY KEY(mid)
);