mybatis配置文件

名字:

mybatis-config.xml

作用:

配置自己的数据库地址、名字、密码以及mysql驱动

...
<!--配置-->
<configuration>
	      
	<!--属性-->
	<properties></properties> 
	<!--全局参数设置-->
	<settings></settings>     
	<!--类型别名-->
	<typeAliases></typeAliases>
	<!--类型处理器-->
	<typeHandles></typeHandles>
	<!--对象工厂-->
	<objectFactory></objectFactory>   
	<!--插件-->    
	<plugins></plugins>
	<!--环境信息集合:起步学习只需要关注一下此处配置--> 
	<environments>
		<!--单个环境信息-->  
	    <environment>
	    	<!--事务-->  
	        transactionManager
	        <!--数据源-->
	        dataSource
	    </environment>
	</environments>
	<!--数据库厂商标识-->
	databaseIdProvider
	<!--映射器-->
	<mappers></mappers>
	
</configuration>
...

例子:

<?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核心配置文件-->
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSl=trur&amp;sueUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=Asia/Shanghai"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
</configuration>

properties 标签

1.如果两个配置文件有同一个字段,优先使用外部配置文件的
2.可以直接引入外部配置文件,properties子元素中可以增加一些属性配置

在这里插入图片描述

在这里插入图片描述

typeAliases 标签

typeAliases类型别名是为java类型设置一个短的名字,存在的意义仅在于用来减少类完全限定名的冗余。

java内置内建类型别名它们都不区分大小写,注意对基本类型名称重复采用的特殊命名风格。

在这里插入图片描述

注意:

方法二中,每一个在包中的Java bean,在没有注解的情况下,会使用bean的首字母小写的非限定类名来作为它的别名。若有注解,则别名为其注解值。(实体类上使用注解:@Alias(“user”))

 <!--MyBatis对常用类有默认别名支持,比如java.lang.Stirng的别名为string。
    除此之外,我们也可以使用 <typeAliases> 设置自定义别名。-->
<typeAliases>
    <!--为某一个类来配置别名-->
    <typeAlias type="全类名" alias="别名"></typeAlias>
    <typeAlias type="com.cueb.entity.User" alias="user"></typeAlias>
    <!--为一个所有包下的所有类配置别名.此时该包下的所有类都有了别名,别名省略包名,和类名相同。  -->
    <package name="包名"/>
</typeAliases>

setting 标签

setting设置标签,这是Mybatis中极为重要的调整设置,它们会改变Mybatis的运行时行为.

在这里插入图片描述

environments标签

这个标签是最主要的标签,就是用来配置数据源的,基本上都是在这里配置数据库的地址,用户名,密码等等。

<?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核心配置文件-->
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSl=trur&amp;sueUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=Asia/Shanghai"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
</configuration>

dataSource子标签的type属性表达采用何种连接池方式,

  • POOLED

  • UNPOOLED

  • JNDI

mappers标签

mappers映射器属性,MapperRegistry:注册绑定我们的Mapper文件。有三种方式如下:

方法一

<mappers>
<!--使用相对路径注册映射文件-->
	<mapper resource="com/ferao/mapper/UserMapper.xml"/>
</mappers>	

方法二

<!--该方式绑定注册时,接口和它的Mapper配置文件必须同名,且它的Mapper配置文件必须在同一个包下-->
<mappers>
<!--注册持久层接口-->
	<mapper class="com.ferao.mapper.UserMapper" />
</mappers>

方法三

<!--该方式注入绑定时,接口和它的Mapper配置文件必须同名,且它的Mapper配置文件必须在同一个包下-->
<mappers>
<!--注册一个包下的所有持久层接口-->
	<package name="com.ferao.mapper"></package>
</mappers>

映射文件

名字:

UserMapper.xml

作用:

通过sql语句,将具体的对应文件返回。

三个重要的描述

parameterType(输入类型)

resultType(输出类型)

resultMap(映射实体类)

传递参数的方式

在mapper中传递多个参数的方式有四种:顺序传递法、@Param注解传参法、Map传参法、Java Bean传参法;

1、顺序传递参数

xxMapper.java

public User selectUser(String name, int deptId);

xxMapper.xml

<select id=“selectUser” resultMap=“UserResultMap”>

select * from user where user_name = #{0} and dept_id = #{1}

</select>

#{}里面的数字代表传入参数的顺序。这种方法不建议使用,sql层表达不直观,且一旦顺序调整容易出错。

2、@Param注解

xxMapper.java

public User selectUser(@Param(“userName”) String name, int Param(“deptId”)deptId);

xxMapper.xml

<select id=“selectUser” resultMap=“UserResultMap”>

select * from user where user_name = #{userName} and dept_id = #{deptId}

</select>

#{}里面的名称对应的是注解@Param括号里面修饰的名称。这种方法在参数不多的情况还是比较直观的,(推荐使用)。

3、Map传参法

xxMapper.java

public User selectUser(Map<String, Object> params);

xxMapper.xml

<select id=“selectUser” resultMap=“UserResultMap”>

select * from user where user_name = #{userName} and dept_id = #{deptId}

</select>

#{}里面的名称对应的是Map里面的key名称。这种方法适合传递多个参数,且参数易变能灵活传递的情况。

4、Java Bean传参数

xxMapper.java

public User selectUser(User user);

xxMapper.xml

<select id=“selectUser”

parameterType=“com.jourwon.pojo.User”

resultMap=“UserResultMap”>

select * from user where user_name = #{userName} and dept_id = #{deptId}

</select>

#{}里面的名称对应的是User类里面的成员属性。这种方法直观,需要建一个实体类,扩展不容易,需要加属性,但代码可读性强,业务逻辑处理方便,推荐使用。(推荐使用)。

parameterType(输入类型)

1、简单类型

2、pojo对象

mybaits使用OGNL表达式解析对象字段的值,#{}或者${}括号中的值为pojo属性名称

[注]OGNL表达式:object Graphic Navigation Language (对象图导航语言),它是通过对象的取值方法来获取数据。在写法上把get给省略了。比如:我们获取用户的名称类中的写法:user.getUsername(); OGNL表达式写法:user.username 那么,mybatis中为什么能直接写username,而不用user呢?因为在parameterType中已经提供了属性所属的类,所以此时不需要写对象名

3、pojo包装对象

开发中通过pojo传递查询条件,查询条件是综合的查询条件,不仅包括用户查询条件还包括其他的查询条件(比如将用户购买商品消息也作为查询条件),这时可以使用包装对象传递输入pojo类参数中包含pojo

输出结果方式 resultType

可输出的类型有四种:返回一般数据类型(单条)、JavaBean 类型(单条)、List类型(多条)、Map类型

1、一般数据类型(单条)

比如要根据Id属性获得数据库中的某个字段值

//**Mapper.java

String getStuNameById(Integer id);

<!–**Mapper.xml–>

<select id=“getStuNameById” resultType=“string”>

select username from t_student where id = #{id}

</select>

2、JavaBean类型(单条)

比如根据某个字段获得数据库中的信息,把查询的结果信息封装成某个javaBean类型的数据,

//**Mapper.java

Student getStuById(Integer id);

<!–**Mapper.xml–>

<select id=“getStuById” resultType=“student”>

select * from t_student where id = #{id}

</select>

3、List类型(多条)

有时候开发者查询的数据不止一条,比如,模糊查询,全表查询等,这时候返回的数据可能不止一条数据,对于多数据的处理可以存放在List集合中

//**Mapper.java

List<Student> getAllStus();

<!-- **Mapper.xml 注意这里的 resultType 返回值类型是集合内存储数据的类型,不是 ‘list’ -->

<select id=“getAllStus” resultType=“student”>

select * from t_student

</select>

4、Map类型

mybatis支持将查询的数据封装成Map,

如果查询的结果是一条,开发者可以把查询的数据以(表字段名,对应的值)方式存入到map中

//**Mapper.java

Map<String, Object> getStuAsMapById(Integer id);

<!-- **Mapper.xml 注意这里的 resultType 返回值类型是 ‘map’ -->

<select id=“getStuAsMapById” resultType=“map”>

select * from t_student where id = #{id}

</select>

如果查询的结果是多条数据,我们也可以把查询的数据以{表中某一字段名, JavaBean}方式来封装成Map

①mapper(dao)接口

// 查询所有学生的信息,把数据库中的 ‘id’ 字段作为 key,对应的 value 封装成 Student 对象
// @MapKey 中的值表示用数据库中的哪个字段名作 key
@MapKey(“id”)
Map<Integer, Student> getAllStusAsMap();

②Mapper.xml 映射文件

<!–注意 resultType 返回值类型,不再是 ‘map’,而是 Map 的 value 对应的 JavaBean 类型–>

<select id=“getAllStusAsMap” resultType=“student”>

select * from t_student

</select>

返回map集合时返回的类型是List<Map<String, Object>>

①mapper(dao)接口

List<Map<String, Object>> getAllStuAsMapById(Integer id);

②Mapper.xml 映射文件

<!–查询多条–>

<select id=“getAllStuAsMapById” parameterType=“int” resultType=“map”>

select * from usr

</select>

resultMap(映射实体类)

实体类属性名和表中字段名不一样时,无法映射到值,输出为Null。

这是因为mybatis会根据这些从数据库中查询到的列名,将列名转化为小写(数据库不区分大小写)去对应实体类中查询相应列名的set方法设值,由于找不到setUserName(),所以会返回Null值。

解决办法

方法一

第1种:通过在查询的SQL语句中定义字段名的别名,让字段名的别名和实体类的属性名一致。

<select id="getOrder" parameterType="int" resultType="com.jourwon.pojo.Order">
       select order_id id, order_no orderno ,order_price price form orders where order_id=#{id};
</select>

方法二

第2种:通过resultMap 中的<result>来映射字段名和实体类属性名的一一对应的关系。

<select id="getOrder" parameterType="int" resultMap="orderResultMap">
 select * from orders where order_id=#{id}
</select>
    
<resultMap type="com.jourwon.pojo.Order" id="orderResultMap">
    <!–用id属性来映射主键字段–>
    <id property="id" column="order_id">
    <!–用result属性来映射非主键字段,property为实体类属性名,column为数据库表中的属性–>
    <result property ="orderno" column ="order_no"/>
    <result property="price" column="order_price" />
</reslutMap>

resultMap的说明:

<!--column不做限制,可以为任意表的字段,而property须为type 定义的pojo属性-->
<resultMap id="" type="">
	<!--设置主键时使用,使用此标签配置映射关系(可能不止一个) -->
	<id column="" jdbcType="" property="" />
	<result column="" jdbcType="" property=""/>
	
	<association property="" javaType="">
	  <id column="" jdbcType="" property=""/>
	  <result  column="" jdbcType="" property=""/>
	</association>
	
	<!-- 集合中的property须为oftype定义的pojo对象的属性-->
	<collection property="pojo的集合属性" ofType="集合中的pojo对象">
	  <id column="集合中pojo对象对应的表的主键字段" jdbcType="字段类型" property="集合中pojo对象的主键属性" />
	  <result column="可以为任意表的字段" jdbcType="字段类型" property="集合中的pojo对象的属性" />
	</collection>
</resultMap>	

1)resultMap 标签

type属性:指开发者需要映射到的类对象

id属性:表示resultMap在select标签中使用时id的名称

方案一 -->为列名指定别名,别名和Java实体类的属性名一致 

	 <select id="findAll"  resultType="user">
	      select id,username as usernames from usr ;
	 </select>	

方案二 --> 使用结果集映射ResultMap [推荐]

	<resultMap id="UserMap" type="User">
        <!--id为主键-->
        <id column="id" property="id" />
        <!--column 是数据库表的名称,property是对应实体类的属性名-->
        <result column="username" property="usernames" />
    </resultMap>

	<!--查询所有-->
    <select id="findAll"  resultMap="UserMap">
      select id,username  from usr ;
    </select>

===========================标签描述====================================


--> 
	
	column   --> 表的主键字段,或者可以为查询语句中的别名字段
	jdbcType --> 字段类型
	property --> 映射pojo对象的主键属性

result标签 --> 属性用于配置映射关系时使用

	column   --> 表的一个字段(可以为任意表的一个字段)
	jdbcType --> 字段类型
	property --> 映射到pojo对象的一个属性(须为type定义的pojo对象中的一个属性)

association标签 --> 

	property --> pojo的一个对象属性
	javaType --> pojo关联的pojo对象

	id标签--> 
	
		column   --> 关联pojo对象对应表的主键字段
		jdbcType --> 字段类型
		property --> 关联pojo对象的主席属性	

	result标签 -->
	
		column   --> 任意表的字段
		jdbcType --> 字段类型
		property --> 关联pojo对象的属性
					
	select   -->表示所需要的哪个类的查询语句
	column   -->从resultMap中传过去用作查询参数
	ofType   -->集合属性中的对象(可以不写)

collection标签 -->

	property --> 表示pojo类集合中的属性
	select   -->表示所需要的哪个类的查询语句
	column   -->从resultMap中传过去用作查询的参数
	ofType   -->集合属性中的对象(可以不写)

===========================标签细节====================================

id & result

	示例

		<id property="id" column="post_id"/>
		<result property="subject" column="post_subject"/>

	含义
	
		这些是结果映射最基本的内容,id和result都将一个列的值映射到一个简单的数据类型

		的属性或字段。这两者之间的唯一不同是,id表示的结果将是对象的标识属性,这些在

		比较对象实例时用到,这样可以提高整体的性能,尤其是缓存和嵌套结果映射的时候
```
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值