在前面的例子中,我们直接在mybatis-config.xml文件里面写死了用户名、密码、url等属性。
现在进行优化,将这些用标签
- 新建一个外部配置文件application.properties,内容如下:
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC username=root password=pan
- 修改mybatis-config.xml文件内容,如下:
类型别名(typeAliases)
- 方式一:直接在mybatis-config.xml中增加
之后在mapper.xml中,resultType的值,则可以直接使用User。
- 方式二:在mybatis-config.xml中指定包名,然后在实体类上增加注解,自定义类名
实体类增加注解,后面在mapper.xml中,resultType的值,可以直接使用注解的值。
@Alias("hello")
public class User {
private int id;
private String name;
private String passWord;
}
设置(settings)
在mybatis-config.xml中,可以进行一些设置,改变mybatis的运行时行为。主要的设置有:
在实际使用时,并不需要都加进去,只要按自己所需,加上常用的即可。
映射器(mappers)- 方式一
- 方式二
使用class文件绑定注册
注意:(1)接口与其Mapper配置文件必须同名(2)接口与其Mapper配置文件必须在同一个包下
- 方式三
使用扫描包进行注入绑定
注意:(1)接口与其Mapper配置文件必须同名(2)接口与其Mapper配置文件必须在同一个包下



