思途实训-day02
上午
杜老师带我们写出了剩下的两个模块,分别是controller和serivce
- Departmentcontroller.java
package com.situ.company.department.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.situ.company.department.model.DepartmentModel; import com.situ.company.department.service.DepartmentService; @RestController @RequestMapping("department") public class DepartmentController { @Autowired private DepartmentService service; @RequestMapping("insert") public int insert(DepartmentModel model) { return service.insert(model); } @RequestMapping("delect") public int delect(DepartmentModel model) { return service.delect(model); } @RequestMapping("updata") public int updata(DepartmentModel model) { return service.updata(model); } @RequestMapping("selectmodel") public DepartmentModel selectmodel(DepartmentModel model) { return service.selectModel(model); } @RequestMapping("selectList") public List<DepartmentModel> select(DepartmentModel model) { return service.selectList(model); } }
- DepartmentSerivce.java
package com.situ.company.department.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.situ.company.department.mapper.DepartmentMapper; import com.situ.company.department.model.DepartmentModel; @Service public class DepartmentService { @Autowired private DepartmentMapper mapper; public int insert(DepartmentModel model) { DepartmentModel mdb = selectModel(model); if(mdb == null) { return mapper.insert(model); } else { return -1; } } public int delect(DepartmentModel model) { return mapper.Delete(model); } public int updata(DepartmentModel model) { return mapper.updata(model); } public DepartmentModel selectModel(DepartmentModel model) { return mapper.selectModel(model); } public List<DepartmentModel> selectList(DepartmentModel model) { String name = model.getName(); if(name == null)//此处的null必须变成“”,否则下面的setName拼接成的为%null% 查询的是name中含有name的字符 { name = ""; } model.setName('%' + name + '%'); return mapper.selectList(model); } }
- Model模块是建立私有化的数据类型,相当于建立数据库中的对应数据。
- Mapper模块在Model基础上进行数据库(sql)语句的编辑(增删改查)
- Service模块在Mapper基础上进行一些逻辑化的操作。例如:id(主键)和code(非主键),但我想让code的值是唯一的,这时候需要用到Service模块的的操作,代码如下:
public int insert(DepartmentModel model) { DepartmentModel mdb = selectModel(model); if(mdb == null) { return mapper.insert(model); } else { return -1; } }
- 最后是Controller模块,这个模块对应的是网页端,给网页服务的。
@RequestMapping("insert")
这个地方的insert和网页的请求一样,不能写错。
3.
下午
老师讲解使用网页端来进行数据库的增删改查
- 刚开始,老师直接使用在网址中输入的命令
http://127.0.0.1:8080/company/department/insert?code=1&name=hls&tel=17852031547
- 然后直接转到网页端,新建一个html文件,文件代码如下:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <script type="text/javascript" src="/company/base/js/jquery-3.3.1.min.js"></script> <style> body { background-image:url('9.jpg'); height: 100%; width: 100%; background-size: cover; position: absolute; overflow: hidden; } #login{ width:400px; height:280px; position:absolute; left: 50%; top: 40%; margin-left:-200px; margin-top:-140px; border:1px; align:center; } #form{ width:300px; height:160px; position:relative; left:50%; top:40%; margin-left:-150px; margin-top:-80px; } #img{ width:300px; height:160px; position:relative; left:55%; top:29.4%; margin-left:-150px; margin-top:-80px; } #bu1{ background-color: black; width:50px; height:40px; } #bu2{ background-color: black; width:50px; height:40px; } #bu3{ background-color: black; width:100px; height:40px; } </style> </head> <body> <div id = "img"> <img src="8.jpg" width="165" height="60" /> </div> <div id = "login"> <div id = "form"> <form> <h3 align="center" style="color:green" >部门注册系统</h3> * 账 号: <input type='text' name = 'code'><br><br> * 名 称: <input type='text' name = 'name'><br><br> * 电 话: <input type='text' name = 'tel'><br> <br> <input id = "bu1" style = "color: white" type="button" value="注册" onclick="test()"> <input id = "bu2" style = "color: white" type="button" value="重置" onclick="test1()"> <input id = "bu3" style = "color: white" type="button" value="成员注册页面" onclick="test2()"> </form> </div> </div> </body> <script type="text/javascript"> function test1() { window.location.href="http://127.0.0.1:8080/company/web/department.html"; } function test() { var code = $("input[name='code']").val(); var name = $("input[name='name']").val(); var tel = $("input[name='tel']").val(); //htpp://127.0.0.1:8080/company/department/insert?code=a1&name=a2&pass=a3 $.ajax({ url:'/company/department/insert', //data:'code='+code+'&name='+name+'&pass='+pass, //从网页端到java发送的请求参数 data:{code:code,name:name,tel:tel}, dataType:'json', //返回的数据类型 java返回至网页端 type:'post', //请求的方式 success:function(d) { console.log(d) } }) } function test2(){ window.open('http://127.0.0.1:8080/company/web/Employee.html'); } </script> </html>
这样就可以实施网页端的操作。
晚上
巩固网页端的知识,自己查询了HTML和CSS的一些操作,做出来了一个小小的界面
感觉杜老师真好,有问必答,太敬业了。
明天差不多要开始真正的做界面和逻辑化了,溜了,溜了!