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

JSD-2204-SpringBoot+MyBatis-Day12

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

JSD-2204-SpringBoot+MyBatis-Day12

1.SpringBoot+MyBatis 1.1MyBatis框架
  • 此框架是目前最流行的数据持久层框架,框架可以帮助我们生成JDBC代码, 从而提高开发效率, 使用此框架程序员只需要通过注解或xml配置文件的方式提供需要执行的SQL语句, Mybatis框架会自动根据SQL语句生成对应的JDBC代码.
  • 如何使用MyBatis框架?
  • 创建SpringBoot工程, 打钩时选择
  • Spring Web
  • Mybatis Framework
  • MySql Driver
  • 创建完工程后 需要在application.properties配置文件中添加连接数据库的信息 否则工程无法正常启动 会报以下错误

1.2商品管理(商品增删改查)和用户登录注册
  • 准备工作: 创建表
    • use empdb;
    • create table product(id int primary key auto_increment,title varchar(100),price double(8,2),num int);
  • 创建SpringBoot工程, 打3个√
  • 创建完工程后,在application.properties里面添加连接数据库的信息

1.2.1添加商品
  • 创建首页index.html 里面添加超链接
  • 创建insert.html页面 里面准备form表单 提交地址为/insert
  • 创建ProductController, 在里面添加insert方法处理/insert请求,创建Product实体类, 在insert方法参数列表处声明Product 用来接收传递过来的参数,
  • 创建ProductMapper,并在里面提供insert方法
  • 在ProductController里面吧ProductMapper 自动装配进来, 然后在insert方法中调用mapper的insert方法 给客户端响应添加完成
1.2.2商品列表
  • 在index.html页面中添加 超链接 地址为/select
  • 在ProductController添加select方法处理/select请求, 方法中调用mapper的select方法,得到装着商品对象的list集合 , 创建一个装html标签的字符串, 里面准备一个table表格(此处代码可以复制粘贴) 最后将html字符串返回给客户端
  • 实现mapper里面的select方法
1.2.3删除商品
  • 在现实商品的表格中添加 删除的超链接,请求地址为/delete?id=xx
  • 在ProductController添加delete方法处理/delete请求, 在方法中调用mapper的deleteById方法 给客户端响应删除完成.
  • 实现mapper中的deleteById方法
1.2.4修改商品
  • 在首页index.html页面中添加 修改商品的超链接 地址为/update.html
  • 创建update.html页面, 页面中准备一个form表单 提交地址为/update
  • 在ProductController里面添加update方法处理/update请求, 在方法中调用mapper的update方法,最后给客户端响应 修改完成.
  • 实现mapper里面的update方法.
1.2.5注册业务
  • 在首页index.html页面中添加注册的超连接,地址为/reg.html
  • 创建reg.html页面,页面中准备一个form表单 提交地址为/reg
  • 创建User对象用来存储用户注册的数据
  • 创建UserController用来处理客户端发送过来的请求,并且添加处理/reg请求的方法
  • 创建UserMapping并在里面提供reg方法和selectByUser方法并且返回一个User对象
1.2.6登录业务
  • 在首页index.html页面中添加登录的超链接 地址为/login.html
  • 创建login.html页面,页面中准备一个form表单 提交地址为/login
  • 在UserController中添加处理/login请求的方法
  • 在UserMapping中使用上诉添加的selectByUser方法
1.4代码目录展示

1.3上诉业务代码展示 1.3.1ProductController
package cn.tedu.boot2.controller;

import cn.tedu.boot2.entity.Product;
import cn.tedu.boot2.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//此注解相当于在Controller注解的基础之上,在每个方法的上面都加上了@ResponseBody注解
@RestController
public class ProductController {
    //自动装配注解, 是Spring框架中提供的注解,添加了此注解后Mybatis框架会根据此接口
    //创建实现类,并且实例化了此实现类,此时mapper变量已经有值可以调用里面的方法
    @Autowired
    ProductMapper mapper;

    @RequestMapping("/insert")
    public String insert(Product product){
        System.out.println("product = " + product);
        mapper.insert(product);
        return "添加完成!";
    }

    @RequestMapping("/select")
    public String select(){
        List list = mapper.select();
       
        String html = "";
        html+="";
        html+="";
        //遍历集合中商品的信息 然后往表格标签里面添加tr和td
        for (Product p: list) {
            html+="";
            html+="";
            html+="";
            html+="";
            html+="";
            html+="";
            html+="";
        }
        html+="
商品列表
id标题价格库存操作
"+p.getId()+""+p.getTitle()+""+p.getPrice()+""+p.getNum()+"删除
"; return html; } @RequestMapping("/delete") public String delete(int id){ mapper.deleteById(id); return "删除完成!返回列表页面"; } @RequestMapping("/update") public String update(Product product){ mapper.update(product); return "修改完成!返回列表页面"; } } @RequestMapping("/insert") public String insert(Product product){ System.out.println("product = " + product); mapper.insert(product); return "添加完成!"; } @RequestMapping("/select") public String select(){ List list = mapper.select(); String html = ""; html +=""; html += " "; for (Product product : list) { html += "" + ""; } html += "
商品列表
编号标题价格库存操作
" + product.getId() + "" + product.getTitle() + "" + product.getPrice() + "" + product.getNum() + "删除
"; return html; } @RequestMapping("/delete") public String delete(int id){ System.out.println("id = " + id); mapper.delete(id); return "删除成功"; } @RequestMapping("/update") public String update(Product product){ mapper.update(product); return "修改成功"; }
1.3.2UserController
package cn.tedu.boot2.controller;

import cn.tedu.boot2.entity.User;
import cn.tedu.boot2.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    UserMapper userMapper;

    @RequestMapping("/reg")
    public String reg(User user){
        System.out.println("user = " + user);
        //通过用户输入的用户名查询过出用户信息
        User u = userMapper.selectByUser(user.getUsername());
        //判断是否查询到了用户信息
        if (u!=null){
            return "用户名已存在";
        }
        userMapper.reg(user);
        return "注册成功";
    }

    @RequestMapping("/login")
    public String login(User user){
        System.out.println("user = " + user);
        User u = userMapper.login(user.getUsername());
        if (u!=null){
            if (u.getPassword().equals(user.getPassword())){
                return "登录成功";
            }
            return "密码错误";
        }
        return "该用户不存在";
    }

}
1.3.3Product
package cn.tedu.boot2.entity;

public class Product {
    private Integer id;
    private String title;
    private Double price;
    private Integer num;

    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", title='" + title + ''' +
                ", price=" + price +
                ", num=" + num +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }
}
1.3.4User
package cn.tedu.boot2.entity;

public class User {
    private Integer id;
    private String username;
    private String password;
    private String nick;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + ''' +
                ", password='" + password + ''' +
                ", nick='" + nick + ''' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getNick() {
        return nick;
    }

    public void setNick(String nick) {
        this.nick = nick;
    }
}
1.3.5ProductMapper
package cn.tedu.boot2.mapper;

import cn.tedu.boot2.entity.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProductMapper {

    //#{变量名} 这种写法会自动找到下面方法中参数列表里面的同名变量
    //如果没有同名变量则会调用参数列表中对象的get方法
    @Insert("insert into product values(null,#{title},#{price},#{num})")
    void insert(Product product);

    //查询到的数据会自动封装到Product对象 并且吧叉形到的多个对象装进list集合里面
    @Select("select * from product")
    List select();

    @Delete("delete  product where id = #{id}")
    void delete(int id);

    @Update("update product set title = #{title} , price = #{price} , num = #{num} where id = #{id}")
    void update(Product product);
}
1.3.6UserMapper
package cn.tedu.boot2.mapper;

import cn.tedu.boot2.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface UserMapper {
    //如果查询感到的数据是一条数据可以通过一个自定义对象接收,如果查询过到的是多数据通过一个
    //自定义对象接收时会报错
    @Select("select * from user where username = #{username}")
    User selectByUser(String username);

    @Insert("insert into user values(null,#{username},#{password},#{nick})")
    void reg(User user);

    @Select("select password from user where username = #{username}")
    User login(String username);

}
1.3.7index.html



    
    Title


    商品管理系统
    添加商品
    商品列表
    修改商品
    登录注册
    注册
    登录

1.3.8insert.html



    
    Title


    添加商品
    
1.3.9login.html



    
    Title


登录
1.3.10reg.html



    
    Title


注册
1.3.11update.html



    
    Title


    修改商品
    

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

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

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