前端:html+css+jvavscript vue框架 + ajax + axios + element ui
后端:jvav + mybatis框架 + mysql数据库
项目管理工具:maven
服务器:Tomcat
相关功能演示
首先启动Tomcat服务器
进入页面
调整每页显示数量:
页面跳转:
添加数据:
删除数据:
条件查询:
条件查询下的页面切换:
前端源码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.el-table .warning-row {
background: oldlace;
}
.el-table .success-row {
background: #f0f9eb;
}
</style>
</head>
<body>
<div id="app">
<!-- 搜索框-->
<el-form :inline="true" :model="brand" class="demo-form-inline">
<el-form-item label="当前状态">
<el-select v-model="brand.status" placeholder="当前状态">
<el-option label="启用" value="1"></el-option>
<el-option label="禁用" value="0"></el-option>
<el-option label="当前状态" value=""></el-option>
</el-select>
</el-form-item>
<el-form-item label="企业名称">
<el-input v-model="brand.company" placeholder="企业名称"></el-input>
</el-form-item>
<el-form-item label="品牌名称">
<el-input v-model="brand.name" placeholder="品牌名称"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="onSubmit">查询</el-button>
</el-form-item>
</el-form>
<el-button type="danger" plain @click="del">删除</el-button>
<el-button type="primary" plain @click="dialogVisible=true">添加</el-button>
<!-- 添加 -- 对话框-->
<el-dialog
title="提示"
:visible.sync="dialogVisible"
width="30%"
>
<!-- 表单内容-->
<el-form ref="form" :model="brand" label-width="80px">
<el-form-item label="品牌名称">
<el-input v-model="brand.name"></el-input>
</el-form-item>
<el-form-item label="企业名称">
<el-input v-model="brand.company"></el-input>
</el-form-item>
<el-form-item label="排序">
<el-input v-model="brand.ordered"></el-input>
</el-form-item>
<el-form-item label="品牌介绍">
<el-input type="textarea" v-model="brand.description"></el-input>
</el-form-item>
<el-form-item label="状态">
<el-switch v-model="brand.status"
active-value="1"
inactive-value="0"
></el-switch>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="addBrand">立即创建</el-button>
<el-button @click="dialogVisible=false">取消</el-button>
</el-form-item>
</el-form>
</el-dialog>
<!-- 表格-->
<template>
<el-table
:data="tableData"
style="width: 100%"
:row-class-name="tableRowClassName"
@selection-change="handleSelectionChange"
>
<el-table-column
type="selection"
width="55">
</el-table-column>
<el-table-column
type="index"
width="50"
>
</el-table-column>
<el-table-column
prop="name"
label="品牌名称"
align="center"
>
</el-table-column>
<el-table-column
prop="company"
label="企业名称"
align="center"
>
</el-table-column>
<el-table-column
prop="ordered"
label="排序"
align="center"
>
</el-table-column>
<el-table-column
prop="statusStr"
label="当前状态"
align="center">
</el-table-column>
<el-table-column
label="操作"
align="center"
>
<el-button type="primary">修改</el-button>
<el-button type="danger" @click="dell">删除</el-button>
</el-table-column>
</el-table>
</template>
<!-- 分页工具条-->
<el-pagination
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page="currentPage"
:page-sizes="[5, 10, 15, 20]"
:page-size="5"
layout="total, sizes, prev, pager, next, jumper"
:total="total">
</el-pagination>
</div>
<script src="js/axios-0.18.0.js"></script>
<script src="js/vue.js"></script>
<script src="element-ui/lib/index.js"></script>
<link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
<script>
new Vue({
el: "#app",
mounted() {
this.select()
},
data() {
return {
//第 ? 页
currentPage: 1,
//每页 ? 条
pageSize: 5,
//页数
total: 100,
//选中的数组
ids: [],
//是否显示对话框
dialogVisible: false,
//搜索表单数据
brand: {
id: '',
status: '',
company: '',
name: '',
ordered: '',
description: '',
},
//选择框数据
multipleSelection: [],
//表格数据
tableData: [{
id: '',
status: '',
company: '',
name: '',
ordered: '',
description: '',
}]
}
},
methods: {
dell() {
//message: this.value
//????????????????????????
},
del() {
_this = this
//二次确认
this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
for (let i = 0; i < this.multipleSelection.length; i++) {
this.ids[i] = this.multipleSelection[i].id
}
//console.log(this.ids);
axios({
method: "post",
url: "http://localhost/Brand/delServlet",
data: _this.ids
}).then(function (resp) {
if (resp.data == "success") {
_this.select()
_this.$message({
message: '删除成功',
type: 'warning'
});
}
})
}).catch(() => {
this.$message({
type: 'info',
message: '已取消删除'
});
});
},
/*select() {
_this = this
axios({
method: "get",
url: "http://localhost/Brand/selectServlet",
}).then(function (resp) {
_this.tableData = resp.data
})
},*/
select() {
_this = this
if (this.brand.status == "" && this.brand.company == "" && this.brand.name == "") {
//console.log(true);
str = "http://localhost/Brand/selectCountServlet?currentPage=" + _this.currentPage + "&pageSize=" + _this.pageSize
} else {
//console.log(false);
str = "http://localhost/Brand/selectxServlet?currentPage=" + _this.currentPage + "&pageSize=" + _this.pageSize
}
console.log(this.brand.status);
axios({
method: "post",
url: str,
data: _this.brand,
}).then(function (resp) {
_this.tableData = resp.data.rows
_this.total = resp.data.total
})
},
tableRowClassName({row, rowIndex}) {
if (rowIndex % 4 == 1) {
return 'warning-row';
} else if (rowIndex % 4 == 3) {
return 'success-row';
}
return '';
},
handleSelectionChange(val) {
console.log(this.multipleSelection)
this.multipleSelection = val;
},
onSubmit() {
this.currentPage = 1
this.select();
},
addBrand() {
//发送ajax请求
_this = this
axios({
method: "post",
url: "http://localhost/Brand/addServlet",
data: _this.brand
}).then(function (resp) {
if (resp.data == "success") {
_this.dialogVisible = false
_this.select()
_this.$message({
message: '添加成功',
type: 'success'
});
_this.brand.status = ""
_this.brand.company = ""
_this.brand.name = ""
}
})
},
handleSizeChange(val) {
//console.log(`每页 ${val} 条`);
this.pageSize = val
this.select()
},
handleCurrentChange(val) {
//console.log(`当前页: ${val}`);
this.currentPage = val
this.select()
},
},
})
</script>
</body>
</html>
后端源码(部分):
Dao层
mapper(实现对数据库的操作)
package com.company.web.mapper;
import com.company.web.pojo.Brand;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface BrandMapper {
@Select("select * from brand")
List<Brand> select();
@Insert("insert into brand values(null,#{name },#{company},#{ordered},#{description},#{status})")
void add(Brand brand);
//@Delete("delete from brand where ??????")
void del(@Param("ids") int[] ids);
@Select("select * from brand limit #{begin},#{size}")
List<Brand> selectByPage(@Param("begin") int begin, @Param("size") int size);
@Select("select count(*) from brand")
int selectCount();
//List<Brand> selectx(@Param("status")int status,@Param("name") String name,@Param("company") String company,@Param("begin")int begin, @Param("size")int size);
List<Brand> selectx(@Param("brand")Brand brand, @Param("begin") int begin, @Param("size") int size);
//@Select("select count(*) from brand where ???")
int selectCountx(Brand brand);
}
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.company.web.mapper.BrandMapper">
<resultMap id="brandResultMap" type="com.company.web.pojo.Brand"></resultMap>
<delete id="del">
delete
from brand
where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
<select id="selectx" resultType="com.company.web.pojo.Brand">
select *
from brand
<where>
<if test="brand.status != null">
and status = #{brand.status}
</if>
<if test="brand.name != null and brand.name !=''">
and name like #{brand.name}
</if>
<if test="brand.company != null and brand.company !=''">
and company like #{brand.company}
</if>
</where>
limit #{begin},#{size}
</select>
<select id="selectCountx" resultType="java.lang.Integer">
select count(*)
from brand
<where>
<if test="status != null">
and status = #{status}
</if>
<if test="name != null and name !=''">
and name like #{name}
</if>
<if test="company != null and company !=''">
and company like #{company}
</if>
</where>
</select>
</mapper>
service层(封装操作数据库的方法)
package com.company.web.server;
import com.company.web.pojo.Brand;
import com.company.web.mapper.BrandMapper;
import com.company.web.pojo.PageBean;
import com.company.web.servlet.SSF;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;
public class BrandServer {
public List<Brand> select() {
//1.加载mybatis核心配置文件
SqlSessionFactory sqlSessionFactory = SSF.factory();
//2.获取SqlSession对象,用它执行sql
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.执行sql
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
List<Brand> list = brandMapper.select();
sqlSession.close();
return list;
}
public PageBean selectx(Brand brand, int currentPage, int pageSize) {
//1.加载mybatis核心配置文件
SqlSessionFactory sqlSessionFactory = SSF.factory();
//2.获取SqlSession对象,用它执行sql
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.执行sql
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
String name = brand.getName();
if (name != null && name.length() > 0) {
brand.setName("%" + name + "%");
}
String company = brand.getCompany();
if (company != null && company.length() > 0) {
brand.setCompany("%" + company + "%");
}
if (brand.getStatus() == 0) {
brand.setStatus(null);
}
int begin = (currentPage - 1) * pageSize;
int size = pageSize;
List<Brand> list = brandMapper.selectx(brand, begin, size);
int countx = brandMapper.selectCountx(brand);
PageBean pageBean = new PageBean(list, countx);
sqlSession.close();
return pageBean;
}
public PageBean<Brand> selectByPage(int currentPage, int pageSize) {
//1.加载mybatis核心配置文件
SqlSessionFactory sqlSessionFactory = SSF.factory();
//2.获取SqlSession对象,用它执行sql
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.执行sql
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
int begin = (currentPage - 1) * pageSize;
int size = pageSize;
int count = brandMapper.selectCount();
List<Brand> list = brandMapper.selectByPage(begin, size);
PageBean pageBean = new PageBean(list, count);
sqlSession.close();
return pageBean;
}
public void add(Brand brand) {
//1.加载mybatis核心配置文件
SqlSessionFactory sqlSessionFactory = SSF.factory();
//2.获取SqlSession对象,用它执行sql
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.执行sql
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
brandMapper.add(brand);
sqlSession.commit();
sqlSession.close();
}
public void del(int[] ids) {
//1.加载mybatis核心配置文件
SqlSessionFactory sqlSessionFactory = SSF.factory();
//2.获取SqlSession对象,用它执行sql
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.执行sql
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
brandMapper.del(ids);
sqlSession.commit();
sqlSession.close();
}
}
servlet层(响应请求,调用service的方法)
package com.company.web.servlet;
import com.alibaba.fastjson.JSON;
import com.company.web.pojo.Brand;
import com.company.web.pojo.PageBean;
import com.company.web.server.BrandServer;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
@WebServlet(urlPatterns = {"/selectxServlet"})
public class SelectxServlet extends HttpServlet {
BrandServer brandServer = new BrandServer();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String _CurrentPage = req.getParameter("currentPage");
String _PageSize= req.getParameter("pageSize");
int currentPage = Integer.parseInt(_CurrentPage);
int pageSize = Integer.parseInt(_PageSize);
BufferedReader reader = req.getReader();
String json = reader.readLine();
Brand brand = JSON.parseObject(json,Brand.class);
PageBean pageBean = brandServer.selectx(brand,currentPage,pageSize);
json = JSON.toJSONString(pageBean);
resp.setContentType("text/json;charset=utf8");
resp.getWriter().write(json);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
doGet(req, resp);
}
}
至此,已完成对jvav se 、jvav sql、jvav web的学习,下面将开启spring篇
完整源码过于啰嗦,这里就不放了,有需要者欢迎评论/私信获取
世界线回溯,从jvav到架构师