SpringBoot-templates+mybtis增删改查+分页模糊查询

SpringBoot-templates+mybtis增删改查+分页模糊查询

使用springBoot和mybtis实现球员管理系统
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

创建SpringBoot项目

软件包在这里插入图片描述
软件包名后面可去掉,版本选8,Web下的springWeb可选可不选不选添加相关的jar就行了,把需要的jar包导进去就OK
点下一步直到创建成功

项目目录

在这里插入图片描述

yml配置

#配置数据源
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/spring?useSSL=false&autoReconnect=true&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
    username: root
    password: root

  #模板引擎
  thymeleaf:
    model: HTML5
    prefix: classpath:/templates/
    suffix: .html
    #指定编码
    encoding: utf-8
    #禁用缓存 默认false
    cache: false

mybatis:
  #别名
  type-aliases-package: com.wang.pojo
  #下划线自动映射驼峰
  configuration:
    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  #指定sql映射文件的位置
  mapper-locations: classpath:/mapper/*.xml
server:
  port: 8081

多余文件可删可不删,使用templates需要导入<html lang="en" xmlns:th="http://www.thymeleaf.org">
创建项目的时候没勾选需要导templates包,也可以在创建项目的时候把包全勾选上

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.wang</groupId>
    <artifactId>springBoot-nba</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springBoot-nba</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
        </dependency>
        <!--分页插件PageHelper-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.3.23</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.3</version>
        </dependency>
        <!--thymeleaf-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application和yml配置

在这里插入图片描述数据库配置
在这里插入图片描述yml配置

数据库表

在这里插入图片描述表名clubs
在这里插入图片描述表名players

数据数据展示

在这里插入图片描述
在这里插入图片描述

pojo实体类

package com.wang.pojo;

import java.util.Date;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Players {
    private Integer pid;

    private String pname;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date birthday;

    private Integer height;

    private Integer weight;

    private String position;

    private Integer cid;

    @Autowired
    private Clubs clubs;
}
package com.wang.pojo;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Clubs {
    private Integer cid;

    private String cname;

    private String city;
}

mapper层(也就是dao层)

package com.wang.mapper;

import com.wang.pojo.Players;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface PlayersMapper {
    int deleteByPrimaryKey(Integer pid);

    int insert(Players record);

    List<Players> queryByName(@Param("name") String name, @Param("id") Integer id);

	Players selectByPrimaryKey(Integer pid);
	
	int updateByPrimaryKey(Players record);
}
package com.wang.mapper;

import com.wang.pojo.Clubs;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface ClubsMapper {
    List<Clubs> query();
}

mapper.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.wang.mapper.PlayersMapper">
  <resultMap id="BaseResultMap" type="com.wang.pojo.Players">
    <!--@mbg.generated-->
    <!--@Table players-->
    <id column="pid" jdbcType="INTEGER" property="pid" />
    <result column="pname" jdbcType="VARCHAR" property="pname" />
    <result column="birthday" jdbcType="DATE" property="birthday" />
    <result column="height" jdbcType="INTEGER" property="height" />
    <result column="weight" jdbcType="INTEGER" property="weight" />
    <result column="position" jdbcType="VARCHAR" property="position" />
    <result column="cid" jdbcType="INTEGER" property="cid" />
    <association property="clubs" javaType="com.wang.pojo.Clubs">
        <result column="name" property="cname"/>
    </association>
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    pid, pname, birthday, height, weight, `position`, cid
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    <!--@mbg.generated-->
    select 
    <include refid="Base_Column_List" />
    from players
    where pid = #{pid,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    <!--@mbg.generated-->
    delete from players
    where pid = #{pid,jdbcType=INTEGER}
  </delete>
  <insert id="insert" keyColumn="pid" keyProperty="pid" parameterType="com.wang.pojo.Players" useGeneratedKeys="true">
    <!--@mbg.generated-->
    insert into players (pname, birthday, height, 
      weight, `position`, cid
      )
    values (#{pname,jdbcType=VARCHAR}, #{birthday,jdbcType=DATE}, #{height,jdbcType=INTEGER}, 
      #{weight,jdbcType=INTEGER}, #{position,jdbcType=VARCHAR}, #{cid,jdbcType=INTEGER}
      )
  </insert>
  <insert id="insertSelective" keyColumn="pid" keyProperty="pid" parameterType="com.wang.pojo.Players" useGeneratedKeys="true">
    <!--@mbg.generated-->
    insert into players
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="pname != null">
        pname,
      </if>
      <if test="birthday != null">
        birthday,
      </if>
      <if test="height != null">
        height,
      </if>
      <if test="weight != null">
        weight,
      </if>
      <if test="position != null">
        `position`,
      </if>
      <if test="cid != null">
        cid,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="pname != null">
        #{pname,jdbcType=VARCHAR},
      </if>
      <if test="birthday != null">
        #{birthday,jdbcType=DATE},
      </if>
      <if test="height != null">
        #{height,jdbcType=INTEGER},
      </if>
      <if test="weight != null">
        #{weight,jdbcType=INTEGER},
      </if>
      <if test="position != null">
        #{position,jdbcType=VARCHAR},
      </if>
      <if test="cid != null">
        #{cid,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.wang.pojo.Players">
    <!--@mbg.generated-->
    update players
    <set>
      <if test="pname != null">
        pname = #{pname,jdbcType=VARCHAR},
      </if>
      <if test="birthday != null">
        birthday = #{birthday,jdbcType=DATE},
      </if>
      <if test="height != null">
        height = #{height,jdbcType=INTEGER},
      </if>
      <if test="weight != null">
        weight = #{weight,jdbcType=INTEGER},
      </if>
      <if test="position != null">
        `position` = #{position,jdbcType=VARCHAR},
      </if>
      <if test="cid != null">
        cid = #{cid,jdbcType=INTEGER},
      </if>
    </set>
    where pid = #{pid,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.wang.pojo.Players">
    <!--@mbg.generated-->
    update players
    set pname = #{pname,jdbcType=VARCHAR},
      birthday = #{birthday,jdbcType=DATE},
      height = #{height,jdbcType=INTEGER},
      weight = #{weight,jdbcType=INTEGER},
      `position` = #{position,jdbcType=VARCHAR},
      cid = #{cid,jdbcType=INTEGER}
    where pid = #{pid,jdbcType=INTEGER}
  </update>
  <select id="queryByName" resultMap="BaseResultMap">
        select p.pid,p.pname, p.birthday, p.height, p.weight, p.`position`, c.cname name FROM players p,clubs c
        <where>
         p.cid=c.cid
         <if test="name != null and name != ''">
            and pname like concat('%',#{name},'%')
         </if>
         <if test="id != null">
         and p.cid like concat('%',#{id},'%')
        </if>
        </where>
</select>
</mapper>
<?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.wang.mapper.ClubsMapper">
  <resultMap id="BaseResultMap" type="com.wang.pojo.Clubs">
    <!--@mbg.generated-->
    <!--@Table clubs-->
    <id column="cid" jdbcType="INTEGER" property="cid" />
    <result column="cname" jdbcType="VARCHAR" property="cname" />
    <result column="city" jdbcType="VARCHAR" property="city" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    cid, cname, city
  </sql>
  <select id="query" resultMap="BaseResultMap">
  select * from clubs;
</select>
</mapper>

service层

package com.wang.service;

import com.github.pagehelper.PageInfo;
import com.wang.pojo.Players;

public interface PlayersService{


    int deleteByPrimaryKey(Integer pid);

    int insert(Players record);

    PageInfo<Players> queryByName(String name,Integer id, Integer pageSize, Integer pageNum);

	Players selectByPrimaryKey(Integer pid);

	int updateByPrimaryKey(Players record);
}

package com.wang.service;

import com.wang.pojo.Clubs;

import java.util.List;

public interface ClubsService{

    List<Clubs> query();
}

service实现类

package com.wang.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wang.mapper.PlayersMapper;
import com.wang.pojo.Players;
import com.wang.service.PlayersService;
import org.springframework.stereotype.Service;

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

@Service
public class PlayersServiceImpl implements PlayersService {

    @Resource
    private PlayersMapper playersMapper;

    @Override
    public int deleteByPrimaryKey(Integer pid) {
        return playersMapper.deleteByPrimaryKey(pid);
    }

    @Override
    public int insert(Players record) {
        return playersMapper.insert(record);
    }

    @Override
    public PageInfo<Players> queryByName(String name, Integer id, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<Players> list = playersMapper.queryByName(name,id);
        PageInfo<Players> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
	
	 @Override
    public int updateByPrimaryKey(Players record) {
        return playersMapper.updateByPrimaryKey(record);
    }

	 @Override
    public Players selectByPrimaryKey(Integer pid) {
        return playersMapper.selectByPrimaryKey(pid);
    }    
}

package com.wang.service.Impl;

import com.wang.mapper.ClubsMapper;
import com.wang.pojo.Clubs;
import com.wang.service.ClubsService;
import org.springframework.stereotype.Service;

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

@Service
public class ClubsServiceImpl implements ClubsService {

    @Resource
    private ClubsMapper clubsMapper;

    @Override
    public List<Clubs> query() {
        return clubsMapper.query();
    }
}

controller层

package com.wang.controller;

import com.github.pagehelper.PageInfo;
import com.wang.pojo.Clubs;
import com.wang.pojo.Players;
import com.wang.service.Impl.ClubsServiceImpl;
import com.wang.service.Impl.PlayersServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

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

@Controller
@RequestMapping("/nba")
public class NbaController {

    @Autowired
    private PlayersServiceImpl playersService;

    @Autowired
    private ClubsServiceImpl clubsService;

//    @RequestMapping(value = "/")
//    public String login(){
//        return "allList";
//    }

    @RequestMapping("/ssm")
    public String list(String name, Integer id, @RequestParam(value = "pageSize", required = false, defaultValue = "2") Integer pageSize, @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum, Model model){
        PageInfo<Players> playersPageInfo = playersService.queryByName(name, id, pageSize, pageNum);
        List<Clubs> query = clubsService.query();
        model.addAttribute("query",query);
        model.addAttribute("name",name);
        model.addAttribute("id",id);
        model.addAttribute("playersPageInfo",playersPageInfo);
        return "allList";
    }

    @RequestMapping("/add")
    public String add(Model model){
        List<Clubs> query = clubsService.query();
        model.addAttribute("query",query);
        return "add";
    }

    @PostMapping("/addNba")
    public String addNba(Players players){
        playersService.insert(players);
        return "redirect:/nba/ssm";
    }

    @RequestMapping("/del")
    public String delNba(Integer id){
        playersService.deleteByPrimaryKey(id);
        return "redirect:/nba/ssm";
    }

    @RequestMapping("/upd")
    public String upd(Integer id,Model model){
        Players players = playersService.selectByPrimaryKey(id);
        model.addAttribute("list",players);
        List<Clubs> query = clubsService.query();
        model.addAttribute("query",query);
        return "upd";
    }

    @RequestMapping("/updNba")
    public String updNba(Players players){
        playersService.updateByPrimaryKey(players);
        return "redirect:/nba/ssm";
    }
}


config配置首页访问和分页展示

package com.wang.config;

import com.github.pagehelper.PageInterceptor;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;

import java.io.IOException;
import java.util.Properties;

@Configuration
public class MybatisConfig {

    @Bean
    public PageInterceptor pageInterceptor() {
        PageInterceptor pageInterceptor = new PageInterceptor();
        // 详见 com.github.pagehelper.page.PageParams
        Properties p = new Properties();
//        p.setProperty("offsetAsPageNum", "false");
//        p.setProperty("rowBoundsWithCount", "false");
//        p.setProperty("reasonable", "false");
        // 设置数据库方言 , 也可以不设置,会动态获取
        p.setProperty("helperDialect", "mysql");
        pageInterceptor.setProperties(p);
        return pageInterceptor;
    }

    @EventListener({ApplicationReadyEvent.class})
    void applicationReadyEvent() {
//        System.out.println("应用已经准备就绪 ... 启动浏览器");
        // 这里需要注url:端口号+测试类方法名
        String url = "http://localhost:8081/nba/ssm";
        Runtime runtime = Runtime.getRuntime();
        try {
            runtime.exec("rundll32 url.dll,FileProtocolHandler " + url);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

upd.html页面

<!DOCTYPE html>
<html lang="en">
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form th:action="@{/nba/updNba}"  th:method="post">
  <div style="width: 450px;height:100%; margin:0px auto">
    <h1 style="color:red">修改球员信息</h1>
    <table border="2px">
      <input type="hidden" th:name="pid" th:value="${list.pid}">
      <tr>
        <td>球员名称</td>
        <td><input type="text" th:name="pname" th:value="${list.pname}" required ></td>
      </tr>
      <tr>
        <td>出生日期</td>
        <td><input type="date" th:name="birthday" th:value="${#dates.format(list.birthday,'yyyy-MM-dd')}" required ></td>
      </tr>
      <tr>
        <td>球员身高</td>
        <td><input type="text" th:name="height" th:value="${list.height}" required ></td>
      </tr>
      <tr>
        <td>球员体重</td>
        <td><input type="text" th:name="weight" th:value="${list.weight}"></td>
      </tr>
      <tr>
        <td>球员位置</td>
        <td>
          <input type="radio" name="position" value="得分后卫" checked>得分后卫
          <input type="radio" name="position" value="控球后卫">控球后卫
          <input type="radio" name="position" value="大前锋">大前锋
          <input type="radio" name="position" value="小前锋">小前锋
          <input type="radio" name="position" value="中锋">中锋
        </td>
      </tr>
      <tr>
        <td>所属球队</td>
        <td>
          <select name="cid">
            <option th:each="q:${query}" th:value="${q.cid}" th:text="${q.cname}" ></option>
          </select>
        </td>
      </tr>
        <td>操作</td>
        <td><input th:type="submit" th:value="修改"><a th:href="@{/nba/ssm}" style="text-decoration: none"> 返回</a></td>
      </tr>
    </table>
  </div>
</form>
</body>
</html>

allList.html页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div style="width: 680px;height:100%; margin:0px auto">
  <h2>球员名单</h2>
  <p>
  <form th:action="@{/nba/ssm}" method="post">
    球员姓名:<input name="name" type="text"/>
    所属球队:
    <select name="id" id="id">
      <option selected disabled>请选择</option>
        <option th:value="${q.cid}" th:each="q:${query}" th:text="${q.cname}"></option>
    </select>
    <input type="submit" value="查询">
    <a type="button" th:href="@{/nba/add}" style="text-decoration: none">添加</a>
  </form>
  </p>
  <table border="2px">
    <tr>
      <th>球队编号</th>
      <th>球员名称</th>
      <th>出生日期</th>
      <th>球员身高</th>
      <th>球员体重</th>
      <th>球员位置</th>
      <th>所属球队</th>
      <th>相关操作</th>
    </tr>
    <!--这里的数据要从servlet里面获取-->
      <tr th:each="list:${playersPageInfo.list}">
        <td th:text="${list.pid}"></td>
        <td th:text="${list.pname}"></td>
        <td th:text="${#dates.format(list.birthday,'yyyy-MM-dd')}"></td>
        <td th:text="${list.height}"></td>
        <td th:text="${list.weight}"></td>
        <td th:text="${list.position}"></td>
        <td th:text="${list.clubs.cname}">
        </td>
        <td>
        <a th:href="@{/nba/del/(id=${list.pid})}">删除</a>
        <a th:href="@{/nba/upd/(id=${list.pid})}">修改</a>
        </td>
      </tr>
    <tr align="center">
      <td colspan="4">
          <a th:if="${playersPageInfo.hasPreviousPage}" th:href="@{/nba/ssm(pageNum=1,name=${name},id=${id})}">首页</a>
          <a th:if="${playersPageInfo.hasPreviousPage}" th:href="@{/nba/ssm(pageNum=${playersPageInfo.prePage},name=${name},id=${id})}">上一页</a>
          <a th:if="${playersPageInfo.hasNextPage}" th:href="@{/nba/ssm(pageNum=${playersPageInfo.nextPage},name=${name},id=${id})}">下一页</a>
          <a th:if="${playersPageInfo.hasNextPage}" th:href="@{/nba/ssm(pageNum=${playersPageInfo.pages},name=${name},id=${id})}">尾页</a>
      </td>
    </tr>
  </table><span th:text="${playersPageInfo.pageNum}"/>页
  共<span th:text="${playersPageInfo.pages}"/></div>
</body>
</html>

add.html页面

<!DOCTYPE html>
<html lang="en">
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form method="post" th:action="@{/nba/addNba}">
  <div style="width: 450px;height:100%; margin:0px auto">
    <h1 style="color: aquamarine">球员新增</h1>
    <table border="2px">
      <tr>
        <td>球员姓名</td>
        <td><input type="text" name="pname"  required></td>
      </tr>
      <tr>
        <td>出生日期</td>
        <td><input type="date" name="birthday" required></td>
      </tr>
      <tr>
        <td>球员身高</td>
        <td><input type="text" name="height" required></td>
      </tr>
      <tr>
        <td>球员体重</td>
        <td>
          <input type="text" name="weight"/>
        </td>
      </tr>
      <tr>
        <td>球员位置</td>
        <td>
          <input type="radio" name="position" checked value="得分后卫">得分后卫
          <input type="radio" name="position" value="控球后卫">控球后卫
          <input type="radio" name="position" value="大前锋">大前锋
          <input type="radio" name="position" value="小前锋">小前锋
          <input type="radio" name="position" value="中锋">中锋
        </td>
      </tr>
      <tr>
        <td>所属球队</td>
        <td>
          <select name="cid">
            <option selected disabled>请选择</option>
             <option th:each="q:${query}" th:value="${q.cid}" th:text="${q.cname}"></option>
         </select>
</td>
</tr>
<tr>
  <td>相关操作</td>
  <td><input type="submit" value="新增"/><a type="button" th:href="@{/nba/ssm}" style="text-decoration: none"> 返回</a></td>
</tr>
</table>
</div>
</form>
</body>
</html>

同步新增数据

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
    $("#nba").on('click',function(){
        $.ajax({
            type: 'post',
            data: $('#form').serialize(),
            url: '${pageContext.request.contextPath}/nba/addNba',
            dataType : 'text',
            success: function (data) {
                if(data =='yes'){
                    window.location.href="${pageContext.request.contextPath}"
                }
            }
        })
    })
</script>

异步新增数据
使用ajax需要导一个jquery包也可以导在线的包

给form表单绑定一个id,给按钮绑定一个id,切记ajax请求要放到body下面不然不会生效
在这里插入图片描述

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值