文章目录
-
-
- 配置文件:mybatis-config.xml
-
- 1. properties(属性)标签
- 2. 设置(settings)
- 3. 类型别名(typeAliases)标签
- 4.mappers标签
-
- 4.1 class:引用接口的全类名
- 4.2 resource:在类路径下找sql映射文件
-
配置文件:mybatis-config.xml
1. properties(属性)标签
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis_01?
useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123"/>
</dataSource>
这些属性都是可外部配置且可动态替换的,既可以在典型的 Java 属性文件中配置,亦可通过 properties 元素的子元素来传递。
下面讲通过java属性文件配置
写一个属性配置文件db.properties:
username=root
password=123
jdbcurl=jdbc:mysql://localhost:3306/mybatis_01?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
driverClass=com.mysql.jdbc.Driver
在全局配置文件中,可以发现properties标签有两个属性:resource/url
resource:
从类路径下开始引用
url:
从磁盘路径或者网络路径下引用
<configuration>
<properties resource="db.properties"/>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!--配置连接池-->
<dataSource type="POOLED">
<!--使用${}取出配置文件的值-->
<property name="driver" value="${driverClass}"/>
<property name="url" value="${jdbcurl}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
</configuration>
2. 设置(settings)
正常情况下,数据库的字段名要和JavaBean对象的属性名一致,比如数据库中叫empname(数据库字段名不区分大小写),那么JavaBean的对象名就叫empName;
如果字段名和属性名不相同,就会导致封装不上,比如看下面的实例:
查询数据库发现loginAccount属性没有封装上为null…
如何解决,就是使用setting标签的属性mapUnderscoreToCamelCase开启自动驼峰命名规则的映射
<!--name:配置项的key,value:配置项的值-->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
3. 类型别名(typeAliases)标签
首先看下映射配置文件中需要配置实体类的全路径类名,如果有很多处就比较麻烦
<mapper namespace="com.hh.dao.EmployeeDao">
<select id="getEmpById" resultType="com.hh.bean.Employee">
select * from t_employee where id=#{id}
</select>
</mapper>
这时我们可以在全局配置文件中使用typeAliases标签,配置这个类的全路径类名的别名
<!--就是为一个JavaBean起别名,别名默认就是类名(不区大小写)-->
<typeAliases>
<typeAlias type="com.hh.bean.Employee"/>
</typeAliases>
此时映射配置文件中不用写JavaBean对象的全类名了,可以写默认的类名
<select id="getEmpById" resultType="Employee">
select * from t_employee where id=#{id}
</select>
如果不想使用默认的类名,可以指定一个别名:
<!--就是为一个JavaBean起别名,别名默认就是类名(不区大小写)-->
<typeAliases>
<typeAlias type="com.hh.bean.Employee" alias="emp"/>
</typeAliases>
<select id="getEmpById" resultType="emp">
select * from t_employee where id=#{id}
</select>
那么问题来了,如果JavaBean比较少,这种方式还可以一个一个的进行配置,可是如果比较多呢?此时可以这样使用:
<typeAliases>
<!--批量其别名,name:指定报名,默认就是类名-->
<package name="com.hh.bean"/>
</typeAliases>
但是,总体而言,还是希望不要起别名,使用全类名以便修改和直观。
4.mappers标签
写好的sql映射文件需要使用mappers注册进来,也就是说告诉MyBatis去哪里找这个sql映射文件。
路径的写法有三种方式:
1.class:引用接口的全类名
需要将xml文件和接口放在同一个目录下面,并且文件名和接口名必须相同
2.resource:在类路径下找sql映射文件
需要将xml文件和接口放在同一个目录下面,且文件名和接口名必须相同
3.url:从磁盘或者网络路径引用
4.1 class:引用接口的全类名
<mappers>
<mapper class="com.hh.dao.EmployeeDao"/>
</mappers>
如果出现绑定异常,先看EmployeeDao.xml有没有部署进来,没有的话先检查该文件有没有错误,如果没有去我博客中常见异常报错中找处理方法
class的另一种用法:对于注解版的sql语句不需要写配置文件,此时就只能通过class来注册了
public interface EmployeeDaoAnnotation {
@Insert("insert into t_employee(empname,gender,email) values (#{empName},#{gender},#{email})")
public int insertEmployee(Employee employee);
@Delete("delete from t_employee where id=#{id}")
public boolean deleteEmployee(int id);
@Update("set empname=#{empName},gender=#{gender},email=#{email} where id=#{id}")
public int updateEmployee(Employee employee);
@Select("select * from t_employee where id=#{id}")
public Employee getEmpById(int id);
}
此时只能通过class来注册,而不能使用resource
<mappers>
<!-- <mapper class="com.hh.dao.EmployeeDao"/>-->
<mapper class="com.hh.dao.EmployeeDaoAnnotation"/>
</mappers>
但是不建议这样使用,因为好不容易把sql语句从类中提取出来写成了xml文件,如果写在类上,又复杂了。
4.2 resource:在类路径下找sql映射文件
要求:需要将xml文件和接口放在同一个目录下面,且文件名和接口名必须相同
<mappers>
<mapper resource="com/hh/dao/Employee.xml"/>
</mappers>
如果不在一个目录下,就会报错。。。。。
那么问题有了,如果只有少量的xml文件需要注册,饿哦们可以一个个的进行注册,可是如果有很多个,如果这样注册肯定很麻烦。发现mappers还有一个属性package。
下面介绍批量注册:
要求:需要将xml文件和接口放在同一个目录下面,且文件名和接口名必须相同
name属性:dao接口所在的包名
<mappers>
<package name="com.hh.dao"/>
</mappers>