后端
根据mysql数据库
jdbc.properties
generatorConfig.xml
BookMapper
package com.zking.ssm.mapper;
import com.zking.ssm.model.Book;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface BookMapper {
int deleteByPrimaryKey(Integer id);
int insert(Book record);
int insertSelective(Book record);
Book selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(Book record);
int updateByPrimaryKey(Book record);
List<Book> queryBook(Book book);
}
BookMapper.xml
BookServiceImpl
package com.zking.ssm.service.impl;
import com.zking.ssm.mapper.BookMapper;
import com.zking.ssm.model.Book;
import com.zking.ssm.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.xml.ws.soap.Addressing;
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<Book> queryBook(Book book) {
return bookMapper.queryBook(book);
}
}
IBookService
package com.zking.ssm.service;
import com.zking.ssm.model.Book;
import org.springframework.stereotype.Repository;
import java.util.List;
public interface IBookService {
int insert(Book record);
List<Book> queryBook(Book book);
}
BookController
package com.zking.ssm.controller;
import com.zking.ssm.model.Book;
import com.zking.ssm.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/book")
public class BookController {
@Autowired
private IBookService bookService;
@RequestMapping("/addBook")
public Map<String,Object> addBook(Book book){
bookService.insert(book);
return toJson(null);
}
@RequestMapping("/queryBook")
public Map<String,Object> queryBook(Book book){
List<Book> books = bookService.queryBook(book);
return toJson(books);
}
public Map<String,Object> toJson(Object data){
Map<String,Object> json=new HashMap<>();
json.put("success",true);
json.put("msg","OK");
json.put("data",data);
return json;
}
}
前端
npm install
npm run dev
src/api/action.js
根据BookController写
'ADD':'/book/addBook',
'QUERY':'/book/queryBook',
src/views/BookList.vue
<template>
<div>
<h1 align="center">SSM阶段机试,ts={{ts}}</h1>
<!-- 搜索栏 -->
<el-form :inline="true">
<el-form-item>
<el-input v-model="bookname" placeholder="请输入书本名称"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="query">查询</el-button>
<el-button type="success" @click="open">新增</el-button>
</el-form-item>
</el-form>
<!-- 数据表格 -->
<el-table :data="tableData" style="width: 100%">
<el-table-column prop="id" label="编号">
</el-table-column>
<el-table-column prop="bookname" label="名称">
</el-table-column>
<el-table-column prop="price" label="价格">
</el-table-column>
<el-table-column prop="booktype" label="类型">
</el-table-column>
</el-table>
<!-- 弹出框 -->
<el-dialog title="新增" :visible.sync="dialogFormVisible">
<el-form :model="ff" :rules="rules" ref="ff" label-width="100px" class="demo-ruleForm">
<el-form-item label="名称" prop="bookname">
<el-input v-model="ff.bookname"></el-input>
</el-form-item>
<el-form-item label="价格" prop="price">
<el-input v-model="ff.price"></el-input>
</el-form-item>
<el-form-item label="类型" prop="booktype">
<el-input v-model="ff.booktype"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogFormVisible = false">取 消</el-button>
<el-button type="primary" @click="add">确 定</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
export default {
data: function() {
return {
ts: new Date().getTime(),
bookname: '',
tableData: [],
dialogFormVisible: false,
ff: {
bookname: '',
price: '',
booktype: ''
},
rules: {
bookname: [{
required: true,
message: '请输入书本名称',
trigger: 'blur'
}],
price: [{
required: true,
message: '请输入书本价格',
trigger: 'blur'
}],
booktype: [{
required: true,
message: '请输入书本类型',
trigger: 'blur'
}],
}
};
},
methods: {
query: function() {
//请求参数
let params = {
bookname: this.bookname
};
//请求URL
let url = this.axios.urls.QUERY;
//发起请求
this.axios.post(url, params).then(resp => {
console.log(resp.data);
this.tableData = resp.data.data;
}).catch(err => {
})
},
open: function() {
this.dialogFormVisible = true;
this.ff={
bookname: '',
price: '',
booktype: ''
}
},
add: function() {
this.$refs['ff'].validate((valid) => {
if (valid) {
let url = this.axios.urls.ADD;
this.axios.post(url, this.ff).then(resp => {
let data = resp.data;
if (data.success) {
this.$message({
message: '成功',
type: 'success'
});
//关闭对话框
this.dialogFormVisible = false;
//刷新列表
this.query();
}
}).catch(err => {
})
} else {
console.log('error submit!!');
return false;
}
});
}
}
}
</script>
<style>
</style>
界面
点击查询就会出下面内容
模糊查询
增加
增加成功