Mybatis 之 xml 配置映射文件

1.导入依赖

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.5</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

</project>

2. 主配置文件

<?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>
    <settings>
        <!--SQL日志-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

    <!-- 配置环境 -->
    <environments default="mysql">
        <!-- 配置mysql的环境-->
        <environment id="mysql">
            <!-- 配置事务的类型-->
            <transactionManager type="JDBC"></transactionManager>
            <!-- 配置数据源(连接池) -->
            <dataSource type="POOLED">
                <!-- 配置连接数据库的4个基本信息 -->
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/kdb1"/>
                <property name="username" value="root"/>
                <property name="password" value="rootpy2018"/>
            </dataSource>
        </environment>
    </environments>
    <!-- Mapper 映射文件  -->
    <mappers>
        <mapper resource="cn/ccut/mapper/UserMapper.xml"></mapper>
    </mappers>
</configuration>

3. UserMapper.java

public interface UserMapper {
    List<User> findAllUser();
}

4. UserMapper.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="cn.ccut.mapper.UserMapper">
    <select id="findAllUser" resultType="cn.ccut.domain.User">
select * from user
    </select>
</mapper>

5. 测试

public class TestA {
    @Test
    public void sta() throws IOException {
        String path="mybatis-config.xml";
        InputStream in = Resources.getResourceAsStream(path);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        System.out.println(mapper.findAllUser());
    }
}

posted @ 2020-08-24 09:05  Tony小哥  阅读(411)  评论(0编辑  收藏  举报