开发环境:
Java JDK11,Maven,IntelliJ Idea,MySQL.
1. 先建立pom.xml文件,导入所需的依赖。
<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>com.oxygen</groupId> <artifactId>projectMybatis</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>projectMybatis</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.complier.target>11</maven.complier.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.10</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.30</version> </dependency> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.10.1</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> <include>**/resources/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.10.1</version> <configuration> <source>11</source> <target>11</target> </configuration> </plugin> </plugins> </build> </project>
在此Resources目录下建立Mybatis的主配置文件:mybatis-config.xml
src/main/resources/MybatisConfig.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://192.168.3.12:3306/oxygen"/> <property name="username" value="root"/> <property name="password" value="abcd2022"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/oxygen/dao/StudentDao.xml"/> </mappers> </configuration>
在此目录下建立mapper文件。mapper文件就是一个有SQL语句的SQL文件。
src/main/java/com/oxygen/dao/StudentDao.xml
<?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"> <mapper namespace="com.oxygen.dao.StudentDao"> <select id="selectStudentsAll" resultType="com.oxygen.entity.Student"> select * from oxygen.student </select> <insert id="insertStudent"> insert into oxygen.student values(#{id},#{name},#{age},#{email}) </insert> </mapper>
在此目录建立dao的Java接口
src/main/java/com/oxygen/dao/StudentDao.java
package com.oxygen.dao; import com.oxygen.entity.Student; import java.util.List; public interface StudentDao { public List<Student> selectStudentsAll(); public int insertStudent(Student student); }
建立Java的实体类
src/main/java/com/oxygen/entity/Student.java
package com.oxygen.entity; public class Student { private int id; private String name; private int age; private String email; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", email='" + email + '\'' + '}'; } }
测试类
src/test/java/TestDB.java
import com.oxygen.entity.Student; 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 java.io.IOException; import java.io.InputStream; import java.util.List; public class TestDB { @Test public void testSearchDB() throws IOException { String config="MybatisConfig.xml"; InputStream in=Resources.getResourceAsStream(config); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in); SqlSession session =sqlSessionFactory.openSession(); // List<Student> result=session.selectList("com.oxygen.dao.StudentDao.selectStudentsAll"); // result.forEach(System.out::println); String sql="com.oxygen.dao.StudentDao.insertStudent"; Student student=new Student(); student.setEmail("tianyu@utopaschool.com"); student.setId(1008); student.setAge(19); student.setName("田雨"); int num=session.insert(sql,student); session.commit(); System.out.println("Inert Result:"+num); session.close(); } }