1.表
2.实体类
package com.sunyu.pojo; import java.util.Objects; public class Student { private Integer studentId; private String studentName; private Integer studentAge; private Integer math; private Integer chinese; private Integer english; public Student(){ } public Student(Integer studentId, String studentName, Integer studentAge, Integer math, Integer chinese, Integer english) { this.studentId = studentId; this.studentName = studentName; this.studentAge = studentAge; this.math = math; this.chinese = chinese; this.english = english; } public Integer getStudentId() { return studentId; } public void setStudentId(Integer studentId) { this.studentId = studentId; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public Integer getStudentAge() { return studentAge; } public void setStudentAge(Integer studentAge) { this.studentAge = studentAge; } public Integer getMath() { return math; } public void setMath(Integer math) { this.math = math; } public Integer getChinese() { return chinese; } public void setChinese(Integer chinese) { this.chinese = chinese; } public Integer getEnglish() { return english; } public void setEnglish(Integer english) { this.english = english; } @Override public String toString() { return "Student{" + "studentId=" + studentId + ", studentName='" + studentName + '\'' + ", studentAge=" + studentAge + ", math=" + math + ", chinese=" + chinese + ", english=" + english + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Student)) return false; Student student = (Student) o; return studentId.equals(student.studentId) && studentName.equals(student.studentName) && studentAge.equals(student.studentAge) && math.equals(student.math) && chinese.equals(student.chinese) && english.equals(student.english); } @Override public int hashCode() { return Objects.hash(studentId, studentName, studentAge, math, chinese, english); } }
3.接口
package com.sunyu.mapper; import com.sunyu.pojo.Student; import org.apache.ibatis.annotations.Param; import java.util.List; import java.util.Map; public interface StudentMapper { // 添加学生 int insertStudent(); boolean deleteStudent(); Student selectStudent(@Param("studentName") String studentName , @Param("studentAge") Integer studentAge); List<Student>getAllStudentInfor(@Param("studentAge") Integer studentAge); List<Map<String,Student>>listStudent(); List<Student>getStudentByLike(@Param("studentAge") Integer studentAge); boolean insertStudentInfor(@Param("studentId")Integer studentId,@Param("studentName") String studentName,@Param("studentAge") Integer studentAge,@Param("math") Integer math,@Param("chinese") Integer chinese , @Param("english")Integer english); } 4.获取代理sqlsession
package com.sunyu.solo; import com.sunyu.mapper.StudentMapper; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; public class GetSession { public static SqlSession session() throws IOException { // 构建核心配置文件输入流 InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml"); // 获取sql对象 SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); // 获取sqlsessionfactory对象 SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream); // 获取sql的会话对象sqlsession,用来操作数据库 SqlSession sqlSession = build.openSession(true); return sqlSession; // 获取接口的代理实现类对象 } }
5.映射文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--<1--namespace与mapper接口的全类名保持一致,--> <mapper namespace="com.sunyu.mapper.StudentMapper"> <!-- 添加学生--> <!-- int insertStudent();--> <insert id="insertStudent"> insert into grade values (null,"ok",23,56,46,79) </insert> <!-- boolean deleteStudent();--> <delete id="deleteStudent"> DELETE from grade where student_id = 1092 </delete> <!-- Student selectStudent(@Param("studentName") String studentName , @Param("studentAge") Integer studentAge);--> <select id="selectStudent" resultType="Student"> select * from grade where student_name = #{studentName} and student_age = #{studentAge} </select> <!-- List<Student>getAllStudentInfor(Integer studentAge);--> <select id="getAllStudentInfor" resultType="Student"> select * from grade where student_age = #{studentAge} </select> <!-- List<Map<String,Student>>listStudent();--> <select id="listStudent" resultType="map"> select * from grade </select> <!-- List<Student>getStudentByLike(@Param("studentAge") Integer studentAge);--> <select id="getStudentByLike" resultType="Student"> select * from grade where student_age like '%${studentAge}%' </select> <!-- boolean insertStudentInfor(@Param("studentId")Integer studentId,@Param("studentName") String studentName,@Param("studentAge") Integer studentAge,@Param("math") Integer math,@Param("chinese") Integer chinese , @Param("english")Integer english);--> <insert id="insertStudentInfor" useGeneratedKeys="true" keyProperty="student_id"> insert into grade values (null,#{studentName},#{studentAge},#{math},#{chinese},#{english}) </insert> </mapper>
6.核心文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 引入properties文件--> <properties resource="jdbc.properties"></properties> <!-- 给父类设置别名--> <typeAliases> <!-- <typeAlias type="com.sunyu.pojo.Student"></typeAlias>--> <!-- 通过包设置类型别名默认别名为类名--> <package name="com.sunyu.pojo"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <!--引入mybatis的映射文件--> <mappers> <!-- 将映射文件包名输入具备的条件:--> <!-- 1.mapper接口和映射文件所在的包必须一致--> <!-- 2.mapper接口的名字与映射文件名字必须一致--> <package name="com.sunyu.mapper"/> </mappers> </configuration>