1.创建数据库:
2. 生辰数据库实现类pojo类:
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 boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return Objects.equals(studentId, student.studentId) && Objects.equals(studentName, student.studentName) && Objects.equals(studentAge, student.studentAge) && Objects.equals(math, student.math) && Objects.equals(chinese, student.chinese) && Objects.equals(english, student.english); } @Override public int hashCode() { return Objects.hash(studentId, studentName, studentAge, math, chinese, english); } }
3.创建交互接口mapper
package com.sunyu.mapper; public interface StudentMapper { // 添加学生 int insertStudent(); boolean deleteStudent(); }
4.创建mybatis核心配置文件mybatis-config.xml
<?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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/studentscore?serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <!--引入mybatis的映射文件--> <mappers> <mapper resource="mapper/StudentMapper.xml"/> </mappers> </configuration>
5.创建接口映射文件StudentMapper
<?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> </mapper>
6.测试添加学生
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 org.junit.Test; import solo.Giudice; import java.io.IOException; import java.io.InputStream; public class StudentMapperTest { @Test public void text() throws IOException { // 构建核心配置文件输入流 InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml"); // 获取sql对象 SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); // 获取sqlsessionfactory对象 SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream); // 获取sql的会话对象sqlsession,用来操作数据库 // true自动提交事务 SqlSession sqlSession = build.openSession(true); // 获取接口的代理实现类对象 StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); // 使用代理实现类调用方法 int i = mapper.insertStudent(); boolean result = Giudice.getResult(i); System.out.println(result); // 手动添加数据库事务 // sqlSession.commit(); sqlSession.close(); } }
7.基于mybatis3.5.7pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>mybatis</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <!-- Mybatis核心 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <!-- junit测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- MySQL驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency> </dependencies> </project>