<?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="db.properties"></properties>--> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost/mybatis_test"></property> <property name="username" value="root"></property> <property name="password" value="111111"></property> </dataSource> </environment> </environments> <!--加载映射文件--> <mappers> <mapper resource="cn/zephyr/mapper/SysUserMapper.xml"></mapper> </mappers> </configuration> 复制代码
// 根据配置文件获取到SqlSessionFactory InputStream configFile = new FileInputStream("./src/main/resources/mybatis-conf.xml"); // 根据SqlSessionFactory获取sqlSession SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configFile); SqlSession sqlSession = sqlSessionFactory.openSession(); // 在sqlSession中获取Mapper SysUserMapper mapper = sqlSession.getMapper(SysUserMapper.class); // 执行SQL查询,获取结果 List<SysUser> sysUsers = mapper.queryList(); System.err.println(Arrays.asList(sysUsers)); 复制代码
<?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="db.properties"></properties>--> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost/mybatis_test"></property> <property name="username" value="root"></property> <property name="password" value="111111"></property> </dataSource> </environment> </environments> <!--加载映射文件--> <mappers> <mapper resource="cn/zephyr/mapper/SysUserMapper.xml"></mapper> </mappers> </configuration> 复制代码
// 1. 读取配置文件 InputStream configFile = new FileInputStream("./src/main/resources/mybatis-conf.xml"); // 2. 根据配置文件获取到SqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configFile); // 3. 根据SqlSessionFactory获取sqlSession SqlSession sqlSession = sqlSessionFactory.openSession(); // 4. 在sqlSession中获取Mapper SysUserMapper mapper = sqlSession.getMapper(SysUserMapper.class); // 5. 执行SQL查询,获取结果 List<SysUser> sysUsers = mapper.queryList(); System.err.println(Arrays.asList(sysUsers)); 复制代码
<mappers>
InputStream configFile = new FileInputStream("./src/main/resources/mybatis-conf.xml"); 复制代码
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configFile); 复制代码这一步主要做的事情就是根据配置文件获取所有的Mapper.xml,从而获取Mapper接口类,并以Mapper接口类为键,对应的代理工厂对象为值,存储到一个Mapper容器(knownMappers)中。这一步整体的代码执行流程如下图所示:
SqlSession sqlSession = sqlSessionFactory.openSession(); 复制代码
sqlSession.getMapper(SysUserMapper.class); 复制代码这一步就根据接口类,从之前的容器(knownMappers)中拿到对应的代理工厂对象,生成对应的代理对象并将其返回,这一步整体的代码执行流程如下:
List<SysUser> sysUsers = mapper.queryList(); 复制代码
欢迎访问我的个人博客