发表于: 2018-03-26 23:55:25
3 485
原本说的事情,今天有变故,所以就进行了任务二。
我发现,做过的任务,一点一点解释出来,确实能学到东西。我决定继续采取这个模式。
SSM框架里面,测试类有这么几句话:
@Test
public void deleteByPrimaryKey() {
studentService.deleteByPrimaryKey(20000474);}
以前不是这样的,我一点一点改的,public代表读取限制符,void不需要返回值,()为什么是空的,删除需要参数int id的啊,这个可以看studentService里面,public int deleteByPrimaryKey(int id);很奇怪,没琢磨透,等下再试试加上int id会不会报错。紧接着就是调用studentService里面的删除方法,输入id就可以了。
刚才在括号里加入了int id,结果运行报错:Method deleteByPrimaryKey should have no parameters也就是这个方法里面不应该有参数。
@Test
public void deleteByPrimaryKey() {
int s = studentService.deleteByPrimaryKey(20000448);
System.out.println(s);
}问题来了,为什么我根据id删除,直接调用方法就好,而根据id查询,就必须写一个Student stuent呢?咨询了牛涛,说是根据Service里面,删除会返回一个值,类型是int,名字是s。
那我再进一步,不设置返回值,看能不能删除。首先是改了接口里面,把public int deleteByPrimaryKey(int id);改成public void deleteByPrimaryKey(int id);,然后改了studentServiceImpl里面,再改了studentMapper。就可以啦。运行,也是可以删除的。
@Test
public void insert() {
Student student = new Student();
student.setId(592);
student.setName("岳不群");
student.setQq("86左冷禅54");
student.setMajor("java");
studentService.insert(student);
System.out.println(student);}
新建一个对象,分别调用Student类里面的setid,name,QQ,Major方法,给这四个属性赋值,然后调用写入数据库的方法,XML里面也是我自己改成了insert into student (id,name,qq,major) values (#{id},#{name},#{qq},#{major})。
@Test
public void selectByPrimaryKey() {
Student student = studentService.selectByPrimaryKey(5221);
System.out.println(student);}
这个是根据ID查询student,调用方法。
@Test
public void updateByPrimaryKey() {
Student student = new Student();
student.setId(353);
student.setName("asd");
student.setQq("44458985");
student.setMajor("sda");
studentService.updateByPrimaryKey(student);}
我把更改不要返回值,都改成了void,也是可以更改的,那么等你改完之后,你怎么知道你更改了数据库呢,除了查看数据库之外。我新建了一个int id= 353,然后System。out。println(studentService.selectByPrimaryKey(id));调用查询方法,查询这个id就可以了。
返回类型的意思,也就是我在数据库里面根据id改了一行数据,数据库受影响的是那么一行数据,那么运行之后,返回了一个1.StudentService里面的改变方法是:
public int updateByPrimaryKey(Student student);
返回类型是int ,同样的,StudentServiceImpl里面也是一个返回类型为int。
数据类型有八种,除了int之外,还有byte, short,booble,long,char,double,folat.这是基本数据类型,除此之外,还有引用数据类型,
private StudentService studentService;比如这句话,StudentService就是引用数据类型。
明天的计划:明天把任务二的配置文件解释出来,还有控制器里面代码的意思。
遇到的问题:暂无
今天的收获:可以在java里手写增删改查了,也明白为什么有些方法要设置引用值了。
java任务二开始时间:2018.01.25
预计demo时间:2018.02.12
可能有延期风险,原因是:json看不懂,控制器的逻辑看不懂,所以又回看了java语法
禅道链接地址:http://task.ptteng.com/zentao/project-task-501.html
评论