java mybatis 使用步骤

2021-01-15 19:13

阅读:816

标签:url   host   void   key   property   local   java   from   interface   

1. Pom 文件依赖
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.0modelVersion>

    groupId>cn.monkeygroupId>
    artifactId>java_mybatis_01artifactId>
    version>1.0-SNAPSHOTversion>
    packaging>jarpackaging>

    dependencies>
        dependency>
            groupId>org.mybatisgroupId>
            artifactId>mybatisartifactId>
            version>3.4.5version>
        dependency>
        dependency>
            groupId>mysqlgroupId>
            artifactId>mysql-connector-javaartifactId>
            version>5.1.6version>
        dependency>
        dependency>
            groupId>log4jgroupId>
            artifactId>log4jartifactId>
            version>1.2.12version>
        dependency>
        dependency>
            groupId>junitgroupId>
            artifactId>junitartifactId>
            version>4.12version>
        dependency>
    dependencies>


project>

2.1  sql 配置

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="mysql">
        
        environment id="mysql">
            
            transactionManager type="JDBC">transactionManager>
            
            dataSource type="POOLED">
                
                property name="driver" value="com.mysql.jdbc.Driver"/>
                property name="url" value="jdbc:mysql://localhost:3306/eesy_mybatis"/>
                property name="username" value="root"/>
                property name="password" value="root"/>
            dataSource>
        environment>
    environments>

    mappers>
        mapper resource="cn/monkey/dao/UserDao.xml">mapper>
    mappers>

configuration>

2.2 mapper 配置文件

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.monkey.dao.UserDao">
    select id="findAll" resultType="cn.monkey.domain.User">
        SELECT * FROM USER
    select>
mapper>

3. 创建 javaBean 和 Dao 层的 interface

4. 调用 java api 调用

public void test1 () throws IOException {
        // 1. 读取配置文件
        InputStream is = Resources.getResourceAsStream("sqlMapConfig.xml");
        // 2. 创建 SqlSessionFactory 工厂
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(is);
        // 3. 使用工厂类创建 SqlSesson 对象
        SqlSession session = factory.openSession();
        // 4. 使用 sqlSession 对象创建 Dao 层的代理对象
        UserDao userDao = session.getMapper(UserDao.class);
        // 5. 使用代理对象执行方法
        List users = userDao.findAll();
        for (User user : users) {
            System.out.println(user);
        }
        // 6. 释放资源
        session.close();
        is.close();
    }

 

java mybatis 使用步骤

标签:url   host   void   key   property   local   java   from   interface   

原文地址:https://www.cnblogs.com/monkey-K/p/12934230.html


评论


亲,登录后才可以留言!