4.generatorConfig.xml设置
(1)修改jar包位置 ,更换成本地maven仓库中mysql数据库jdbc驱动jar包的位置
修改classPathEntry配置,更换成本地maven仓库中mysql数据库jdbc驱动jar包的位置
修改table配置表信息(tableName和domainObjectName),更换成对应数据库表
点击Edit Configurations...配置,添加Maven,输入命名:mybatis-gerenator:gerenate -e
(2)修改你要生成的表名及实体类名字(生成完之后记得注释)
要使用mybatisGenrtator 进行生成(使用完记得切换)
实体类添加相关注解
package com.zking.spboot.model;
import lombok.ToString;
@ToString
public class Book {
private Integer id;
private String bookname;
private Float price;
private String booktype;
public Book(Integer id, String bookname, Float price, String booktype) {
this.id = id;
this.bookname = bookname;
this.price = price;
this.booktype = booktype;
}
public Book() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public String getBooktype() {
return booktype;
}
public void setBooktype(String booktype) {
this.booktype = booktype;
}
}
5.application.yml设置
查看端口号,访问路径名。
修改账号密码
以上步骤完成,先将web项目发布至tomcat并启动,如果首页访问成功,表示web项目部署成功,可以开始编写后台代码了。
6.mapper层
添加注解,写一个模糊查询的方法
package com.zking.spboot.mapper;
import com.zking.spboot.model.Book;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface BookMapper {
int insert(Book record);
List all(Book book);
int insertSelective(Book record);
Book selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(Book record);
int updateByPrimaryKey(Book record);
}
7.service层
可以复制mapper层,只留下需要的方法
创建iml,添加相关注解,及返回结果
package com.zking.spboot.service.impl;
import com.zking.spboot.mapper.BookMapper;
import com.zking.spboot.model.Book;
import com.zking.spboot.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookServiceImpl implements IBookService {
@Autowired
private BookMapper bookMapper;
@Override
public int insert(Book record) {
return bookMapper.insert(record);
}
@Override
public List all(Book book) {
return bookMapper.all(book);
}
}
8.controller层
package com.zking.spboot.controller;
import com.zking.spboot.model.Book;
import com.zking.spboot.service.IBookService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
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;
import java.util.Map;
@RestController
@RequestMapping("/book")
public class BookController {
@Autowired
private IBookService bookService;
@RequestMapping("/add")
public ResponseData> add(Book book){
try {
bookService.insert(book);
return new ResponseData<>(200,"新增成功",null);
} catch (Exception e) {
e.printStackTrace();
return new ResponseData<>(500,"新增失败",null);
}
}
@RequestMapping("/all")
public ResponseData> all(Book book){
try {
List books =bookService.all(book);
bookService.insert(book);
return new ResponseData<>(200,"ok",books);
} catch (Exception e) {
e.printStackTrace();
return new ResponseData<>(500,"c查询书本信息失败",null);
}
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
class ResponseData{
private int code;
private String msg;
private T data;
}
前端 1.导入项目 2.下载依赖
如果出现以下报错,输入npm cache clear --force并重试
依赖下载完成后输入 npm dev run 启动项目
3.找到action.js浏览器 访问效果
1.这里的服务器地址与前端的必须一致
2.把后端方法添加到前端接口
element官网
Element - The world's most popular Vue UI framework
4.找到 BookList开始写前端方法
模糊查询方法如下:
SpringBoot阶段机试,ts={{ts}}
查询
运行效果图
添加新增按钮
SpringBoot阶段机试,ts={{ts}}
查询 新增 取 消 确 定



