续上篇:Springboot整合Mybatis的详细案例+图解+分析-CSDN博客
mapper层(StudentMapper)
//通过id查询student方法
Student searchStudentById(int id);
//通过id删除student方法
int deleteStudentById(int id);
//通过id增加student方法
int insertStudent(Student student);
//通过id修改student方法
int updateStudent(Student student);
service层(StudentService)
//通过id查询student方法
Student searchStudentById(int id);
//通过id删除student方法
int deleteStudentById(int id);
//通过id增加student方法
String insertStudent(Student student);
//通过id修改student方法
String updateStudent(Student student);
service接口层(StudentServiceImpl)
//通过id查找
@Override
public Student searchStudentById(int id) {
return studentMapper.searchStudentById(id);
}
//删除
@Override
public int deleteStudentById(int id) {
int result1 = studentMapper.deleteStudentById(id);
if (result1 == 1) {
return 1;
} else {
return 0;
}
}
//添加
@Override
public String insertStudent(Student student)
{
int result2=studentMapper.insertStudent(student);
if(result2==1)
{
return "添加成功!";
}
else
{
return "添加失败!";
}
}
//修改
@Override
public String updateStudent(Student student)
{
int result3=studentMapper.updateStudent(student);
if(result3==1)
{
return "修改成功!";
}
else
{
return "修改失败!";
}
}
controller层(StudentController)
@RequestMapping("/query/{id}")
public String searchStudentById(@PathVariable("id") int id)
{
Student student=studentService.searchStudentById(id);
return student.toString();
}
@RequestMapping("/delete/{id}")
public int deleteStudentById(@PathVariable("id") int id)
{
return studentService.deleteStudentById(id);
}
@RequestMapping("/insert")
public String insertStudent(Student student){
return studentService.insertStudent(student);
}
@RequestMapping("/update")
public String updateStudent(Student student)
{
return studentService.updateStudent(student);
}
*mapper.xml(StudentMapper.xml)
<resultMap id="BaseResultMap" type="com.example.demo.entity.Student">
<result column="id" jdbcType="INTEGER" property="id"/>
<result column="userName" jdbcType="VARCHAR" property="username"/>
<result column="passWord" jdbcType="VARCHAR" property="password"/>
<result column="sex" jdbcType="VARCHAR" property="sex"/>
<result column="age" jdbcType="INTEGER" property="age"/>
</resultMap>
<select id="queryStudentList" resultType="Student">
select * from student
</select>
<select id="searchStudentById" parameterType="java.lang.Integer" resultType="Student">
select * from student where id=#{id}
</select>
<delete id="deleteStudentById" parameterType="java.lang.Integer">
delete from student where id=#{id}
</delete>
<insert id="insertStudent">
insert into student (id,username,password,sex,age) values (#{id},#{username},#{password},#{sex},#{age})
</insert>
<update id="updateStudent">
update student set username=#{username},password=#{password},sex=#{sex},age=#{age} where id=#{id}
</update>
运行结果
查询:http://localhost:8081/query/2
==> Preparing: select * from student
==> Parameters:
<== Columns: id, username, password, sex, age
<== Row: 1, tom, 123, nan, 22
<== Row: 2, qq, 123456, nu, 23
<== Row: 3, aa, 11111, nan, 20
<== Row: 4, cd, 123456, nan, 21
<== Total: 4
添加:http://localhost:8081/insert?id=5&username=dd&password=123456&sex=nan&age=25
==> Preparing: select * from student
==> Parameters:
<== Columns: id, username, password, sex, age
<== Row: 1, tom, 123, nan, 22
<== Row: 2, qq, 123456, nu, 23
<== Row: 3, aa, 11111, nan, 20
<== Row: 4, cd, 123456, nan, 21
<== Row: 5, dd, 123456, nan, 25
<== Total: 5
删除:http://localhost:8081/delete/4
==> Parameters:
<== Columns: id, username, password, sex, age
<== Row: 1, tom, 123, nan, 22
<== Row: 2, qq, 123456, nu, 23
<== Row: 3, aa, 11111, nan, 20
<== Row: 5, dd, 123456, nan, 25
<== Total: 4
修改:http://localhost:8081/update?id=5&username=LL&password=1256&sex=nu&age=20
==> Parameters:
<== Columns: id, username, password, sex, age
<== Row: 1, tom, 123, nan, 22
<== Row: 2, qq, 123456, nu, 23
<== Row: 3, aa, 11111, nan, 20
<== Row: 5, LL, 1256, nu, 20
<== Total: 4