一、示例代码图
二、代码例子
1.接口类--boyMapper
package com.project.Mapper; import com.project.pojo.Boy; import java.util.List; public interface boyMapper { //1.定义一个查询全部的方法 //List<封装类> 查询全部的方法 List<Boy> selectAll(); }
2.封装类--Boy
package com.project.pojo; public class Boy { private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Boy(Integer id, String name, Integer age) { this.id = id; this.name = name; this.age = age; } public Boy() { } @Override public String toString() { return "boy{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
3.测试类---BoyTest
package com.project.pojo; import com.project.Mapper.boyMapper; 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.util.List; public class BoyTest { public static void main(String[] args) throws IOException { //1.加载mybatis连接文件(核心配置文件) String resource = "mybatis-config.xml";//这是连接数据库的统一配置xml文件 InputStream inputStream = Resources.getResourceAsStream(resource);//固定写法 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//固定写法 //2.获取SqlSessionFactory对象--固定写法 SqlSession sqlSession = sqlSessionFactory.openSession(); //3.执行sql语句 //接口 自定义接口名 = sqlSession.getMapper(接口.class); boyMapper boyMapper = sqlSession.getMapper(boyMapper.class); //接口里面的方法 List<Boy> boyAll = boyMapper.selectAll(); //4.打印语句 System.out.println(boyAll); //5.释放资源--固定写法 sqlSession.close(); } }
4.映射文件--boyMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--映射查询方法--> <mapper namespace="com.project.Mapper.boyMapper"><!--namespace=对应接口的全部路径--> <select id="selectAll" resultType="com.project.pojo.Boy"><!--参数一:接口对应的方法名 参数二:封装类的详细地址--> select * from boy;<!--要执行的sql语句--> </select> </mapper>
5.统一配置文件--mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://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"/><!--改:数据库的固定连接,老版本不用cj--> <property name="url" value="jdbc:mysql://localhost:3306/javafx"/><!--改数据库的要连接的库--> <property name="username" value="root"/><!--改:数据库的账号--> <property name="password" value="123456"/><!--改:数据库的密码--> </dataSource> </environment> </environments> <mappers> <!--加载映射文件--> <mapper resource="com/project/Mapper/boyMapper.xml"/><!--改:把映射文件的源根路径复制到这里--> </mappers> </configuration>
6.依赖文件--pom.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>maven-java</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> </properties> <!--mybatis需要的依赖包--> <dependencies> <!--mybatis依赖包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.11</version> </dependency> <!--mysql依赖包--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.30</version> </dependency> </dependencies> </project>
7.数据库对应的表