Utils工具类
package util;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MBatisUtils {
private static SqlSessionFactory fac;
static {
try {
Reader rede=Resources.getResourceAsReader("mybatis.xml");
fac=new SqlSessionFactoryBuilder().build(rede);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static SqlSession getSession(){
return fac.openSession();
}
}
user实体类
package entity;
import java.util.Date;
public class Myuser {
private Integer id;
private String username;
private String userpwd;
private String nickname;
private Integer age;
private Date birthday;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpwd() {
return userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
UserMaping
<?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="nsUser">
<select id="queryAll" resultType="entity.Myuser">
select id,username,userpwd,nickname,age,birthday from myuser
</select>
<select id="queryById" parameterType="int" resultType="entity.Myuser">
select id,username,userpwd,nickname,age,birthday from myuser where id=#{id}
</select>
</mapper>
全局配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="username" value="system" />
<property name="password" value="password" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="entity/UserMapper.xml"/>
</mappers>
</configuration>
UserDao
package Dao;
import java.util.List;
import util.MBatisUtils;
import entity.Myuser;
public class UserDao {
/*
* 查询所有
*/
public List<Myuser> getAllList() {
return MBatisUtils.getSession().selectList("nsUser.queryAll");
}
/*
* 根据用户名查找详细信息
*/
public Myuser getMyuser(int id) {
return MBatisUtils.getSession().selectOne("nsUser.queryById",id);
}
}
Jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>显示所有用户信息</title>
</head>
<body>
<table align="center" width="80%" border="1" >
<caption>用户信息列表</caption>
<tr>
<th>ID</th>
<th>USENAME</th>
<th>USERPWD</th>
<th>NICKNAME</th>
<th>AGE</th>
<th>BIRTHDAY</th>
</tr>
<c:forEach var="u" items="${userlist}" varStatus="status">
<tr<c:if test="${status.index%2==1 }">style="background-color:green;"</c:if>>
<th>${u.id }</th>
<th>${u.username }</th>
<th>${u.userpwd}</th>
<th><a href="${pageContext.request.contextPath }/Servlet?action=ShowDetial&id=${u.id}">${u.nickname }</a> </th>
<th>${u.age }</th>
<th> <fmt:formatDate value="${u.birthday}" pattern="yyyy-MM-dd"/></th>
</tr>
</c:forEach>
</table>
</body>
</html>
showdetil
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>显示所有用户信息</title>
</head>
<body>
<table align="center" width="80%" border="1" >
<caption>用户信息列表</caption>
<tr>
<th>ID</th>
<th>USENAME</th>
<th>USERPWD</th>
<th>NICKNAME</th>
<th>AGE</th>
<th>BIRTHDAY</th>
</tr>
<c:forEach var="u" items="${userlist}" varStatus="status">
<tr<c:if test="${status.index%2==1 }">style="background-color:green;"</c:if>>
<th>${u.id }</th>
<th>${u.username }</th>
<th>${u.userpwd}</th>
<th><a href="${pageContext.request.contextPath }/Servlet?action=ShowDetial&id=${u.id}">${u.nickname }</a> </th>
<th>${u.age }</th>
<th> <fmt:formatDate value="${u.birthday}" pattern="yyyy-MM-dd"/></th>
</tr>
</c:forEach>
</table>
</body>
</html>