码迷,mamicode.com
首页 > 其他好文 > 详细

Mybatis 之入门模板

时间:2020-08-24 16:55:16      阅读:51      评论:0      收藏:0      [点我收藏+]

标签:rjava   result   select   数据   pen   url   class   cep   log   

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";
       // FileInputStream fileInputStream = new FileInputStream(path);
        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());
    }
}

Mybatis 之入门模板

标签:rjava   result   select   数据   pen   url   class   cep   log   

原文地址:https://www.cnblogs.com/zhenqk/p/13551954.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!