mybatis配置文件(properties、typeAliases、mappers( resource、class、url、package))

1、properties:读取文件信息

(1)jdbc配置文件:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/stu_mangement
jdbc.username=root
jdbc.password=root

(2)核心配置文件:

指定jdbc文件的位置:

<properties resource="jdbc.properties"/>

读取文件内容:

<dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}" />
                <property name="url" value="${jdbc.url}" />
                <property name="username" value="${jdbc.username}" />
                <property name="password" value="${jdbc.password}" />
</dataSource>

2、typeAliases:类型别名

(1)配置前:需要写类的全路径

    <select id="findStudentBysname" parameterType="String" resultType="pers.zhb.pojo.Student">
        select * from student where sname like "%"#{value}"%"
    </select>

(2)配置别名后:

核心配置文件:

<typeAliases>
        <typeAlias type="pers.zhb.pojo.Student" alias="Student"/>
</typeAliases>

student.xml:

    <select id="findStudentBysname" parameterType="String" resultType="Student">
        select * from student where sname like "%"#{value}"%"
    </select>

如果同一包下有多个pojo采用上面的方法显然是不行的,下面的配置可以扫描该包及其子包下的所有pojo:

 <select id="findStudentById" parameterType="Integer" resultType="Student">
        select * from student where studentno = #{v}
 </select>

3、mappers:映射器

(1) <mapper resource=" " />

    <mappers>
        <mapper resource="sqlmap/student.xml"/>
    </mappers>

直接指定文件的路径即可。

(2) <mapper class=""/>

核心配置文件:

<mappers>
        <mapper class="pers.zhb.mapper.StudentMapper"/>
</mappers>

注意:要求mapper接口名称和mapper映射文件名称相同,且放在同一个目录中。

(3)<package url=""/>

指定文件的完全路径。

(4)package属性:

<mappers>
        <package name="pers.zhb.mapper"></package>
</mappers>

原文地址:https://www.cnblogs.com/zhai1997/p/12530641.html

时间: 2024-08-04 13:15:48

mybatis配置文件(properties、typeAliases、mappers( resource、class、url、package))的相关文章

Mybatis学习记录(三)--Mybatis配置文件详解

关于mybatis的配置文件,主要有两个,一个是SqlMapperConfig.xml文件一个是mapper.xml文件,分别对这两个进行深入全面学习. 一.SqlMapperConfig.xml文件 1.标签概况 在SqlMapperConfig.xml中主要有以下标签,其中环境集合environments和spring整合后废除不用.objectFactory和plugins不经常使用. properties(属性) settings(全局配置参数) typeAliases(类型别名) ty

mybatis 配置文件参数说明

本文转自:http://www.yihaomen.com/article/java/302.htm 1. 配置文件参数说明: mybatis 配置文件:Configuration.xml : <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://m

【原创】mybatis配置文件报错:The content of element type &quot;configuration&quot; must match &quot;(properties&gt;settings&gt;typeAliases&gt;typeHa...

最近使用mybatis,在mybatis的xml配置文件中总是会提示错误 The content of element type "configuration" must match "(properties>settings>typeAliases>typeHandlers>objectFactory>objectWrapperFactory>plugins>environments>mappers?)". 后来发现

MyBatis配置文件(一)――properties属性

MyBatis配置文件中有很多配置项,这些配置项分别代表什么,有什么作用,需要理一下了.先通过下面这个例子来看都有哪些配置项 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

Spring/Maven/MyBatis配置文件结合properties文件使用

使用properties文件也叫注入,比如把一些常用的配置项写入到这个文件,然后在Spring的XML配置文件中使用EL表达式去获取. 这种方式不只Spring可以使用,同样MyBatis也可以使用,只不过加载的方式不一样,但是获取值同样是EL表达式.具体的参考官方文档. properties语法参考:https://zh.wikipedia.org/wiki/.properties,注意转移字符. Spring: 本次使用的例子来自这章http://www.cnblogs.com/EasonJ

MyBatis 配置文件基本结构

一.MyBatis 配置文件基本结构 在使用mybatis框架时,首先导入其对应的jar包,并进行相应的配置,所以得对配置文件的每个参数都得了解.一个完全的mybatis配置文件结构如下: [html] view plain copy <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0/

MyBatis——配置文件

mybatis配置文件 转载:http://loveshisong.cn/mybatis/2015/01/20/MyBatis(%E4%BA%8C)%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6.html MyBatis 的配置文件即mybatis-config.xml中的configuration标签下有settings.properties等属性.本文对其中常用属性的配置做简单介绍 本文结构 properties 属性 typeAliases 类型别名 typeHa

MyBatis配置文件mybatis-config.xml

<?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"> <!--通过这个配置文件,完成mybatis与数据库的连接 --> <confi

MyBatis学习 之 四、MyBatis配置文件

四.MyBatis主配置文件 在定义sqlSessionFactory时需要指定MyBatis主配置文件: Xml代码   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-config.xml&qu