SqlMapConfig
<?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 resource="jdbc.properties"></properties>
<environments default="developemnt">
<environment id="developemnt">
<transactionManager type="JDBC"></transactionManager>
<property name="driver" value="${driverClassName}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="StudentMapper.xml"></mapper>
</mappers>
</configuration>
Mapper
<?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="rar">
<select id="getAll" resultType="Student">
select sno,name from students
</select>
</mapper>
jdbc.properties文件
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/friend?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
username=root
password=123456
测试类
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.*;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.List;
public class MyTest {
@Test
public void testA() throws IOException {
//使用文件流读取核心配置文件
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
//创建SqlSessionFactory工厂
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
//取出sqlSession
SqlSession sqlSession = factory.openSession();
//取出查询操作
List<Student> list = sqlSession.selectList("rar.getAll");
list.forEach(student -> System.out.println(student));
//关闭sqlSession
sqlSession.close();
}
}
表
结果
原因是啥吗