Mybatis+Struts2的结合:实现用户插入和查找(2)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib prefix="s" uri="/struts-tags"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body bgcolor="#33CC99">
<h3>用户列表</h3></body>
    <table>
        <tr>
            <td>ID</td>
            <td>用户名</td>
            <td>密码</td>
        </tr>
       
        <s:iterator value="users" >   
            <tr>
                <td> <s:property value="id" />        </td>
                <td> <s:property value="username" />  </td> 
                <td> <s:property value="password" />  </td>
            </tr>
        </s:iterator> 
    </table>

</html>

5. struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>   
    <constant value="true" />
    <constant value="true" />
   
    <package namespace="/user" extends="struts-default">
        <action >
            <result>
            /user_add_success.jsp
            </result>
           
            <result>
            /user_get.jsp?users=${users}
            </result>
        </action>
    </package>
</struts>

6. conf.xml

<?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>
            <transactionManager type="JDBC" />
                <dataSource type="POOLED">
                    <property value="com.mysql.jdbc.Driver" />
                    <property value="jdbc:mysql://localhost:3306/mybatis" />
                    <property value="root" />
                    <property value="000000" />
                </dataSource>
        </environment>
    </environments>
    <mappers>
            <mapper resource="com/hjj/dao/userMapper.xml"/>
    </mappers>
</configuration>

7. userMapper.xml

<?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="com.hjj.dao.userMapper">
        <select  resultType="com.hjj.model.User">
                select * from user;
        </select>
       
        <insert  parameterType="com.hjj.model.User">
                insert into user (username,password) values (#{username},#{password});
        </insert>
</mapper>

8 .User.java

package com.hjj.model;

public class User {
   
    private int id;
    private String username;
    private String password;
   
    public User(int id, String username, String password) {
        super();
        this.id = id;
        this.username = username;
        this.password = password;
    }
    public User() {
        super();
        // TODO Auto-generated constructor stub
    }
   
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
   
    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
    }
}

9. UserAction.java

package com.hjj.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.hjj.dao.MysqlDAO;
import com.hjj.model.*;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/a8465fed53287ae1bac0f86fdfd64687.html