MyBatis入门及配置

使用MyBatis,只需将mybatis-x.x.x.jar文件配置到classpath中即可
官网 http://mybatis.github.io/
下载路径:https://github.com/mybatis/mybatis-3/releases
文档:http://mybatis.github.io/mybatis-3/zh/getting-started.html

MyBatis入门配置
MyBatis核心配置文件:



<configuration>
    
    <properties resource="jdbc.properties">properties>
    <environments default="development">
        <environment id="development">
            
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="${driver}" />
                <property name="url" value="${url}" />
                <property name="username" value="${username}" />
                <property name="password" value="${password}" />
            dataSource>
        environment>
    environments>
    <mappers>
        
        <mapper resource="指定xml配置文件/xxx.xml"/>
        
        <mapper class="指定已定义的接口的类路径" />
    mappers>
configuration>

使用xml的方式时需要定义的xml配置文件



    
<mapper namespace="定义的接口的类的全路径">
    
    <select id="接口中对应的方法" resultType="java.util.Map">
        放入需要执行的sql语句(select * from user)
    select>
mapper>

使用注解的方式只需要在接口中的方法上边加上注解即可

public interface MybatisAdvance {
    /**
     * 查询用户信息
     */
    @Select("SELECT * FROM USER")
    public List queryUser();
}

传参方式:
     # { 键 }:实际上是使用占位符“ ? ”的方式来替代,接口绑定可以通过索引的方式# { 0 }传参,默认从0开始
      ${ 键 }:不安全,不会识别字符串,相当于“+值+”,直接替换对应的值

你可能感兴趣的:(MyBatis)