分析流程


 

1.创建 Maven 工程

工程信息如下:
Groupid :cn.oesoft
ArtifactId :mybatis02
Packing :jar
 
 
 
<dependencies>
        <!-- 日志坐标 -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <!-- 解析 xml 的 dom4j -->
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
        <!-- mysql 驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <!-- dom4j 的依赖包 jaxen -->
        <dependency>
            <groupId>jaxen</groupId>
            <artifactId>jaxen</artifactId>
            <version>1.1.6</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

 

编写sqlmapconfig.xml

<?xml version="1.0" encoding="UTF-8" ?> 
<configuration> 
 <environments default="development"> 
 <environment id="development"> 
 <transactionManager type="JDBC" /> 
 <dataSource type="POOLED"> 
 <property name="driver" value="com.mysql.jdbc.Driver" ></property> 
 <property name="url" value="jdbc:mysql:///eesy" ></property> 
 <property name="username" value="root"></property>
 <property name="password" value="1234"></property>
 </dataSource> 
 </environment> 
 </environments>
</configuration>

编写读取配置文件的类

public class Resources {
 
    /**
     * 根据传入的参数,获取一个字节输入流
     * @param filePath
     * @return
     */
    public static InputStream getResourceAsStream(String filePath){
        return Resources.class.getClassLoader().getResourceAsStream(filePath);
    }
}

编写 Mapper 

/**
 * 用于封装执行的SQL语句和结果类型的全限定类名
 */
public class Mapper {
 
    private String queryString;//SQL
    private String resultType;//实体类的全限定类名
 
    public String getQueryString() {
        return queryString;
    }
 
    public void setQueryString(String queryString) {
        this.queryString = queryString;
    }
 
    public String getResultType() {
        return resultType;
    }
 
    public void setResultType(String resultType) {
        this.resultType = resultType;
    }
}

编写 Configuration 配置类

/**
 * 自定义mybatis的配置类
 */
public class Configuration {
 
    private String driver;
    private String url;
    private String username;
    private String password;
 
    private Map<String,Mapper> mappers = new HashMap<String,Mapper>();
 
    public Map<String, Mapper> getMappers() {
        return mappers;
    }
 
    public void setMappers(Map<String, Mapper> mappers) {
        this.mappers.putAll(mappers);//此处需要使用追加的方式
    }
 
    public String getDriver() {
        return driver;
    }
 
    public void setDriver(String driver) {
        this.driver = driver;
    }
 
    public String getUrl() {
        return url;
    }
 
    public void setUrl(String url) {
        this.url = url;
    }
 
    public String getUsername() {
        return username;
    }
 
    public void setUsername(String username) {
        this.username = username;
    }
 
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
}

编写 User 实体类

@Data
public class User {
    private Integer id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;
 
    }