在数据库中创建一个学生表s_student和班级表c_class
| 学生(id) | 学生姓名(name) | 学生年龄(age) | 所属班级(cid) |
| 1 | 张三 | 18 | 1 |
| 2 | 李四 | 18 | 2 |
| 3 | 王五 | 19 | 2 |
| 4 | 赵六 | 20 | 1 |
| 班级id(id) | 班级名称(classname) |
| 1 | 一班 |
| 2 | 二班 |
要求:
1.查询id为2的学生信息;
2.将id为4的学生姓名修改为李雷,年龄修改为21;
3.增加一条学生信息(5,赵六,19,1);
4.删除id为1的学生信息;
5.查询姓名为赵六,年龄为20的学生信息;
6.一对多查询出二班所有学生的信息;
7.一对一查询出id=2的学生信息;
步骤:
1.创建表
2.创建持久化类Student、Class1
package com.xiao.pojo;
public class Student {
private Integer id;
private String name;
private Integer age;
private Integer cid;
private Class1 classId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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;
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public Class1 getClassId() {
return classId;
}
public void setClassId(Class1 classId) {
this.classId = classId;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + ''' +
", age=" + age +
", cid=" + cid + "classId=" + classId +
'}';
}
}
package com.xiao.pojo;
import java.util.List;
public class Class1 {
private Integer id;
private String classname;
private List studentList;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getClassname() {
return classname;
}
public void setClassname(String classname) {
this.classname = classname;
}
public List getStudentList() {
return studentList;
}
public void setStudentList(List studentList) {
this.studentList = studentList;
}
@Override
public String toString() {
return "Class{" +
"id=" + id +
", classname='" + classname + ''' + "studentList=" + studentList +
'}';
}
}
3.在src/main/java创建com.xiao.dao包,包下创建StudentMapper、ClassMapper接口
package com.xiao.dao;
import com.xiao.pojo.Student;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface StudentMapper {
//(1)查询id=2的学生信息
@Select("select * from s_student where id = #{id}")
Student selectStudentById(int id);
//(2)将id=4的学生姓名修改为李雷,年龄修改为21
@Update("update s_student set name = #{name},age = #{age} " + "where id = #{id}")
int updateStudent(Student student);
//(3)增加一条学生信息(赵六,19,1);
@Insert("Insert into s_student(name,age,cid)" + "values(#{name},#{age},#{cid})")
int insertStudent(Student student);
//(4)删除id=1的学生信息
@Delete("delete from s_student where id = #{id}")
int deleteStudentById(int id);
//(5)查询姓名为赵六,年龄为19的学生信息;
@Select("select * from s_student where name = #{param01} and age = #{param02}")
Student selectStudentByNameAndAge(@Param("param01") String name,@Param("param02") int age);
//(6)一对多查询出二班所有学生的信息
@Select("select * from s_student where cid = #{id}")
@Results({@Result(id = true,column = "id",property = "id"),
@Result(column = "name",property = "name"),
@Result(column = "age",property = "age")})
List selectStudentByCid(int cid);
//(7)一对一查询出id=4的学生所有信息
@Select("select * from s_student where id = #{id}")
@Results({@Result(column = "cid",property = "classId",
one = @One(select = "com.xiao.dao.ClassMapper.selectClassById"))})
Student selectStudentWithClass(int id);
}
package com.xiao.dao;
import com.xiao.pojo.Class1;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
public interface ClassMapper {
//(6)一对多查询出二班所有学生的信息
@Select("select * from c_class where id = #{id}")
@Results({@Result(id = true,property = "id",column = "id"),
@Result(property = "classname",column = "classname"),
@Result(column = "id",property = "studentList",
many = @Many(select = "com.xiao.dao.StudentMapper.selectStudentByCid"))})
Class1 selectStudentByClass(int id);
//(7)一对一查询出id=4的学生所有信息
@Select("select * from c_class where id = #{id}")
Class1 selectClassById(int id);
}
4.在mybatis-config.xml引入接口
5.创建测试类
package Test;
import com.xiao.dao.ClassMapper;
import com.xiao.dao.StudentMapper;
import com.xiao.pojo.Class1;
import com.xiao.pojo.Student;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import utils.MyBatisUtils;
public class mybatisTest {
SqlSession sqlSession = MyBatisUtils.getSession();
//(1)查询id=2的学生信息
@Test
public void findStudentById(){
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Student student = mapper.selectStudentById(2);
System.out.println(student.toString());
sqlSession.close();
}
//(2)将id=4的学生姓名修改为李雷,年龄修改为21
@Test
public void updateStudent(){
Student student = new Student();
student.setId(4);
student.setName("李雷");
student.setAge(21);
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
int update = mapper.updateStudent(student);
if (update > 0){
System.out.println("成功修改了" + update + "条数据!");
}else {
System.out.println("修改失败!");
}
System.out.println(student.toString());
sqlSession.commit();
sqlSession.close();
}
//(3)增加一条学生信息(5,赵六,19,1);
@Test
public void insertStudent(){
Student student = new Student();
student.setId(5);
student.setName("赵六");
student.setAge(19);
student.setCid(1);
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
int insert = mapper.insertStudent(student);
if (insert > 0){
System.out.println("成功添加了" + insert + "条数据!");
}else {
System.out.println("添加失败!");
}
System.out.println(student.toString());
sqlSession.commit();
sqlSession.close();
}
//(4)删除id=1的学生信息
@Test
public void deleteStudentById(){
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
int delete = mapper.deleteStudentById(1);
if (delete > 0){
System.out.println("成功删除了" + delete + "条数据!");
}else {
System.out.println("删除失败!");
}
sqlSession.commit();
sqlSession.close();
}
//(5)查询姓名为赵六,年龄为19的学生信息;
@Test
public void findStudentByNameAndAge(){
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Student student = mapper.selectStudentByNameAndAge("赵六",19);
System.out.println(student.toString());
sqlSession.close();
}
//(6)一对多查询出二班所有学生的信息
@Test
public void selectStudentByClass(){
ClassMapper mapper = sqlSession.getMapper(ClassMapper.class);
Class1 class1 = mapper.selectStudentByClass(2);
System.out.println(class1.toString());
sqlSession.close();
}
//(7)一对一查询出id=4的学生所有信息
@Test
public void selectStudentWithClass(){
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Student student = mapper.selectStudentWithClass(4);
System.out.println(student.toString());
sqlSession.close();
}
}



