时间:2021-07-01 10:21:17 帮助过:14人阅读
package com.rookie.bigdata.domain; /** * @author * @date 2018/10/9 */ public class Student { private Long stuNo; private String name; private Integer age; public Long getStuNo() { return stuNo; } public void setStuNo(Long stuNo) { this.stuNo = stuNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; if (stuNo != null ? !stuNo.equals(student.stuNo) : student.stuNo != null) return false; if (name != null ? !name.equals(student.name) : student.name != null) return false; return age != null ? age.equals(student.age) : student.age == null; } @Override public int hashCode() { int result = stuNo != null ? stuNo.hashCode() : 0; result = 31 * result + (name != null ? name.hashCode() : 0); result = 31 * result + (age != null ? age.hashCode() : 0); return result; } @Override public String toString() { return "Student{" + "stuNo=" + stuNo + ", name=‘" + name + ‘\‘‘ + ", age=" + age + ‘}‘; } }View Code
package com.rookie.bigdata.service; import com.rookie.bigdata.domain.Student; /** * @author * @date 2018/10/9 */ public interface StudentService { /** * 添加一个学生 * * @param student */ void create(Student student); /** * 删除一个学生 * * @param stuNo */ void deleteByNo(Integer stuNo); /** * 查询学生 * * @param stuNo */ Integer queryByStuNo(Integer stuNo); }
package com.rookie.bigdata.service; import com.rookie.bigdata.domain.Student; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; /** * @author * @date 2018/10/9 */ @Service public class StudentServiceImpl implements StudentService { @Autowired private JdbcTemplate jdbcTemplate; @Override public void create(Student student) { jdbcTemplate.update("INSERT INTO student(stu_no,name,age)VALUE (?,?,?)", student.getStuNo(), student.getName(), student.getAge()); } @Override public void deleteByNo(Integer stuNo) { jdbcTemplate.update("DELETE FROM student WHERE stu_no=?", stuNo); } @Override public Integer queryByStuNo(Integer stuNo) { return jdbcTemplate.queryForObject("select count(1) from student", Integer.class); } }
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/springboot spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver
测试代码如下:
package com.rookie.bigdata.service; import com.rookie.bigdata.domain.Student; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import static org.junit.Assert.*; /** * @author * @date 2018/10/9 */ @RunWith(SpringRunner.class) @SpringBootTest public class StudentServiceImplTest { @Autowired private StudentServiceImpl studentService; @Test public void create1() throws Exception { Student student = new Student(); student.setStuNo(1L); student.setName("张三"); student.setAge(23); studentService.create(student); } @Test public void deleteByName1() throws Exception { studentService.deleteByNo(1); } @Test public void queryByStuNo1() throws Exception { System.out.println(studentService.queryByStuNo(1)); } }
springboot之JdbcTemplate
标签:mysq 删除 ice ott interface drive unit arc test