Mybatis的Mapper接口方式实现简单crud操作:
1.创建实体类 与数据库对应
我的实体类是《Student》
1 package com.hxzy.mybatis.pojo; 2 3 import lombok.Data; 4 5 @Data 6 public class Student { 7 private Integer s_id; 8 private String s_name; 9 private String s_address;10 11 /**12 * 有参构造方法13 * @param s_id14 * @param s_name15 * @param s_address16 */17 public Student(Integer s_id, String s_name, String s_address) {18 this.s_id = s_id;19 this.s_name = s_name;20 this.s_address = s_address;21 }22 23 /**24 * 无参构造25 */26 public Student() {27 28 }29 30 @Override31 public String toString() {32 return "Student{" +33 "s_id=" + s_id +34 ", s_name='" + s_name + '\'' +35 ", s_address='" + s_address + '\'' +36 '}';37 }38 }
2.我的数据库表是student 《注意表字段要和实体类对应!!》
3.创建配置文件 (一般放在接口包中 ,名字为 接口名+Mapper.xml)
我的名字是 Student_Mapper.xml
1 2 5 89 10 13 14 17 18 21 22 25 26 27 delete from student where student.s_name = #{name}28 29 3031 update student set student.s_name = #{s_name} where student.s_id = #{s_id}32 33 34
4.创建核心配置文件 (这个时候mybatis的配置文件 一般都叫 Mybatis_cfg.xml)
1 2 56 7 8 9 21 2210 11 2012 13 14 1915 16 17 18 23 2524
5. 创建接口 (接口里面的方法名 就是Student_Mapper.xml的id)
package com.hxzy.mybatis.pojo;import java.util.List;public interface Student_Mapper { /** * 查询单个学生 * @param i * @return */ Student select_student(int i); /** * 查询全部的学生 */ Listselect_all_student(); /** * 查询姓杜的学生 */ List du_student(String str); /** * 添加学生 */ void insert_student(Student student); /** * 删除学生 */ void delete_student(String str); /** * 修改学生 */ void update_student(Student student);}
编写测试类:
package com.hxzy.mybatis.test;import com.hxzy.mybatis.entity.SqlSession_entity;import com.hxzy.mybatis.pojo.Student;import com.hxzy.mybatis.pojo.Student_Mapper;import org.apache.ibatis.session.SqlSession;import org.junit.Test;import java.util.List;public class Test_mybatis { /** * 查询单个学生 */ @Test public void select_student(){ //得到SqlSession 对象 SqlSession session = SqlSession_entity.getSession(); //此处省略了Statement语句的编写 Student_Mapper student_mapper = session.getMapper(Student_Mapper.class); //加载interface中的方法 Student student = student_mapper.select_student(1); session.close(); System.out.println(student); } /** * 查询多个学生 */ @Test public void select_all_student(){ //获得Session对象 SqlSession session = SqlSession_entity.getSession(); //反射技术 将接口加载到内存 Student_Mapper student_mapper = session.getMapper(Student_Mapper.class); //将返回结果存入集合中 Listlist = student_mapper.select_all_student(); //增强for遍历 for (Student student : list) { System.out.println(student); } session.close(); } /** * 查询所有姓杜的学生 */ @Test public void test_du(){ //获得Session SqlSession session = SqlSession_entity.getSession(); Student_Mapper student_mapper = session.getMapper(Student_Mapper.class); List list = student_mapper.du_student("杜"); System.out.println(list); session.close(); } /** * 添加学生 */ @Test public void tst_add(){ //获得Session SqlSession session = SqlSession_entity.getSession(); Student_Mapper student_mapper = session.getMapper(Student_Mapper.class); //添加值 student_mapper.insert_student(new Student(5,"黄小斌","石家庄")); //提交 session.commit(); //关闭 session.close(); } /** * 删除学生 */ @Test public void delete_student() { SqlSession session = SqlSession_entity.getSession(); Student_Mapper student_mapper = session.getMapper(Student_Mapper.class); student_mapper.delete_student("刘啊文"); session.commit(); session.close(); } /** * 修改学生 */ @Test public void update_student() { SqlSession session = SqlSession_entity.getSession(); System.out.println(); Student_Mapper student_mapper = session.getMapper(Student_Mapper.class); student_mapper.update_student(new Student(4,"小阿六","避暑山庄")); session.commit(); session.close(); }}