栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

40分钟手搓从数据库到前端页面(附完整代码)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

40分钟手搓从数据库到前端页面(附完整代码)

建表

快速建一个userinfos表,并插入两条数据

show databases;

drop database if exists mydemo;

create database mydemo;

use mydemo;

create table userinfos(
userid int primary key not null auto_increment,
username varchar(20) not null,
birthday date not null);

show tables;

insert into userinfos(username,birthday) values ('aaa','1996-9-9');
insert into userinfos(username,birthday) values('bbb','1996-9-9');

select * from userinfos;

新建项目,引入jar包

项目结构预览
由于40分钟没能搓完,这会儿已经ssmdemo6了!,想要框架玩的6,基础功必须扎实!!

pom.xml




  4.0.0

  com.kgc
  ssmdemo2
  1.0-SNAPSHOT
  war

  ssmdemo2 Maven Webapp
  
  http://www.example.com

  
    UTF-8
    1.8
    1.8
    4.3.7.RELEASE
    2.9.8
  

    
  
    
      junit
      junit
      4.11
      test
    

    
    
      mysql
      mysql-connector-java
      5.1.38
    
    
      com.alibaba
      druid
      1.1.10
    

    
    
      org.mybatis
      mybatis
      3.4.6
    
    
      org.mybatis
      mybatis-spring
      1.3.2
    

    
    
      javax.servlet
      servlet-api
      2.5
      
      provided
    
    
    
      javax.servlet
      jstl
      1.2
    
   
    
    
    
      com.fasterxml.jackson.core
      jackson-core
      ${json-version}
    
    
    
      com.fasterxml.jackson.core
      jackson-databind
      ${json-version}
    
    
    
      com.fasterxml.jackson.core
      jackson-annotations
      ${json-version}
    

    
    
    
    
    
      org.springframework
      spring-core
      ${spring-version}
    
    
    
      org.springframework
      spring-beans
      ${spring-version}
    
    
    
      org.springframework
      spring-context
      ${spring-version}
    
    
    
    
    
      org.springframework
      spring-jdbc
      ${spring-version}
    
    
    
      org.springframework
      spring-tx
      ${spring-version}
    
    
    
    
    
      org.springframework
      spring-webmvc
      ${spring-version}
    

  

  
    ssmdemo2
    
      
 
   maven-clean-plugin
   3.1.0
 
 
 
   maven-resources-plugin
   3.0.2
 
     
 
 
   org.apache.maven.plugins
   maven-compiler-plugin
   3.6.1
   
     1.8
     1.8
   
 

   maven-surefire-plugin
   2.22.1
 
 
   maven-war-plugin
   3.2.2
 
 
   maven-install-plugin
   2.5.2
 
 
   maven-deploy-plugin
   2.8.2
 
      
    
     
    
      
 src/main/java
 
   ***.xml
 
 false
      
    
  


src.main.java entity 实体类
package com.kgc.ssmdemo2.entity;

import java.io.Serializable;
import java.util.Date;



public class Userinfos implements Serializable {
    private int userid;
    private String username;
    private Date birthday;

    public int getUserid() {
 return userid;
    }

    public void setUserid(int userid) {
 this.userid = userid;
    }

    public String getUsername() {
 return username;
    }

    public void setUsername(String username) {
 this.username = username;
    }

    public Date getBirthday() {
 return birthday;
    }

    public void setBirthday(Date birthday) {
 this.birthday = birthday;
    }
}

dao 接口
package com.kgc.ssmdemo5.dao;

import com.kgc.ssmdemo5.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserDAO {
    @Insert("insert into userinfos(username,birthday) values (#{username},#{birthday}) ")
    public void insertUser(User user);

    @Delete("delete from userinfos where userid=#{userid}")
    public void delUser(@Param("userid") int userid);

    @Update("update userinfos set username=#{username},birthday=#{birthday} where userid=#{userid}")
    public void modUser(User user);

    @Select("select * from userinfos")
    public List findAll();

    @Select("select * from userinfos where userid=#{userid}")
    public User findById(@Param("userid") int userid);
}

这里用了几个注解的小操作,省去了后面写usermapper.xml和mybatis-config.xml ,距离40分钟搓完的小目标又近了一步,哈哈!

service
package com.kgc.ssmdemo2.service;

import com.kgc.ssmdemo2.dao.UserDAO;
import com.kgc.ssmdemo2.entity.Userinfos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class UserService {

    @Autowired
    private UserDAO userDAO;

    public void saveUser(Userinfos user){
 userDAO.insertUser(user);
    }

    public void deUser(int userid){
 userDAO.delUser(userid);
    }

    public void modUser(Userinfos user){
 userDAO.modUser(user);
    }

    public List findAll(){
 return userDAO.findAll();
    }

    public Userinfos finByid(int userid){
 return userDAO.finByid(userid);
    }
}

controller
package com.kgc.ssmdemo2.controller;

import com.kgc.ssmdemo2.entity.Userinfos;
import com.kgc.ssmdemo2.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;


@Controller
public class InitCtrlk {

    @Autowired
    private UserService userService;
    
    
    @InitBinder
    public void dateHanlder(WebDataBinder binder){
 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
 sdf.setLenient(true);
 binder.registerCustomEditor(Date.class,new CustomDateEditor(sdf,true));
    }

    @RequestMapping("/init.do")
    public String init(HttpServletRequest request){
 request.setAttribute("infos",userService.findAll());
 return "index.jsp";
    }

    @RequestMapping("/save.do")
    public String save(Userinfos user){
userService.saveUser(user);
return "redirect:/init.do";
    }

    @RequestMapping("/del.do")
    public String del(int userid){
 userService.deUser(userid);
 return "redirect:/init.do";
    }

    @RequestMapping("/mod.do")
    public String mod(Userinfos user){
 userService.modUser(user);
 return "redirect:/init.do";
    }

    @RequestMapping("/single.do")
    public String single(int userid,HttpServletRequest request){
 request.setAttribute("user",userService.finByid(userid));
 return "user.jsp";
    }
}

resources spring.xml



    
    

    
    

    
    
 
 
 
 
 
 
 
 
 
    

    
    
 
 
    
    
    

    
    
    
 
 
 
 
 
 
    

    
    
 
 
    


web

开始搓邪恶的前端页面小程序,加油!

web.xml



  Archetype Created Web Application

  
  
    contextConfigLocation
    classpath:spring.xml
  

  
  
    charset
    org.springframework.web.filter.CharacterEncodingFilter
  
    encoding
    utf-8
  
  
  
    charset
    /*
  

  
  
    org.springframework.web.context.ContextLoaderListener
  

  
  
    spring
     
    org.springframework.web.servlet.DispatcherServlet
  
    contextConfigLocation
    classpath:spring.xml
  
  
  
  
    spring
    *.do
  


index.jsp

留意这里注释掉的地方,掌握这些小技巧,40分钟不是梦!

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: mu_bai
  Date: 2019/11/13
  Time: 22:12
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>


    Title


<%--div.header>a[href='save.jsp']{创建新用户}   打完按tab键,感受快乐--%>



<%--div.ctx>table.table>(tr>th*4)+(tr>td*4)--%>

用户编号 用户姓名 用户生日 用户操作
${us.userid} ${us.username} ${us.birthday} <%--a[href='single.do?userid=']{修改}*2--%> 修改 删除
save.jsp
<%--
  Created by IntelliJ IDEA.
  User: mu_bai
  Date: 2019/11/13
  Time: 22:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


<%--div.ctx>form[action='save.do' method='post']>(label{用户}+input[type='text' name='']+br)*3--%>



user.jsp
<%--
  Created by IntelliJ IDEA.
  User: mu_bai
  Date: 2019/11/13
  Time: 23:19
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"%>


    Title





最后

部署Tomcat

40分钟,一套用SSM框架完成的CURD打完收功!!

彩蛋


卑微博主在敲第9遍的时候,成功在40分钟内把这套SSM简单的哼哼嘿哈打完收工,傻乐半天QAQ!
关于SSM框架之前整合的一些困惑的地方,竟然有了些明悟,果然框架这边上手练才是进步最快的方式。
就像老陈说的,敲20遍,你就成神了,加油!

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号