MyBatis

 

 

 

在resource包下面的两个配置文件

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.jdbc.Driver"/>
<property name="url" value="jdbc:MysqL:///student?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="a1b2c3"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="usermapper.xml"/>
</mappers>
</configuration>


usermapper.xml:
<!--
sql语句的
-->
<?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="test">
<select id="selectAll" resultType="it.student">
select * from student;
</select>
</mapper>






test:
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 test {
public static void main(String[] args) throws IOException {
//加载mybatis核心配置文件获取sqlSession
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsstream(resource);
sqlSessionFactory sqlSessionFactory = new sqlSessionFactoryBuilder().build(inputStream);

//获取对象
sqlSession sqlSession = sqlSessionFactory.openSession();

//执行sql
List<Object> objects = sqlSession.selectList("test.selectAll");
System.out.println(objects);
sqlSession.close();
}
}

 

相关文章

可以认为OpenFeign是Feign的增强版,不同的是OpenFeign支持S...
为进一步规范小程序交易生态、提升用户购物体验、满足用户在...
云原生之使用Docker部署Dashdot服务器仪表盘
本文主要描述TensorFlow之回归模型的基本原理
1.漏洞描述Apache Druid 是一个集时间序列数据库、数据仓库和...
内部类(当作类中的一个普通成员变量,只不过此成员变量是cl...