VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 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>
复制代码

 


相关教程
关于我们--广告服务--免责声明--本站帮助-友情链接--版权声明--联系我们       黑ICP备07002182号