Mybatis零基础教程(1)

1. 创建Maven工程,pom.xml文件的配置如下:

<?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.cc.mybatis</groupId>
    <artifactId>Mybatisdemo01</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.36</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        
    </dependencies>

</project>

需要配置mybatis, mysql驱动,日历log4j, 测试包junit

2. 在resources目录下,添加mybatis的mysql配置文件,例如Mybats-mysql.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="dev">
        <environment id="dev">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/student" />
                <property name="username" value="root" />
                <property name="password" value="1234" />
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="com/cc/mybatis/dao/studentMapper.xml"></mapper>
    </mappers>
</configuration>

SSM是面向接口编程,在查询数据时,需要些接口,然后再xml文件中配置,例如有一个dao的接口如下:

package com.cc.mybatis.dao;

import com.cc.mybatis.entity.student;

import java.util.List;

public interface studentDao {

    List<student> findAll();
}

其中 findAll()的方法,不需要在他的继承类中实现,只需要在xml中配置,如下:

<?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="com.cc.mybatis.dao.studentDao">

    <select id="findAll" resultType="com.cc.mybatis.entity.student">
        select * from stinfo
    </select>

</mapper>

注意点如下:

  (1)mapper namespace的内容是dao类的全路径

  (2)查询用select标签

  (3)查询的结果用resultType标签

  (4)id的值是接口中的方法名

做好这些即可测试代码了,例如:

// 1. 读取mybatis配置
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        // 2. 创建SqlSessionFactory
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(inputStream);
        // 3. 使用工厂生产SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 4. 使用SqlSession对象创建Dao接口的代理对象
        studentDao studentDao = sqlSession.getMapper(studentDao.class);
        // 5. 使用代理对象执行方法
        List<student> studentList = studentDao.findAll();
        for (student stu : studentList) {
            System.out.println(stu);
        }
        // 6. 释放资源
        sqlSession.close();
        inputStream.close();

猜你喜欢

转载自blog.csdn.net/yao_hou/article/details/88319194