7、Spring+Struts2+MyBaits(无映射接口实现类)

时间:2023-01-01 19:08:59

1、创建userinfo.sql数据库脚本

  create table userinfo
(id number(4),
name varchar2(50),
password varchar2(20),
telephone varchar2(15),
isadmin varchar2(5)); --4.2 用户表序列
create sequence seq_userinfo;
alter table userinfo add constraint pk_userinfo_id primary key(id); insert into userinfo values(seq_userinfo.nextval,'holly','','134518024
','是');
commit;

userinfo.sql

2、创建如下项目结构

7、Spring+Struts2+MyBaits(无映射接口实现类)

7、Spring+Struts2+MyBaits(无映射接口实现类)

3、在src的com.bean包下创建UserInfo.java

 package com.bean;
/**
* 用户信息表
* @author Holly老师
*
*/
public class UserInfo {
private Integer id;//编号
private String name; //姓名
private String password; //密码
private String telephone; //电话
private String isadmin; //是否是管理员 public UserInfo() { }
public UserInfo(Integer id, String name, String password, String telephone,
String isadmin) {
this.id = id;
this.name = name;
this.password = password;
this.telephone = telephone;
this.isadmin = isadmin;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getIsadmin() {
return isadmin;
}
public void setIsadmin(String isadmin) {
this.isadmin = isadmin;
}
@Override
public String toString() {
return "UserInfo [id=" + id + ", isadmin=" + isadmin + ", name=" + name
+ ", password=" + password + ", telephone=" + telephone + "]";
} }

UserInfo.java

4、在src的com.dao包下创建UserInfoDao.java

 package com.dao;

 import java.util.List;

 import com.bean.UserInfo;
/**
* mybatis映射文件的dao接口
* @author Holly老师
*
*/
public interface UserInfoDao {
/**
* 1.根据对象去查询对象
* @return
*/
public UserInfo findUser(UserInfo userinfo);
/**
* 2.查询所有
* @return
*/
public List<UserInfo> findAll();
/**
* 3.根据id查询
*/
public UserInfo findById(int id);
/**
* 4.保存对象
* @param userinfo
*/
public void saveObj(UserInfo userinfo);
/**
* 5.修改对象
*/
public void updateObj(UserInfo userinfo);
/**
* 6.删除对象
* @param id
*/
public void deleteObj(int id); }

UserInfoDao.java

5、在src的com.dao包下创建UserInfoDao.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.dao.UserInfoDao">
<!--
<resultMap type="userinfo" id="userMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="password" column="password"/>
<result property="telephone" column="telephone"/>
<result property="isadmin" column="isadmin"/>
</resultMap>
-->
<!--1.根据对象查询对象 -->
<select id="findUser" parameterType="userinfo" resultType="userinfo">
select * from userinfo where name=#{name} and password=#{password}
</select> <!-- 2.查询所有 -->
<select id="findAll" resultType="userinfo">
select * from userinfo
</select> <!-- 3.根据id查询 -->
<select id="findById" parameterType="int" resultType="userinfo">
select * from userinfo where id=#{id}
</select> <!-- 4.根据id删除 -->
<delete id="deleteObj" parameterType="int">
delete from userinfo where id=#{id}
</delete> <!-- 5.修改 -->
<update id="updateObj" parameterType="userinfo">
update userinfo set name=#{name},password=#{password},telephone=#{telephone},isadmin=#{isadmin} where id=#{id}
</update> <!-- 6.保存 -->
<insert id="saveObj" parameterType="userinfo">
insert into userinfo values(seq_userinfo.nextval,#{name},#{password},#{telephone},#{isadmin})
</insert> </mapper>

UserInfoDao.xml

6、在src下创建mybatis-config.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>
<!-- mybatis映射文件的别名配置 -->
<typeAliases>
<typeAlias type="com.bean.UserInfo" alias="userinfo"/>
</typeAliases> <!-- 注册mybatis的映射文件 -->
<mappers>
<mapper resource="com/dao/UserInfoDao.xml"/>
</mappers>
</configuration>

mybatis-config.xml

7、在src下com.action包下创建UserInfoAction.java

 package com.action;

 import java.util.List;

 import javax.servlet.http.HttpServletRequest;

 import org.apache.struts2.ServletActionContext;

 import com.bean.UserInfo;
import com.dao.UserInfoDao;
import com.opensymphony.xwork2.ActionSupport;
/**
*
* @author pc
* ActionSupport可以最数据校验
*/
public class UserInfoAction extends ActionSupport {
//action中引用实体类和页面上一一对应
//表单name属性值里的对象
private UserInfo user; /*注入dao*/
private UserInfoDao dao;
/**
* 1.登录
* @return
*/
public String login(){
System.out.println("action:"+user.getName());
UserInfo userinfo=dao.findUser(user);
if(userinfo!=null){
System.out.println("action查询");
return "success";
}else{
return "error";
}
}
/**
* 2.查询所有
* @return
*/
public String findAll(){
List<UserInfo> list=dao.findAll();
if(list!=null){
System.out.println("action查询所有");
HttpServletRequest request=ServletActionContext.getRequest();
request.setAttribute("list", list);
return "success";
}else{
return "error";
}
}
/**
* 3.根据id删除
* @return
*/
public String deleteObj(){
System.out.println("id:"+user.getId());
try {
dao.deleteObj(user.getId());
System.out.println("删除成功");
return "success";
} catch (Exception e) {
System.out.println("删除失败");
e.printStackTrace();
return "error";
}
} /**
* 4.根据id查询
* @return
*/
public String findById(){
System.out.println("id:"+user.getId());
UserInfo userinfo=dao.findById(user.getId());
if(userinfo!=null){
System.out.println("根据id查询到");
HttpServletRequest request=ServletActionContext.getRequest();
request.setAttribute("uv", userinfo);
return "success";
}else{
return "error";
}
}
/**
* 5.修改
* @return
*/
public String updateObj(){
System.out.println("action:"+user);
try {
dao.updateObj(user);
System.out.println("修改成功");
return "success";
} catch (Exception e) {
System.out.println("修改失败");
e.printStackTrace();
return "error";
}
}
/**
* 6.添加
* @return
*/
public String saveObj(){
System.out.println("action:"+user);
try {
dao.saveObj(user);
System.out.println("save success");
return "success";
} catch (Exception e) {
System.out.println("save fail");
e.printStackTrace();
return "error";
}
} public UserInfo getUser() {
return user;
} public void setUser(UserInfo user) {
this.user = user;
} public UserInfoDao getDao() {
return dao;
}
public void setDao(UserInfoDao dao) {
this.dao = dao;
}
}

UserInfoAction.java

8、在src下创建applicationContext.xml

 <?xml version="1.0" encoding="UTF-8"?>
<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 ">
<!-- 1.配置数据源 -->
<bean id="oracleDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
<property name="username" value="holly"/>
<property name="password" value="sys"/>
</bean> <!--2.在sqlSessionFactory中注入数据源 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="oracleDataSource"/> <!-- 引用mybatis的主配置文件,(注册dao映射文件) -->
<property name="configLocation">
<value>classpath:mybatis-config.xml</value>
</property>
</bean> <!-- 4.dao注入sqlSesson -->
<bean id="userdao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.dao.UserInfoDao"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean> <!-- 6.在Action中注入service -->
<bean id="userAction" class="com.action.UserInfoAction">
<property name="dao" ref="userdao"/>
</bean>
</beans>

applicationContext.xml

9、在src下创建struts.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "struts-2.1.dtd" >
<struts>
<!-- 中文乱码处理 -->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<package name="default" namespace="/" extends="struts-default">
<!-- method是对应action类的有返回值的方法名 -->
<!-- 1.登录,注册,通配符设置,动态调用方法,*表示Login或register -->
<!-- class为spring配置文件action的bean 的id -->
<action name="login" class="userAction" method="login">
<result name="success" type="redirectAction">findAll</result>
<result name="error">error.jsp</result>
</action> <!-- 2.查询所有 -->
<action name="findAll" class="userAction" method="findAll">
<result name="success">index.jsp</result>
<result name="error">error.jsp</result>
</action>
<!-- 3.根据id查询 -->
<action name="findById" class="userAction" method="findById">
<result name="success">update.jsp</result>
<result name="error">error.jsp</result>
</action>
<!-- 4.修改 -->
<action name="updateObj" class="userAction" method="updateObj">
<result name="success" type="redirectAction">findAll</result>
<result name="error">update.jsp</result>
</action>
<!-- 5.删除 -->
<action name="deleteObj" class="userAction" method="deleteObj">
<result name="success" type="redirectAction">findAll</result>
<result name="error">error.jsp</result>
</action>
<!-- 6.添加 -->
<action name="saveObj" class="userAction" method="saveObj">
<result name="success" type="redirectAction">findAll</result>
<result name="error">error.jsp</result>
</action>
</package>
</struts>

struts.xml

10、编辑WebRoot下WEB-INF下web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list> </web-app>

web.xml

11、在WebRoot下创建login.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<center>
<fieldset style="width:400px;">
<legend>登录</legend>
<form action="login.action" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="user.name"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="user.password"/></td>
</tr>
<tr>
<td><input type="submit" value="提交"/></td>
<td><input type="reset" value="重置"/></td>
</tr>
</table> </form>
</fieldset>
</center>
</body>
</html>

login.jsp

12、在WebRoot下创建index.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<center> <fieldset style="width:500px">
<legend>
首页
</legend> <a href="register.jsp">注册</a>
<form action="findAll.action" method="post">
<table border="1">
<tr>
<td>编号</td>
<td>用户名</td>
<td>密码</td>
<td>手机号</td>
<td>是否是管理员</td>
<td>操作</td>
</tr>
<c:forEach var="i" items="${requestScope.list}">
<tr>
<td>${i.id}</td>
<td>${i.name}</td>
<td>${i.password}</td>
<td>${i.telephone}</td>
<td>${i.isadmin}</td>
<td><a href="findById?user.id=${i.id}">修改</a>&nbsp;|&nbsp;<a href="deleteObj?user.id=${i.id}">删除</a></td>
</tr>
</c:forEach>
</table> </form>
</fieldset>
</center>
</body>
</html>

index.jsp

13、在WebRoot下创建register.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>注册</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page"> </head> <body>
<center>
<fieldset style="width:400px;">
<legend>注册</legend>
<form action="saveObj.action" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="user.name"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="user.password"/></td>
</tr>
<tr>
<td>电话号码:</td>
<td><input type="text" name="user.telephone"/></td>
</tr>
<tr>
<td>是否是管理员:</td>
<td>
<input type="radio" name="user.isadmin" value="是">
<input type="radio" name="user.isadmin" value="否" checked="checked"/>
</td>
</tr>
<tr>
<td><input type="submit" value="提交"/></td>
<td><input type="reset" value="重置"/></td>
</tr>
</table> </form>
</fieldset>
</center>
</body>
</html>

register.jsp

14、在WebRoot下创建update.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>修改页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page"> </head> <body>
<center>
<fieldset style="width:400px;">
<legend>修改页面</legend>
<form action="updateObj.action" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="user.name" value="${uv.name}"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="user.password" value="${uv.password}"/></td>
</tr>
<tr>
<td>电话号码:</td>
<td><input type="text" name="user.telephone" value="${uv.telephone}"/></td>
</tr>
<tr>
<td>是否是管理员:</td>
<td>
<input type="radio" name="user.isadmin" value="是">
<input type="radio" name="user.isadmin" value="否" checked="checked"/>
</td>
</tr>
<tr>
<td><input type="submit" value="提交"/></td>
<td><input type="reset" value="重置"/></td>
</tr>
</table> </form>
</fieldset>
</center>
</body>
</html>

update.jsp

15、在WebRoot下创建error.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
操作失败!
</body>
</html>

error.jsp

16、运行

7、Spring+Struts2+MyBaits(无映射接口实现类)

7、Spring+Struts2+MyBaits(无映射接口实现类)

7、Spring+Struts2+MyBaits(无映射接口实现类)的更多相关文章

  1. 8、Spring&plus;Struts2&plus;MyBaits&lpar;Spring注解&plus;jdbc属性文件&plus;log4j属性文件&rpar;

    一.注解理论 使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base- ...

  2. Spring boot 配置文件参数映射到配置类属性

    [参考文章]:SpringBoot之@EnableConfigurationProperties分析 [参考文章]:在Spring Boot中使用 @ConfigurationProperties 注 ...

  3. 5、Spring&plus;Struts2&plus;MyBatis&plus;分页(mybatis无代理)增删改查

    1.创建如下项目结构 2.在src下的com.entity包下创建Dept.java package com.entity; /** * 部门表 * @author Holly老师 * */ publ ...

  4. MyBatis Spring整合配置映射接口类与映射xml文件

    本文转自http://blog.csdn.net/zht666/article/details/38706083 Spring整合MyBatis使用到了mybatis-spring,在配置mybati ...

  5. spring测试junit事务管理及spring面向接口注入和实现类单独注入(无实现接口),实现类实现接口而实现类单独注入否则会报错。

    1.根据日志分析,spring junit默认是自动回滚,不对数据库做任何的操作. 18:16:57.648 [main] DEBUG o.s.j.d.DataSourceTransactionMan ...

  6. Spring&plus;Struts2&plus;Hibernate框架整合流程

    一:基本步骤 新建Maven项目,导入相关依赖(推荐) 在WEB-INF的web.xml中进行配置 ————–Hibernate配置 —————- 创建entity包,创建数据库相关实体类 根据实体类 ...

  7. 最新版ssh hibernate spring struts2环境搭建

    最新版ssh hibernate spring struts2环境搭建 最新版spring Framework下载地址:spring4.0.0RELEASE环境搭建 http://repo.sprin ...

  8. 6、Spring&plus;Struts2&plus;MyBatis(mybatis有代理)整合增删改查

    1.创建如下的oracle脚本 create table userinfo (id ), name ), password telephone ), isadmin )); --4.2 用户表序列 c ...

  9. spring&plus;struts2&plus;ibatis 框架整合以及解析

    一. spring+struts2+ibatis 框架 搭建教程 参考:http://biancheng.dnbcw.net/linux/394565.html 二.分层 1.dao: 数据访问层(增 ...

随机推荐

  1. RabbitMQ原理与相关操作&lpar;一&rpar;

    小编是菜鸟一枚,最近想试试MQ相关的技术,所以自己看了下RabbitMQ官网,试着写下自己的理解与操作的过程. 刚开始的第一篇,原理只介绍 生产者.消费者.队列,至于其他的内容,会在后续中陆续补齐. ...

  2. PHP Fatal Error&colon; call to undefined function mysql&lowbar;connect&lpar;&rpar; &lbrack;duplicate&rsqb;

    You shouldn't use mysql_* functions to start with. They are deprecated as of PHP 5.5. Use mysqli or ...

  3. 页面多次调用查询文章(have&lowbar;posts&lpar;&rpar;)

    通常来说一个页面只调用查询一次文章.have_posts()   如果页面,比如首页需要按照不同的查询参数调用多次文章 需要做如下处理:   //loop前 $temp_query = $wp_que ...

  4. PHP获得文件的md5并检验是否被修改

    由于需要判断上传的文件是否被修改过,需要记录上传文件的md5值,所以这里说一下一下获取文件md5值的方法.   md5_file() md5_file() 函数计算文件的 MD5 散列.md5() 函 ...

  5. listen函数

    listen函数仅仅由TCP服务器调用,它做2件事: 1)当socket函数创建一个套接字时,它被假设为一个主动套接字,也就是说,它是一个将调用connect发起连接的客户套接字 listen函数把一 ...

  6. java课堂笔记4

  7. python 画个小猪佩奇

    不知道大家小时候有没有学习过logo语言,就是操纵一只小王八,来画各种图案.博主小学微机课就学习了这个,最近发现python的turtle包就是logo语言,所以画个小猪佩奇和大家分享. 代码来自知乎 ...

  8. php 二维数组按照某个键排序

    $date = array_column($arr, 'run_date'); //上面得到的结果:array(0=>'2017-11-21',1=>'2017-11-20',3=> ...

  9. redis实现与分析-多机数据库

    1.复制,主从结构 redis 2.8以前的复制,由2个步骤 1.初始的同步 2.命令传播 存在问题:同步时出现主从 断线,需要重新发送同步sync信号,非常消耗性能 redis2.8以后新版复制 采 ...

  10. Least slack time scheduling

    This algorithm is also known as least laxity first. 词语解释:Laxity 松懈的:马虎的:不严格的,Least-Laxity-First 松弛程度 ...