前端、后台和数据库间数据的交互及基本操作(spring-mybatis-mvc)

前言:项目中使用到了jquery-1.8.3.js框架。

mysql数据库设计;

配置pom.xml :




  4.0.0

  org.example
  spring-mybatis-mvc
  1.0-SNAPSHOT
  war

  spring-mybatis-mvc
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
    5.2.7.RELEASE
  

  
    
      junit
      junit
      4.13
    

    
    
      org.springframework
      spring-webmvc
      ${spring.version}
    

    
    
      org.springframework
      spring-test
      ${spring.version}
    

    
    
      org.springframework
      spring-web
      ${spring.version}
    

    
    
      javax.servlet
      javax.servlet-api
      3.0.1
    

    
    
      org.projectlombok
      lombok
      1.18.16
    

    
    
      org.mybatis
      mybatis
      3.2.8
    

    
    
      org.mybatis
      mybatis-spring
      1.3.2
    

    
    
      mysql
      mysql-connector-java
      8.0.20
    

    
    
      org.springframework
      spring-tx
      ${spring.version}
    

    
    
      org.springframework
      spring-jdbc
      ${spring.version}
    

    
    
      com.alibaba
      druid
      1.2.1
    

    
      com.alibaba
      fastjson
      1.2.74
    

    
    
      org.example
      util
      1.0-SNAPSHOT
    



    

  

  
    spring-mybatis-mvc
    
      
        
          maven-clean-plugin
          3.1.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.8.0
        
        
          maven-surefire-plugin
          2.22.1
        
        
          maven-war-plugin
          3.2.2
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
      
    
  

1、配置数据库连接属性:(spring.xml)

#mysql
mysql_name=数据库账户
mysql_pwd=数据库密码
mysql_url=jdbc:mysql://localhost:3306/数据库名称?characterEncoding=utf-8&serverTimezone=Asia/Shanghai

2、配置Mybatis配置文件:





    
        
        
        

    

3、配置Spring.xml配置文件:




     
    
    
    

    
    
        
            
                classpath:db.properties
            
        
    

    
    
        
        
        
    

    
    
        
    

    
    
        
        
        
        
        
        
        
        
    
    
    
        

    


4、配置springmvc.xml:




    
    

    
    

        
            
                
                    
                        text/html;charset=utf-8
                    
                
            
        

    

    
    

5 、配置实现对数据库进行增删改查操作的.xml文件(实现dao层内方法):





    

    
        insert into t_student
        (
         name,student_no,sex,age,birthday,height,weight,address
        )
        values
        (
            #{name},#{studentNo},#{sex},${age},#{birthday},${height},${weight},#{address}
        )
    

    
        update t_student set
            name=#{name},
            student_no=#{studentNo},
            sex=#{sex},age=${age},
                             birthday=#{birthday},
                             height=${height},
                             weight=${weight},
                             address=#{address}
            where id=#{id}
    

    
        delete from t_student where id = ${id}
    

6、配置好web.xml配置文件:




  Archetype Created Web Application

  
  
    contextConfigLocation
    classpath:spring.xml
  

  
  
    org.springframework.web.context.ContextLoaderListener
  

  
  
    springMvc
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:springMvc.xml
    

    
    1

  

  
    springMvc
    /
  

  
  
    default
    *.js
  
  


7、编写好静态页面index.xml(以下是写好的):

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    信息
    
    


    
    
学号 名称 年龄 性别 出生日期 身高 体重 家庭住址 操作

8、controller层:

package com.www.controller;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.text.SimpleDateFormat;
import java.util.Date;

public class BaseController {

    @InitBinder
    public void initBinder(ServletRequestDataBinder binder){

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        /**
         * 重新注册客户端编辑器
         * 寻找与实体中Date.class属性名称和参数名称相同的数据,在通过sdf的方式进行转换为Date
         */
        binder.registerCustomEditor(Date.class,new CustomDateEditor(sdf,true));

    }
}
package com.www.controller;

import com.www.entity.TStudent;
import com.www.service.TStudentServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/tsc")
public class TStudentController extends BaseController{


    @Resource(name = "TStudentServiceImpl")
    private TStudentServiceImpl tssi;

    @RequestMapping("/index")
    public String index(){
        return "/student/index";
    }

    /**
     *
     */
    @RequestMapping("/find")
    @ResponseBody
    public List find(TStudent ts) {
        List list = tssi.find(ts);
        return list;
    }

    /**
     *
     */
    @RequestMapping("/saveOrEdit")
    @ResponseBody
    public Map saveOrEdit(TStudent ts){
        return tssi.saveOrEdit(ts);
    }

    /**
     *
     */
    @RequestMapping("/edit/{id}")
    @ResponseBody
    public TStudent edit(@PathVariable(value = "id") int id){

        TStudent ts = tssi.get(id);

        return ts;
    }
    /**
     *
     */

    @RequestMapping("/delete/{id}")
    @ResponseBody
    public Map delete(@PathVariable(value = "id") int id){
        return tssi.delete(id);
    }

}

9、dao层:

package com.www.dao;

import com.www.entity.TStudent;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface TStudentDao {

    List find(TStudent ts);

    int insert(TStudent ts);
    int update(TStudent ts);

    //不是对象传值 , 需@Param
    int remove(@Param(value = "id") int id);
}

10、实体类(entity):

package com.www.entity;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;

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

@Data
public class TStudent implements Serializable {

    private static final long serialVersionUID = -3252363464563436L;

    private Integer id;
    private String name;
    private String password;
    private String sex;
    @JSONField(format = "yyyy-MM-dd")
    private Date birthday;
    private Date createDate;
    private String studentNo;
    private String address;
    private String status;
    private Integer age;
    private Integer weight;
    private Integer height;
    private Integer classId;

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    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 getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

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

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public String getStudentNo() {
        return studentNo;
    }

    public void setStudentNo(String studentNo) {
        this.studentNo = studentNo;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getWeight() {
        return weight;
    }

    public void setWeight(Integer weight) {
        this.weight = weight;
    }

    public Integer getHeight() {
        return height;
    }

    public void setHeight(Integer height) {
        this.height = height;
    }

    public Integer getClassId() {
        return classId;
    }

    public void setClassId(Integer classId) {
        this.classId = classId;
    }

    @Override
    public String toString() {
        return "TStudent{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", sex='" + sex + '\'' +
                ", birthday=" + birthday +
                ", createDate=" + createDate +
                ", studentNo='" + studentNo + '\'' +
                ", address='" + address + '\'' +
                ", status='" + status + '\'' +
                ", age=" + age +
                ", weight=" + weight +
                ", height=" + height +
                ", classId=" + classId +
                '}';
    }
}

11、service层:

package com.www.service;

import com.www.dao.TStudentDao;
import com.www.entity.TStudent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TStudentServiceImpl {

    @Autowired
    private TStudentDao tsd;

    public List find(TStudent ts){
        List list = tsd.find(ts);
        return list;
    }

    public Map saveOrEdit(TStudent ts) {
        Map map = new HashMap<>();
        int i = 0;
        if (StringUtils.isEmpty(ts.getId())){
            i = tsd.insert(ts);
            map.put("is",i>0);
            map.put("msg","保存"+(i>0?"成功":"失败"));
        }else {
            i = tsd.update(ts);
            map.put("is",i>0);
            map.put("msg","编辑"+(i>0?"成功":"失败"));
        }

        return map;
    }

    public TStudent get(int id) {
        TStudent ts = new TStudent();
        ts.setId(id);
        List list = this.find(ts);
        ts = list.size()>0? list.get(0):null;
        return ts;

    }

    public Map delete(int id) {

        Map map = new HashMap<>();
        int i = tsd.remove(id);
        map.put("msg","删除"+(i>0?"成功!":"失败!"));
        map.put("is",i>0);

        return map;
    }
}

12、简单的执行流程:

前端、后台和数据库间数据的交互及基本操作(spring-mybatis-mvc)_第1张图片

13、。。。。。。(不足的、有疑问的欢迎提出、咨询)感谢观看!!!

你可能感兴趣的:(例题)