Mybatis初体验

快速入门

项目视图

Mybatis初体验_第1张图片

步骤

  1. 创建Maven工程;
  2. 添加依赖;
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>x.x.x</version>
</dependency>
  1. resources文件夹创建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.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    
<!--注册mapper,路径用斜杆不用点-->
    <mappers>
        <mapper resource="com/jie/dao/EmpMapper.xml"/>
    </mappers>
</configuration>
  1. 创建mybatis工具类
public class mybatisUtil {
     
    private static SqlSessionFactory sqlSessionFactory;
    static{
     
        String resource="mybatis-config.xml";
        InputStream inputStream = null;
        try {
     
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
     
            e.printStackTrace();
        }
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }
    public static SqlSession getSqlSession(){
     
        return sqlSessionFactory.openSession();
    }
}
  1. pojo层写实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {
     
    private int empno;
    private String ename;
    private String job;
    private Date hiredate;
    private float sal;
}
  1. mapper层写接口和实现接口的xml文件
public interface EmpMapper {
     
    List<Emp> getEmpList();
}

<?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">
<!--需要maven资源过滤-->
<!--绑定一个接口-->
<mapper namespace="com.jie.dao.EmpMapper">
<!--    文件写全限定名-->
    <select id="getEmpList" resultType="com.jie.pojo.Emp">
        select * from emp
    </select>
</mapper>
  1. pom.xml文件中设置Maven资源过滤
 <build>
        <!--        资源过滤-->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties
                    **/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties
                    **/*.xml </include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

测试结果

Mybatis初体验_第2张图片

public class EmpMapperTest {
     
    @Test
    public void getEmpListTest(){
     
        SqlSession sqlSession = mybatisUtil.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        List<Emp> empList = mapper.getEmpList();
        for (Emp emp : empList) {
     
            System.out.println(emp);
        }
        sqlSession.close();
    }
}

Mybatis初体验_第3张图片

你可能感兴趣的:(JDBC)