一、场景模拟
基于自定义Mybatis框架和已有的Mysql数据库Mybatis,查询所有用户信息。
二、创建工程并引入Mybatis框架的坐标
<?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>com.william</groupId><artifactId>TestFormalMybatisFramework</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.5</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies>
</project>
三、创建domain
package com.william.domain;import Framework.domain.Mapper;import java.util.HashMap;
import java.util.Map;/*** @author :lijunxuan* @date :Created in 2019/7/9 16:54* @description :* @version: 1.0*/
public class User {private Integer id;private String username;private String password;private String sex;@Overridepublic String toString() {return "User{" +"id=" + id +", username='" + username + '\'' +", password='" + password + '\'' +", sex='" + sex + '\'' +'}';}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}
}
四、创建Sql文件UserMapper.xml
加入mybatis的约束
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.william.dao.UserDao"><select id="findAll" resultType="com.william.domain.User">select * from user</select>
</mapper>
五、3.1.5 创建核心配置文件SqlMapConfig.xml
加入mybatis的约束,不加会报错
报错Cause: org.xml.sax.SAXParseException lineNumber: 2; columnNumber: 16; 文档根元素 "
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//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://127.0.0.1:3306/web02?characterEncoding=utf8" /><property name="username" value="root" /><property name="password" value="root" /></dataSource></environment></environments><mappers><mapper resource="com/william/mapper/UserMapper.xml"></mapper></mappers>
</configuration>
六、3.1.6 测试
1.测试程序
package com.william;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.InputStream;
import java.util.List;/*** @author :lijunxuan* @date :Created in 2019/7/9 17:05* @description :* @version: 1.0*/
public class TestFramework {@Testpublic void test(){//获取核心配置文件的流对象InputStream inputStream = TestFramework.class.getClassLoader().getResourceAsStream("SqlMapConfig.xml");//构建sqlSessionFactory对象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//生产SqlSession对象SqlSession sqlSession = sqlSessionFactory.openSession();//调用selectList方法List list = sqlSession.selectList("com.william.dao.UserDao" + "." + "findAll");for (Object o : list) {System.out.println(o);}//释放资源sqlSession.close();}
}