Mybatis WEB版本

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>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值