1、创建userinfo.sql数据库脚本
1 create table userinfo 2 (id number(4), 3 name varchar2(50), 4 password varchar2(20), 5 telephone varchar2(15), 6 isadmin varchar2(5)); 7 8 --4.2 用户表序列 9 create sequence seq_userinfo; 10 alter table userinfo add constraint pk_userinfo_id primary key(id); 11 12 insert into userinfo values(seq_userinfo.nextval,‘holly‘,‘123‘,‘134518024 13 ‘,‘是‘); 14 commit;
userinfo.sql
2、创建如下项目结构
3、在src的com.bean包下创建UserInfo.java
1 package com.bean; 2 /** 3 * 用户信息表 4 * @author Holly老师 5 * 6 */ 7 public class UserInfo { 8 private Integer id;//编号 9 private String name; //姓名 10 private String password; //密码 11 private String telephone; //电话 12 private String isadmin; //是否是管理员 13 14 public UserInfo() { 15 16 } 17 public UserInfo(Integer id, String name, String password, String telephone, 18 String isadmin) { 19 this.id = id; 20 this.name = name; 21 this.password = password; 22 this.telephone = telephone; 23 this.isadmin = isadmin; 24 } 25 public Integer getId() { 26 return id; 27 } 28 public void setId(Integer id) { 29 this.id = id; 30 } 31 public String getName() { 32 return name; 33 } 34 public void setName(String name) { 35 this.name = name; 36 } 37 public String getPassword() { 38 return password; 39 } 40 public void setPassword(String password) { 41 this.password = password; 42 } 43 public String getTelephone() { 44 return telephone; 45 } 46 public void setTelephone(String telephone) { 47 this.telephone = telephone; 48 } 49 public String getIsadmin() { 50 return isadmin; 51 } 52 public void setIsadmin(String isadmin) { 53 this.isadmin = isadmin; 54 } 55 @Override 56 public String toString() { 57 return "UserInfo [id=" + id + ", isadmin=" + isadmin + ", name=" + name 58 + ", password=" + password + ", telephone=" + telephone + "]"; 59 } 60 61 62 }
UserInfo.java
4、在src的com.dao包下创建UserInfoDao.java
1 package com.dao; 2 3 import java.util.List; 4 5 import com.bean.UserInfo; 6 /** 7 * mybatis映射文件的dao接口 8 * @author Holly老师 9 * 10 */ 11 public interface UserInfoDao { 12 /** 13 * 1.根据对象去查询对象 14 * @return 15 */ 16 public UserInfo findUser(UserInfo userinfo); 17 /** 18 * 2.查询所有 19 * @return 20 */ 21 public List<UserInfo> findAll(); 22 /** 23 * 3.根据id查询 24 */ 25 public UserInfo findById(int id); 26 /** 27 * 4.保存对象 28 * @param userinfo 29 */ 30 public void saveObj(UserInfo userinfo); 31 /** 32 * 5.修改对象 33 */ 34 public void updateObj(UserInfo userinfo); 35 /** 36 * 6.删除对象 37 * @param id 38 */ 39 public void deleteObj(int id); 40 41 42 43 }
UserInfoDao.java
5、在src的com.dao包下创建UserInfoDao.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 <mapper namespace="com.dao.UserInfoDao"> 4 <!-- 5 <resultMap type="userinfo" id="userMap"> 6 <id property="id" column="id"/> 7 <result property="name" column="name"/> 8 <result property="password" column="password"/> 9 <result property="telephone" column="telephone"/> 10 <result property="isadmin" column="isadmin"/> 11 </resultMap> 12 --> 13 <!--1.根据对象查询对象 --> 14 <select id="findUser" parameterType="userinfo" resultType="userinfo"> 15 select * from userinfo where name=#{name} and password=#{password} 16 </select> 17 18 <!-- 2.查询所有 --> 19 <select id="findAll" resultType="userinfo"> 20 select * from userinfo 21 </select> 22 23 <!-- 3.根据id查询 --> 24 <select id="findById" parameterType="int" resultType="userinfo"> 25 select * from userinfo where id=#{id} 26 </select> 27 28 <!-- 4.根据id删除 --> 29 <delete id="deleteObj" parameterType="int"> 30 delete from userinfo where id=#{id} 31 </delete> 32 33 <!-- 5.修改 --> 34 <update id="updateObj" parameterType="userinfo"> 35 update userinfo set name=#{name},password=#{password},telephone=#{telephone},isadmin=#{isadmin} where id=#{id} 36 </update> 37 38 <!-- 6.保存 --> 39 <insert id="saveObj" parameterType="userinfo"> 40 insert into userinfo values(seq_userinfo.nextval,#{name},#{password},#{telephone},#{isadmin}) 41 </insert> 42 43 44 </mapper>
UserInfoDao.xml
6、在src下创建mybatis-config.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" > 3 <configuration> 4 <!-- mybatis映射文件的别名配置 --> 5 <typeAliases> 6 <typeAlias type="com.bean.UserInfo" alias="userinfo"/> 7 </typeAliases> 8 9 <!-- 注册mybatis的映射文件 --> 10 <mappers> 11 <mapper resource="com/dao/UserInfoDao.xml"/> 12 </mappers> 13 </configuration>
mybatis-config.xml
7、在src下com.action包下创建UserInfoAction.java
1 package com.action; 2 3 import java.util.List; 4 5 import javax.servlet.http.HttpServletRequest; 6 7 import org.apache.struts2.ServletActionContext; 8 9 import com.bean.UserInfo; 10 import com.dao.UserInfoDao; 11 import com.opensymphony.xwork2.ActionSupport; 12 /** 13 * 14 * @author pc 15 * ActionSupport可以最数据校验 16 */ 17 public class UserInfoAction extends ActionSupport { 18 //action中引用实体类和页面上一一对应 19 //表单name属性值里的对象 20 private UserInfo user; 21 22 23 /*注入dao*/ 24 private UserInfoDao dao; 25 /** 26 * 1.登录 27 * @return 28 */ 29 public String login(){ 30 System.out.println("action:"+user.getName()); 31 UserInfo userinfo=dao.findUser(user); 32 if(userinfo!=null){ 33 System.out.println("action查询"); 34 return "success"; 35 }else{ 36 return "error"; 37 } 38 } 39 /** 40 * 2.查询所有 41 * @return 42 */ 43 public String findAll(){ 44 List<UserInfo> list=dao.findAll(); 45 if(list!=null){ 46 System.out.println("action查询所有"); 47 HttpServletRequest request=ServletActionContext.getRequest(); 48 request.setAttribute("list", list); 49 return "success"; 50 }else{ 51 return "error"; 52 } 53 } 54 /** 55 * 3.根据id删除 56 * @return 57 */ 58 public String deleteObj(){ 59 System.out.println("id:"+user.getId()); 60 try { 61 dao.deleteObj(user.getId()); 62 System.out.println("删除成功"); 63 return "success"; 64 } catch (Exception e) { 65 System.out.println("删除失败"); 66 e.printStackTrace(); 67 return "error"; 68 } 69 } 70 71 /** 72 * 4.根据id查询 73 * @return 74 */ 75 public String findById(){ 76 System.out.println("id:"+user.getId()); 77 UserInfo userinfo=dao.findById(user.getId()); 78 if(userinfo!=null){ 79 System.out.println("根据id查询到"); 80 HttpServletRequest request=ServletActionContext.getRequest(); 81 request.setAttribute("uv", userinfo); 82 return "success"; 83 }else{ 84 return "error"; 85 } 86 } 87 /** 88 * 5.修改 89 * @return 90 */ 91 public String updateObj(){ 92 System.out.println("action:"+user); 93 try { 94 dao.updateObj(user); 95 System.out.println("修改成功"); 96 return "success"; 97 } catch (Exception e) { 98 System.out.println("修改失败"); 99 e.printStackTrace(); 100 return "error"; 101 } 102 } 103 /** 104 * 6.添加 105 * @return 106 */ 107 public String saveObj(){ 108 System.out.println("action:"+user); 109 try { 110 dao.saveObj(user); 111 System.out.println("save success"); 112 return "success"; 113 } catch (Exception e) { 114 System.out.println("save fail"); 115 e.printStackTrace(); 116 return "error"; 117 } 118 } 119 120 public UserInfo getUser() { 121 return user; 122 } 123 124 public void setUser(UserInfo user) { 125 this.user = user; 126 } 127 128 public UserInfoDao getDao() { 129 return dao; 130 } 131 public void setDao(UserInfoDao dao) { 132 this.dao = dao; 133 } 134 }
UserInfoAction.java
8、在src下创建applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> 3 <!-- 1.配置数据源 --> 4 <bean id="oracleDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 5 <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> 6 <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/> 7 <property name="username" value="holly"/> 8 <property name="password" value="sys"/> 9 </bean> 10 11 <!--2.在sqlSessionFactory中注入数据源 --> 12 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 13 <!-- 注入数据源 --> 14 <property name="dataSource" ref="oracleDataSource"/> 15 16 <!-- 引用mybatis的主配置文件,(注册dao映射文件) --> 17 <property name="configLocation"> 18 <value>classpath:mybatis-config.xml</value> 19 </property> 20 </bean> 21 22 23 24 <!-- 4.dao注入sqlSesson --> 25 <bean id="userdao" class="org.mybatis.spring.mapper.MapperFactoryBean"> 26 <property name="mapperInterface" value="com.dao.UserInfoDao"/> 27 <property name="sqlSessionFactory" ref="sqlSessionFactory"/> 28 </bean> 29 30 31 32 <!-- 6.在Action中注入service --> 33 <bean id="userAction" class="com.action.UserInfoAction"> 34 <property name="dao" ref="userdao"/> 35 </bean> 36 </beans>
applicationContext.xml
9、在src下创建struts.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "struts-2.1.dtd" > 3 <struts> 4 <!-- 中文乱码处理 --> 5 <constant name="struts.i18n.encoding" value="UTF-8"/> 6 <package name="default" namespace="/" extends="struts-default"> 7 <!-- method是对应action类的有返回值的方法名 --> 8 <!-- 1.登录,注册,通配符设置,动态调用方法,*表示Login或register --> 9 <!-- class为spring配置文件action的bean 的id --> 10 <action name="login" class="userAction" method="login"> 11 <result name="success" type="redirectAction">findAll</result> 12 <result name="error">error.jsp</result> 13 </action> 14 15 <!-- 2.查询所有 --> 16 <action name="findAll" class="userAction" method="findAll"> 17 <result name="success">index.jsp</result> 18 <result name="error">error.jsp</result> 19 </action> 20 <!-- 3.根据id查询 --> 21 <action name="findById" class="userAction" method="findById"> 22 <result name="success">update.jsp</result> 23 <result name="error">error.jsp</result> 24 </action> 25 <!-- 4.修改 --> 26 <action name="updateObj" class="userAction" method="updateObj"> 27 <result name="success" type="redirectAction">findAll</result> 28 <result name="error">update.jsp</result> 29 </action> 30 <!-- 5.删除 --> 31 <action name="deleteObj" class="userAction" method="deleteObj"> 32 <result name="success" type="redirectAction">findAll</result> 33 <result name="error">error.jsp</result> 34 </action> 35 <!-- 6.添加 --> 36 <action name="saveObj" class="userAction" method="saveObj"> 37 <result name="success" type="redirectAction">findAll</result> 38 <result name="error">error.jsp</result> 39 </action> 40 </package> 41 </struts>
struts.xml
10、编辑WebRoot下WEB-INF下web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 3 <context-param> 4 <param-name>contextConfigLocation</param-name> 5 <param-value>classpath:applicationContext.xml</param-value> 6 </context-param> 7 <listener> 8 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 9 </listener> 10 <filter> 11 <filter-name>struts2</filter-name> 12 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 13 </filter> 14 15 <filter-mapping> 16 <filter-name>struts2</filter-name> 17 <url-pattern>/*</url-pattern> 18 </filter-mapping> 19 20 <welcome-file-list> 21 <welcome-file>login.jsp</welcome-file> 22 </welcome-file-list> 23 24 </web-app>
web.xml
11、在WebRoot下创建login.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP ‘index.jsp‘ starting page</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 </head> 22 23 <body> 24 <center> 25 <fieldset style="width:400px;"> 26 <legend>登录</legend> 27 <form action="login.action" method="post"> 28 <table> 29 <tr> 30 <td>用户名:</td> 31 <td><input type="text" name="user.name"/></td> 32 </tr> 33 <tr> 34 <td>密码:</td> 35 <td><input type="password" name="user.password"/></td> 36 </tr> 37 <tr> 38 <td><input type="submit" value="提交"/></td> 39 <td><input type="reset" value="重置"/></td> 40 </tr> 41 </table> 42 43 </form> 44 </fieldset> 45 </center> 46 </body> 47 </html>
login.jsp
12、在WebRoot下创建index.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme() + "://" 6 + request.getServerName() + ":" + request.getServerPort() 7 + path + "/"; 8 %> 9 10 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 11 <html> 12 <head> 13 <base href="<%=basePath%>"> 14 15 <title>My JSP ‘index.jsp‘ starting page</title> 16 <meta http-equiv="pragma" content="no-cache"> 17 <meta http-equiv="cache-control" content="no-cache"> 18 <meta http-equiv="expires" content="0"> 19 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 20 <meta http-equiv="description" content="This is my page"> 21 <!-- 22 <link rel="stylesheet" type="text/css" href="styles.css"> 23 --> 24 </head> 25 26 <body> 27 <center> 28 29 <fieldset style="width:500px"> 30 <legend> 31 首页 32 </legend> 33 34 <a href="register.jsp">注册</a> 35 <form action="findAll.action" method="post"> 36 <table border="1"> 37 <tr> 38 <td>编号</td> 39 <td>用户名</td> 40 <td>密码</td> 41 <td>手机号</td> 42 <td>是否是管理员</td> 43 <td>操作</td> 44 </tr> 45 <c:forEach var="i" items="${requestScope.list}"> 46 <tr> 47 <td>${i.id}</td> 48 <td>${i.name}</td> 49 <td>${i.password}</td> 50 <td>${i.telephone}</td> 51 <td>${i.isadmin}</td> 52 <td><a href="findById?user.id=${i.id}">修改</a> | <a href="deleteObj?user.id=${i.id}">删除</a></td> 53 </tr> 54 </c:forEach> 55 </table> 56 57 </form> 58 </fieldset> 59 </center> 60 </body> 61 </html>
index.jsp
13、在WebRoot下创建register.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>注册</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 19 </head> 20 21 <body> 22 <center> 23 <fieldset style="width:400px;"> 24 <legend>注册</legend> 25 <form action="saveObj.action" method="post"> 26 <table> 27 <tr> 28 <td>用户名:</td> 29 <td><input type="text" name="user.name"/></td> 30 </tr> 31 <tr> 32 <td>密码:</td> 33 <td><input type="password" name="user.password"/></td> 34 </tr> 35 <tr> 36 <td>电话号码:</td> 37 <td><input type="text" name="user.telephone"/></td> 38 </tr> 39 <tr> 40 <td>是否是管理员:</td> 41 <td> 42 <input type="radio" name="user.isadmin" value="是"> 43 <input type="radio" name="user.isadmin" value="否" checked="checked"/> 44 </td> 45 </tr> 46 <tr> 47 <td><input type="submit" value="提交"/></td> 48 <td><input type="reset" value="重置"/></td> 49 </tr> 50 </table> 51 52 </form> 53 </fieldset> 54 </center> 55 </body> 56 </html>
register.jsp
14、在WebRoot下创建update.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>修改页面</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 19 </head> 20 21 <body> 22 <center> 23 <fieldset style="width:400px;"> 24 <legend>修改页面</legend> 25 <form action="updateObj.action" method="post"> 26 <table> 27 <tr> 28 <td>用户名:</td> 29 <td><input type="text" name="user.name" value="${uv.name}"/></td> 30 </tr> 31 <tr> 32 <td>密码:</td> 33 <td><input type="password" name="user.password" value="${uv.password}"/></td> 34 </tr> 35 <tr> 36 <td>电话号码:</td> 37 <td><input type="text" name="user.telephone" value="${uv.telephone}"/></td> 38 </tr> 39 <tr> 40 <td>是否是管理员:</td> 41 <td> 42 <input type="radio" name="user.isadmin" value="是"> 43 <input type="radio" name="user.isadmin" value="否" checked="checked"/> 44 </td> 45 </tr> 46 <tr> 47 <td><input type="submit" value="提交"/></td> 48 <td><input type="reset" value="重置"/></td> 49 </tr> 50 </table> 51 52 </form> 53 </fieldset> 54 </center> 55 </body> 56 </html>
update.jsp
15、在WebRoot下创建error.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP ‘index.jsp‘ starting page</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 </head> 22 23 <body> 24 操作失败! 25 </body> 26 </html>
error.jsp
16、运行
时间: 2024-10-24 05:22:57