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_id 14 * @param s_name 15 * @param s_address 16 */ 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 @Override 31 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 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <!-- 6 配置namespace 这个路径就是接口所在的路径 7 --> 8 <mapper namespace="com.hxzy.mybatis.pojo.Student_Mapper"> 9 10 <select id="select_student" parameterType="int" resultType="com.hxzy.mybatis.pojo.Student"> 11 select * from student where s_id = #{id} 12 </select> 13 14 <select id="select_all_student" resultType="com.hxzy.mybatis.pojo.Student" > 15 select * from student; 16 </select> 17 18 <select id="du_student" resultType="com.hxzy.mybatis.pojo.Student" parameterType="String"> 19 select * from student where student.s_name like "${value}%" 20 </select> 21 22 <select id="insert_student" parameterType="com.hxzy.mybatis.pojo.Student"> 23 insert into student values(#{s_id},#{s_name},#{s_address}) 24 </select> 25 26 <delete id="delete_student" parameterType="String"> 27 delete from student where student.s_name = #{name} 28 </delete> 29 30 <update id="update_student" parameterType="com.hxzy.mybatis.pojo.Student"> 31 update student set student.s_name = #{s_name} where student.s_id = #{s_id} 32 </update> 33 34 </mapper>
4.创建核心配置文件 (这个时候mybatis的配置文件 一般都叫 Mybatis_cfg.xml)
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <environments default="test1"> 7 8 <!--配置数据库的信息--> 9 <environment id="test1"> 10 <!--开启JDBC的事务--> 11 <transactionManager type="JDBC"></transactionManager> 12 <!--配置一些必要的信息--> 13 <dataSource type="POOLED"> 14 <property name="driver" value="com.mysql.jdbc.Driver"></property> 15 <property name="url" value="jdbc:mysql://127.0.0.1:3306/stu"></property> 16 <property name="username" value="root" ></property> 17 <property name="password" value="gubin"></property> 18 </dataSource> 19 </environment> 20 </environments> 21 <!--p配置核心配置文件--> 22 <mappers> 23 <mapper resource="student_Mapper.xml"></mapper> 24 </mappers> 25 </configuration>
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); /** * 查询全部的学生 */ List<Student> select_all_student(); /** * 查询姓杜的学生 */ List<Student> 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); //将返回结果存入集合中 List<Student> list = 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<Student> 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(); } }