SpringBoot 综合案例day-05

部门管理小程序

需求:部门信息的增删查改操作、分页浏览

架构:前后分离模式(后台restful服务、前台界面)

技术:后台(SpringBoot、SpringMVC、SpringIOC、Mybatis、restful)

           前台(html、jquery、ajax)

后台开发

1.添加

/dept/post        POST

2.删除

/dept/remove  POST

3.更新

/dept/put          POST

4.查询

/dept/get          GET

5.列表

/dept/list           GET

dept-service 后端代码层编写

使用的jar包,最后的devtools用于热启动方便自动重载。

1.实体类

package cn.xdl.entity;

import java.io.Serializable;

public class Dept implements Serializable{
	private Integer deptno;
	private String dname;
	private String loc;
	public Integer getDeptno() {
		return deptno;
	}
	public void setDeptno(Integer deptno) {
		this.deptno = deptno;
	}
	public String getDname() {
		return dname;
	}
	public void setDname(String dname) {
		this.dname = dname;
	}
	public String getLoc() {
		return loc;
	}
	public void setLoc(String loc) {
		this.loc = loc;
	}
	
}

2.deptDao接口

package cn.xdl.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import cn.xdl.entity.Dept;

public interface DeptDao {
	
	@Select("select * from dept order by deptno")
	public List<Dept> findAll();
	
	@Select("select * from dept where deptno=#{id}")
	public Dept findBuId(int id);
	
	@Insert("insert into dept(deptno,dname,loc) values(dept_seq.nextval,#{dname},#{loc})")
	public int save(Dept dept);
	
	@Update("update dept set dname=#{dname},loc=#{loc} where deptno=#{deptno}")
	public int update(Dept dept);
	
	@Delete("delete from dept where deptno=#{deptno}")
	public int delete(int id);
}

3.主启动类

@SpringBootApplication
@MapperScan(basePackages="cn.xdl.dao")
@ServletComponentScan
public class MyBootApplication {

	public static void main(String[] args) {
		SpringApplication.run(MyBootApplication.class, args);
	}	

}

4.application.yml文件配置数据库参数。注意层级关系和冒号后的空格

server:
 port: 8888
spring:
 datasource:
  username: scott
  password: 123456
  url: jdbc:oracle:thin:@127.0.0.1:1521:MLDN
  driverClassName: oracle.jdbc.OracleDriver

5.编写一个测试类测试mybatis连接正常

@RunWith(SpringRunner.class)
@SpringBootTest(classes={MyBootApplication.class})
public class TestDeptDao {
	@Autowired
	private DeptDao deptDao;
	
	@Test
	public void test1(){
		List<Dept> list = deptDao.findAll();
		for (Dept dept : list) {
			System.out.println(dept.getDeptno()+" "+dept.getDname());
		}
	}
}

6.运行测试类可以在eclipse后台查看到打印的所有结果。

7.编写Controller

package cn.xdl.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;

import cn.xdl.dao.DeptDao;
import cn.xdl.entity.Dept;
@RestController
@Transactional //事务管理
public class DeptController {
	@Autowired
	private DeptDao deptDao;
	
	// /dept/list 默认查询第1页,5条 
	@GetMapping("/dept/list")//dept/list?page=xx&pageSize=xx
	public List<Dept> list(
			@RequestParam(value="page",required=false,defaultValue="1")int page,
			@RequestParam(value="pageSize",required=false,defaultValue="5")int pageSize){
		PageHelper.startPage(page,pageSize);
		List<Dept> list = deptDao.findAll();
		return list;
	}
	@GetMapping("/dept/get") // /dept/get?id=xx
	public Dept get(Integer id) {
		if (id==null) {
			return null;
		}
		Dept dept = deptDao.findBuId(id);
		return dept;
	}
	@PostMapping("/dept/post")
	public int post(Dept dept) {
		return deptDao.save(dept);
	}
	
	@PostMapping("/dept/put")
	public int put(Dept dept) {
		return deptDao.update(dept);
	}
	
	@PostMapping("/dept/remove")
	public int remove(Integer id) {
		return deptDao.delete(id);
	}
}

8.运行主启动类,浏览器输入http://localhost:8888/dept/list等进行组件测试。

至此,后台开发完毕。

前台开发

建一个maven项目或者动态web项目,此处建立的是动态web项目。dept-ui

WebContent新建js文件夹引入jquery.js文件。

创建一个list.html页面文件使用ajax进行前后端交互。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
var page = 1;
	$(function(){
		list();
	})
	function nextPage(){
		page++;
		list(page,5);
	}
	function prePage(){
		page--;
		list(page,5);
	}
	//删除数据
	function del(deptno){
		$.ajax({
			url:"http://localhost:8888/dept/remove",
			type:"post",
			data:{"id":deptno},
			dataType:"json",
			success:function(result){
				if (result==1) {
					alert("删除成功");
					list();//刷新列表
				} else {
					alert("删除失败");
				}
			},
			error:function(){
				alert("删除异常");
			}
		});
	}
	//加载列表显示
    function list(page,pageSize){
		//清空原有列表数据
		$("#depts").empty();
		//发送ajax请求显示列表
		$.ajax({
			url:"http://localhost:8888/dept/list",
			type:"get",
			data:{"page":page,"pageSize":pageSize},
			dataType:"json",
			success:function(result){
				for(var i=0;i<result.length;i++){
					//获取部门信息
					var deptno = result[i].deptno;
					var dname = result[i].dname;
					var loc = result[i].loc;
					//配成一个tr元素
					var str= '<tr>';
					str+='<td>'+deptno+'</td>';
					str+='<td>'+dname+'</td>';
					str+='<td>'+loc+'</td>';
					str+='<td><a href="#">更新</a>';
					str+=' <a href="#" onclick="del('+deptno+')">删除</a></td>';
				    str+='</tr>';
				    //将tr元素添加到table中
				    $("#depts").append(str);
				}
			}
		});
	}
</script>
</head>
<body>
	<h1>部门列表</h1>
	<table id="depts">	
	</table>
	<a href="#" onclick="list(1,5)">第一页</a>
	<a href="#" onclick="prePage()">上一页</a>
	<a href="#" onclick="nextPage()">下一页</a>
</body>
</html>

注意要使后台json数据可以用于前台,需要在后台项目中写一个Filter用于授权。

@WebFilter(filterName="ajaxCorsFilter",servletNames={"dispatcherServlet"})
public class AjaxCorsFilter implements Filter{

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		//设置response响应header参数,目的支持ajax跨域访问响应问题
		HttpServletResponse httpServletResponse = (HttpServletResponse)response;
		//用于查询到json跨域
		httpServletResponse.setHeader("Access-Control-Allow-Origin", "http://localhost:8080");
		//存放数据跨域,请求方式全部可跨域
		httpServletResponse.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE");
		chain.doFilter(request, response);
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

}

在tomcat服务器运行使用http://localhost:8080/dept-ui/list.html进行测试。

---------------------------------------------------------------------------------------------

打包发布案例

在dept-service后端项目的pom.xml文件加入

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

用于打包发布项目。

右键项目Run As-->Maven Build-->Goals 栏输入package等同于终端mvn package命令

打包成功。

终端java -jar 包名就可以运行。

前端项目

右键Export-->WAR File 打包成war包,丢到tomcat服务器运行。

再别的机器上运行别人项目,注意配置文件中数据库连接的url的配置。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在这个互联网时代,客服可以说必不可少,每个电商网站都应该有一个强大的智能客服对话系统,以满足用户沟通的需求。智能客服对话系统,不仅需要人工的沟通,同时结合人工智能实现智能对话,减少人工客服的成本,势在必行。基于SpringBoot+Python的多语言前后端智能多人聊天系统课程,将以基础知识为根基,带大家完成一个强大的智能客服系统,该系统将包含以下功能:智能对话机器人、单聊、群聊、消息撤回、上线、下线通知、用户动态信息实时提示等。即时通讯和人工智能,在未来的发展趋势,必然需要大批人才,掌握这两个技术势在必行。项目是一个真实可用的项目,商业价值不言而喻。也可以基于课程的基础上进一步完善和优化,所以价值是很高的。本课程包含的技术: 开发工具为:IDEA、WebStorm、PyCharmTensorflowRNNLSTMAnacondaSpringBoot SpringCloudWebsocketSTOMPDjangoVue+Nodejs+jQuery等 课程亮点: 1.与企业接轨、真实工业界产品2.从基础到案例,逐层深入,学完即用3.市场主流的前后端分离架构和人工智能应用结合开发4.多语言结合开发,满足多元化的需求5.涵盖TensorFlow1.x+TensorFlow2.x版本6.智能机器人实战7.即时通讯实战8.多Python环境切换9.微服务SpringBoot10.集成SpringCloud实现统一整合方案 11.全程代码实操,提供全部代码和资料 12.提供答疑和提供企业技术方案咨询 课程目录:第一章、Anaconda以及TensorFlow环境和使用0、智能多人聊天系统课程说明1、智能多人聊天系统之Anaconda讲解2、智能多人聊天系统之Anaconda安装和使用3、智能多人聊天系统之Anaconda之conda命令使用4、智能多人聊天系统之TensorFlow讲解5、智能多人聊天系统之TensorFlow安装和使用6、TensorFlow常量、变量和占位符实战讲解17、TensorFlow常量、变量和占位符实战讲解28、TensorFlow原理补充讲解9、TensorFlow四则运算实战讲10、TensorFlow矩阵操作以及运算实战讲解111、TensorFlow矩阵操作以及运算实战讲解212、TensorFlow均匀分布和正态分布数据实战讲解13、智能多人聊天系统之Numpy实战讲解14、智能多人聊天系统之matplotlib实战讲解15、TensorFlow深度学习DNN讲解16、TensorFlow常用Python扩展包讲解17、TensorFlow常用回归算法以及正则化讲解18、TensorFlow损失函数定义和使用实战讲解19、TensorFlow优化器讲解以及综合案例实战讲解20、智能多人聊天系统之RNN讲解21、智能多人聊天系统之RNN种类讲解22、智能多人聊天系统之RNN代码实战23、智能多人聊天系统之LSTM讲解24、智能多人聊天系统之attention机制讲解25、智能多人聊天系统之Django环境构建及初体验26、智能多人聊天系统之Django开发27、Python章节环境侯建和项目搭建28、Python TensorFlow读取训练数据代码编写29、Python TensorFlow形成语料编码30、Python TensorFlow保存字典文件31、Python TensorFlow构建词向量32、Python TensorFlow构建lstm模型以及attention wrapper33、Python TensorFlow训练代码编写34、Python整体代码讲解35、Python运用模型代码讲解36、SpringBoot讲解以及构建web应用37、Spring Cloud注册中心构建38、智能多人聊天系统之前端Vue项目构建39、SpringBoot+Websocket群聊40、SpringBoot+Websocket昵称群聊41、SpringBoot+Websocket群聊+单聊实战42、SpringBoot+Stomp单聊143、SpringBoot+Stomp单聊244、SpringBoot+Stomp单聊+群聊45、Django Web整合TF代码讲解及Postman调试46、智能客服系统单聊群聊等项目功能代码讲解147、智能客服系统单聊群聊等项目功能代码讲解248、智能客服系统集成机器人对话代码开发讲解49、智能机器人TensorFlow2版本升级实战之训练模型代码讲解50、智能机器人TensorFlow2版本升级实战之预测代码讲解 51、智能机器人TensorFlow2版本升级实战补充讲解

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值