MyBatis alias configuration
In sqlMapConfig.xml files often have to configure a lot of bean, the following code:
<!--别名配置-->
<typeAliases>
<!--type:指的使需要配置的代码,alias:指定一个别名,建议使当前类-->
<typeAlias type="com.***.mybatis.bean.Student" alias="student"></typeAlias>
</typeAliases>
But often these classes in the bean directory, so MyBatis launched a package scanning this property, as follows
<!--别名配置-->
<typeAliases>
<!--指定包名,指定后,别名默认就是包下的类名-->
<package name="com.***.mybatis.bean"/>
</typeAliases>
This eliminates the need to prepare excessive <typeAlias> </ typeAlias>, and the lower dao corresponding mapper can directly reference the file, such as the following is written directly in the resultType the student class, and case insensitive
<!--查询所有学生-->
<!--id:接口中类方法名,resultType:结果集的返回类型-->
<select id="findAll" resultType="student">
select * from tb_stu;
</select>
But there are still many mapper.xml to be configured,
and in order not to repeat the configuration file Mapperxml, we can write
<mappers>
<!--当前包需要和mapper接口类所在包同名,并且当前包需要一级一级创建-->
<package name="com.houpu.mybatis.mapper"/>
<!-- <mapper resource="mapper/StudentMapper.xml"></mapper>-->
</mappers>
And often we are so configured, wasted too much time, development efficiency will be much slower
<mappers>
<mapper resource="mapper/StudentMapper.xml"></mapper>
</mappers>
But one thing to note: when referencing mapper.xml hierarchy to Dao and Mapper is to be consistent,
and the packet to create a level as follows:
Mapper files in three directories, so the xml files have the same; as follows:
such references will succeed! ! !
Finally write about, reference sql configuration file
to create a new file the following documents:
db.driverName=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql:///数据库名
db.username=账号
db.password=密码
In sqlMapConfig.xml file as follows:
<!--配置properties读取外部文件,注意:上下顺序-->
<properties resource="file的路径名"></properties>
<!--配置环境-->
<environments default="abc">//default随便编写
<!--环境中定义了,要访问的数据库连接池,事务管理类型-->
<environment id="abc">
<transactionManager type="JDBC"></transactionManager>
<!--数据库连接池-->
<dataSource type="POOLED">
<property name="driver" value="${db.driverName}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</dataSource>
</environment>
</environments>
Note that the above files must be written in <configuration> sqlMapConfig.xml file </ configuration> tag