image.png
如需要在MyBatis核心配置文件中进行修改配置,应按照如上顺序进行修改

properties(属性)

image.png
image.png
image.png

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <!-- 引用外部配置文件 -->
  7. <properties resource="db.properties">
  8. <!-- 里面是错的信息也没事 -->
  9. <property name="username" value="周希程"></property>
  10. <property name="password" value="111111"></property>
  11. </properties>
  12. <environments default="development">
  13. <environment id="development">
  14. <transactionManager type="JDBC"/>
  15. <dataSource type="POOLED">
  16. <property name="driver" value="${driver}"/>
  17. <property name="url" value="${url}"/>
  18. <property name="username" value="${username}"/>
  19. <property name="password" value="${password}"/>
  20. </dataSource>
  21. </environment>
  22. </environments>
  23. </configuration>
<?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">
<!-- namespace=绑定一个对应的Dao/Mapper接口 -->
<!-- 等同于JDBC中的UserDaoImpl(接口实现类),转变为Mapper配置文件 -->
<mapper namespace="com.comprehensive.dao.UserMapper">

    <!-- 引用外部配置文件 -->
    <select id="getUserList" resultType="com.comprehensive.pojo.User">
        select * from mybatis.user
    </select>

</mapper>
//测试类
public class UserMapper_Test {
    @Test
    //引用外部配置文件
    public void test1() {
        SqlSession sqlSession = MyBatis_Util.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        System.out.println(mapper.getUserList());
    }
}

settings(设置)

具体的可以查看官方文档,项目中需要配置的时候再查看文档

typeAliases(别名)

image.png

<?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>

    <!-- 引用外部配置文件 -->
    <properties resource="db.properties">
        <!-- 里面是错的信息也没事 -->
        <property name="username" value="周希程"></property>
        <property name="password" value="111111"></property>
    </properties>

    <!-- 类型别名 -->
    <typeAliases>
        <typeAlias type="com.comprehensive.pojo.User" alias="User"></typeAlias>
        <!--
            也可以指定一个包名,MyBatis 会在包名下面搜索需要的 Java Bean,比如:
            <package name="com.comprehensive.pojo"></package>
            返回类型的首个字母应是小写
        -->
        <package name="com.comprehensive.pojo"></package>
    </typeAliases>

    <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>

    <!-- 映射器 -->
    <!-- 每一个mapper.xml都需要在MyBatis核心配置文件中注册 -->
    <mappers>
        <!-- 方式一:resource -->
        <mapper resource="com/comprehensive/dao/UserMapper.xml"/>
        <!-- 方式二:class -->
        <!--<mapper class="com.comprehensive.dao.UserMapper"></mapper>-->
        <!-- 方式三:package -->
        <!--<package name="com.comprehensive.dao"></package>-->
    </mappers>

</configuration>
<?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">
<!-- namespace=绑定一个对应的Dao/Mapper接口 -->
<!-- 等同于JDBC中的UserDaoImpl(接口实现类),转变为Mapper配置文件 -->
<mapper namespace="com.comprehensive.dao.UserMapper">

    <!-- 类型别名 typeAlias -->
    <select id="getUserById" parameterType="int" resultType="User">
        select * from mybatis.user where id=#{id}
    </select>

    <!-- 类型别名 package -->
    <select id="getUserByName" parameterType="String" resultType="user">
        select * from mybatis.user where name=#{name}
    </select>

    <!-- 类型别名 注解 -->
    <select id="getUserByPassword" parameterType="String" resultType="u">
        select * from mybatis.user where pwd=#{pwd}
    </select>

</mapper>
//实体类
@Alias("u")
public class User {
    private int id;
    private String name;
    private String pwd;

    public User() {}

    public User(int id, String name, String pwd) {
        this.id = id;
        this.name = name;
        this.pwd = pwd;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", pwd='" + pwd + '\'' +
                '}';
    }
}
//测试类
public class UserMapper_Test {
    @Test
    //类型别名 typeAlias
    public void test2() {
        SqlSession sqlSession = MyBatis_Util.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        System.out.println(mapper.getUserById(1));
    }
    @Test
    //类型别名 package
    public void test3() {
        SqlSession sqlSession = MyBatis_Util.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        System.out.println(mapper.getUserByName("李杰亮"));
    }
    @Test
    //类型别名 注解
    public void test4() {
        SqlSession sqlSession = MyBatis_Util.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        System.out.println(mapper.getUserByPwd("123456"));
    }
}

plugins(插件)

具体的可以查看官方文档

environments(环境配置)

MyBatis可以配置成适应多种环境
不过需要注意的是:尽管可以配置多个环境,但每个SqlSessionFactory实例只能选择一种环境
MyBatis默认的事务管理器:JDBC;MyBatis默认的连接池:POOLED

mappers(映射器)

image.png

<?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>

    <!-- 引用外部配置文件 -->
    <properties resource="db.properties">
        <!-- 里面是错的信息也没事 -->
        <property name="username" value="周希程"></property>
        <property name="password" value="111111"></property>
    </properties>

    <!-- 类型别名 -->
    <typeAliases>
        <typeAlias type="com.comprehensive.pojo.User" alias="User"></typeAlias>
        <!--
            也可以指定一个包名,MyBatis 会在包名下面搜索需要的 Java Bean,比如:
            <package name="com.comprehensive.pojo"></package>
            返回类型的首个字母应是小写
        -->
        <package name="com.comprehensive.pojo"></package>
    </typeAliases>

    <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>

    <!-- 映射器 -->
    <!-- 每一个mapper.xml都需要在MyBatis核心配置文件中注册 -->
    <mappers>
        <!-- 方式一:resource -->
        <mapper resource="com/comprehensive/dao/UserMapper.xml"/>
        <!-- 方式二:class -->
        <!--<mapper class="com.comprehensive.dao.UserMapper"></mapper>-->
        <!-- 方式三:package -->
        <!--<package name="com.comprehensive.dao"></package>-->
    </mappers>

</configuration>