当前位置:
首页 > Python基础教程 >
-
java教程之利用idea创建一个基本的smm项目(带增删(2)
="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
接着在webapp下WEB-INF配置web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>mvcDemo</display-name> <welcome-file-list> <welcome-file>/pages/index.jsp</welcome-file> </welcome-file-list> <!-- 注册ServletContext监听器,创建容器对象,并且将ApplicationContext对象放到Application域中 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 指定spring核心配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 解决乱码的过滤器 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 指定配置文件位置和名称 如果不设置,默认找/WEB-INF/<servlet-name>-servlet.xml --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
数据库创建一个表
DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` varchar(200) NOT NULL, `name` varchar(200) CHARACTER SET utf8 NOT NULL, `pwd` varchar(200) CHARACTER SET utf8 NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
可以先添加数据方便测试
INSERT INTO `user` VALUES ('2', '22', '44'); INSERT INTO `user` VALUES ('5', '123', '123'); INSERT INTO `user` VALUES ('1', '123s', '123');
开始编写代码
pojo代码
package com.lwy.pojo; public class User { private String id; public String getId() { return id; } public void setId(String id) { this.id = id; } private String name; private String pwd; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }
mapper
package com.lwy.mapper; import com.lwy.pojo.User; import org.apache.ibatis.annotations.Param; import java.util.List; public interface UserMapper { User userlogin(@Param("name")String name, @Param("pwd") String pwd); int adduser(User user); int delteuser(String id); int updateuser(User user); User getById(String id); List<User> getallUser(); }
xxMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper SYSTEM "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.lwy.mapper.UserMapper"> <resultMap id="ResultMap" type="User"> <id property="userid" column="user_id"></id> <result property="name" column="name"></result> <result property="pwd" column="pwd"></result> </resultMap> <select id="userlogin" resultType="User"> SELECT * FROM `user` WHERE `name`=#{name} AND pwd=#{pwd} </select> <insert id="adduser" parameterType="User"> INSERT INTO user(id,name,pwd) VALUES(#{id},#{name},#{pwd}) </insert> <delete id="delteuser" parameterType="String"> delete from user where id=#{id} </delete> <update id="updateuser" parameterType="User"> UPDATE user SET NAME=#{name}, pwd=#{pwd} WHERE id=#{id} </update> <select id="getById" parameterType="String" resultMap="ResultMap"> SELECT * FROM user WHERE id=#{id} </select> <select id="getallUser" resultMap="ResultMap"> SELECT * FROM user </select> </mapper>
service层
package com.lwy.service; import com.lwy.pojo.User; import org.apache.ibatis.annotations.Param; import java.util.List; public interface UserService { User userlogin(@Param("name") String name, @Param("pwd") String pwd); int adduser(User user); int delteuser(String id); int updateuser(User user); User getById(String id); List<User> getallUser(); }
实现
package com.lwy.service.impl; import com.lwy.pojo.User; import com.lwy.mapper.UserMapper; import com.lwy.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; import java.util.UUID; @Service public class UserServiceImpl implements UserService { @Autowired UserMapper userMapper; @Override public int adduser(User user) { String id = UUID.randomUUID().toString(); user.setId(id); return userMapper.adduser(user); } @Override public int delteuser(String id) { return userMapper.delteuser(id); } @Override public int updateuser(User user) { return userMapper.updateuser(user); } @Override public User getById(String id) { return userMapper.getById(id); } @Override public List<User> getallUser() { return userMapper.getallUser(); } @Override public User userlogin(String name, String pwd) { return userMapper.userlogin(name, pwd); } }
controller层
package com.lwy.controller; import com.lwy.pojo.User; import com.lwy.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import java.util.List; @Controller public class UserController { @Autowired UserService userService; @RequestMapping("/login.do") public String login(String username, String password,Model model){ System.out.println("开始登陆"); userService.userlogin(username,password); // System.out.println("开始查询所有用户"); // List<User> userlist = userService.getallUser(); // model.addAttribute("userlist",userlist); return "redirect:/getAllUser.do"; } @RequestMapping("/addUser.do") public String addUser(User user){ userService.adduser(user); return "redirect:/getAllUser.do"; } @RequestMapping("/deleteUser.do") public String deleteUser(String id){ userService.delteuser(id); return "redirect:/getAllUser.do"; } @RequestMapping("/updateUser.do") public String updateUser(User user){ userService.updateuser(user); return "redirect:/getAllUser.do"; } @RequestMapping("/getById.do") public String getUserById(String id,Model model){ model.addAttribute("user",userService.getById(id)); return "pages/user_update"; } @RequestMapping("/getAllUser.do") public String getAllUser(Model model){ System.out.println("开始查询所有用户"); List<User> userlist = userService.getallUser(); model.addAttribute("userlist",userlist); return "pages/userlist"; } }
部署到tomcat中
如图
选择本地的tomcat
页面
登陆
<%-- Created by IntelliJ IDEA. User: hasee Date: 2019/6/3 Time: 16:03 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="../login.do" method="post"> 账号:<input type="text" name="username" /> 密码:<input type="password" name="password" /> <button type="submit">提交</button> </form> </body> </html>
显示
<%-- Created by IntelliJ IDEA. User: hasee Date: 2019/6/3 Time: 18:27 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Title</title> </head> <body> 列表 <a href="/pages/user_add.jsp">添加</a> <a href="/pages/logout.do">退出</a> <a href="/pages/admin.jsp">admin page</a> <table> <tr> <td>id</td> <td>姓名</td> <td>密码</td> <td>操作</td> </tr> <c:forEach items="${requestScope.userlist}" var="list"> <tr> <td>${list.id}</td> <td>${list.name}</td> <td>${list.pwd}</td> <td> <a href="../getById.do?id=${list.id}">修改</a> <a href="../deleteUser.do?id=${list.id}">删除</a> </td> </tr> </c:forEach> </table> </body> </html>
添加
<%-- Created by IntelliJ IDEA. User: hasee Date: 2019/6/3 Time: 20:18 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form method="post" action="../addUser.do"> 你的账号<input type="text" name="name"> 你的密码<input type="text" name="pwd" /> <button type="submit">保存</button> </form> </body> </html>
修改
<%-- Created by IntelliJ IDEA. User: hasee Date: 2019/6/3 Time: 20:18 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Title</title> </head> <body> <form method="post" action="../updateUser.do"> <input type="hidden" name="id" value="${requestScope.user.id}"/> gfdshjsahsdhsdhsdhsd 你的账号<input type="text" name="name" value="${requestScope.user.name}"/> 你的密码<input type="text" name="pwd" value="${requestScope.user.pwd}"/> <button type="submit">修改</button> </form> </body> </html>
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式