学习小结
为期两个星期的实训结束了,在这两周的时间里,我学到了不少之前没有接触过的知识,这也是我第一次正式接触前端后端的开发。在学习开发的过程中,出现过不少问题,自己能够解决的、自己不能解决的都有,下面是我对这次实训的一次总结。
首先是了解了什么是SSM(SSM=Spring+springMvc+mybatis)、SpringMVC的工作原理以及如何搭建SpringMVC的步骤,再深一步就是注解开发和非注解开发,项目建立的基本步骤:新建web,File—new--other--Web--Dynamic web Project--next--Project name--next--next--Web Module里面选中Generate—Finish,在开发过程中,最可能出现的错误就是404和500,出现404就要考虑是不是配的注解不对,还是路径不对,如果出现500则是代码的问题。
学习了前端的开发之后,就是添加数据库的问题了,建立数据库大概分为这几个步骤以及需要注意的问题(以具体的实例举例):
添加数据库--
- 导入config到src
- config中的db.properties中的url最后的名字与数据库名字要一致,password和username要改成自己的密码和用户名
- SqlMapConfig.xml中的<properties resource="config/db.properties"></properties> “”中的内容要一致
- jar包复制到WEB-INF-->lib中 全选build path add
- Web App Libraries-->mysql-connector-java-5.1.45-bin.jar-->com.mysql-->jdbc-->Driver.class 右击选择copy qualified name 粘贴 到db.properties中的driver后面,去掉.class
- Src中pojo的User里面的参数要和数据库中的名字一致
- com.neuedu里面建一个mapper包
- mapper包里面建UserMapper.java
添加数据库之后,需要将前端后端结合起来,实现各个功能,下面是我自己的一些总结以及注意点:
- 先写mapper文件,新建Mapper.java和Mapper.xml文件,.java文件是一个接口,.xml文件里面写sql语句
- 接下来写service文件,新建Service.java和ServiceImpl.java文件,接口和实现,e.g public class DepartmentServiceImpl implements DepartmentService {@AutowiredDepartmentMapper departmentMapper;Service.java中的方法和Mapper.java中的方法相同
- 接着写controller
e.g @RequestMapping(value="/user")
public class UserController {
@Autowired
private DepartmentService deparetmentSerivce;
@RequestMapping(value = "findDep")
public ModelAndView findDep(Integer departid,String departname)throws Exception {
department department = deparetmentSerivce.findDep(departid, departname);
ModelAndView mv = new ModelAndView();
mv.addObject("department",department);
mv.setViewName("bumenguanli/save2_index2");
return mv;
}
注意:
1、在Service中新建一个文件后,需要在config(配置文件)中spring包中的applicationContext-service.xml文件中加入
<bean id="DepartmentService class="com.neuedu.service.DepartmentServiceImpl"/>
Springmvc 如果不在web-inf中,那么value的值<property name="prefix" value="/"/>
.jsp文件中要改chareset=UTF-8,否则会乱码
e.g<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
function chaxun(){
var dp_id =document.getElementById("departid").value;
var dp_name = document.getElementById("departname").value;
precision.style.display=“";
2、.jsp中的方法
e.g
function chaxun(){
var dp_id =document.getElementById("departid").value;
var dp_name = document.getElementById("departname").value;
precision.style.display="";
window.location.href="../user/findDep?departid="+dp_id+"&departname="+dp_name;
/* ?后面和controller参数相对应 */ ../user/findDep和controller的value相同
}
写方法的时候,public int insDep(Department department)throws Exception;
int是返回类型,参数是通过什么查询之类的,多的话就用对象传
resultType 返回的类型 parameter 传入的的类型可以不写
integer比int多一个null值