主配置文件
要使用的properties文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mydb
jdbc.username=root
jdbc.password=admin
一个主配置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的主配置文件 -->
<configuration>
<!--配置参数-->
<settings>
<!--开启Mybatis支持延迟加载-->
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"></setting>
<!--开启二级缓存-->
<setting name="cacheEnabled" value="true"/>
</settings>
<!--配置properties:
可以在标签内部链接的数据库的信息,也可以通过属性引用外部配置文件的信息
resource属性:(常用)
用于指定配置文件的位置,是按照类路径的写法来写的,并且比逊存在类路径下
url属性:
要求按照url的写法来写地址(文件协议)
url:同一资源定位符,可唯一表示一个资源的位置
-->
<!--1. 使用url得到4个基本信息 文件协议,这三中方式使用时任意选一种就好-->
<properties url="file:///D:/IntelliJIDEAWorkProject/mybatiss_Test/day01_03_crud/src/main/resources/jdbcConfig.properties"></properties>
<!--2. 外部配置文件得到4个基本信息,这三中方式使用时任意选一种就好-->
<properties resource="jdbcConfig.properties"></properties>
<!--3. 配置连接数据库的4个基本信息,这三中方式使用时任意选一种就好-->
<properties>
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/gq_day18"/>
<property name="username" value="root"/>
<property name="password" value="admin"/>
</properties>
<!--使用typeAliases配置别名,他只能配置domain中类的别名-->
<typeAliases>
<!--typeAlias用于配置别名,type属性指定的实体类全限定类名,alias属性指定别名,就像int/INT/Intager等等那样使用了-->
<typeAlias type="cn.itcast.domain.User" alias="user"></typeAlias>
<typeAlias type="cn.itcast.domain.User" alias="UESR"></typeAlias>
<!--如果想配置多一点,使用这个麻烦了,package用于指定要配置别名的包,当指定之后该包下的实体类都会被注册别名,并且类名就是别名,不再区分大小写-->
<package name="cn.itcast.domain"/>
</typeAliases>
<!-- 配置环境 -->
<environments default="mysql">
<!-- 配置mysql的环境-->
<environment id="mysql">
<!-- 配置事务的类型-->
<transactionManager type="JDBC"></transactionManager>
<!-- 配置数据源(连接池) -->
<dataSource type="POOLED">
<!-- 1. 硬编码 配置连接数据库的4个基本信息 -->
<!--<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/gq_day18"/>
<property name="username" value="root"/>
<property name="password" value="admin"/>-->
<!--2. 直接引用上面properties标签的内容-->
<!-- <property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="{username}"/>
<property name="password" value="${password}"/>-->
<!--3. 引用使用了resource或url属性的properties标签 外部配置文件-->
<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>
</environment>
</environments>
<!-- 指定映射配置文件的位置,映射配置文件指的是每个dao独立的配置文件 -->
<mappers>
<!--使用注解时-->
<mapper class="cn.itcast.dao.UserDao"/>
<!--使用xml文件时-->
<mapper resource="cn/itcast/dao/UserDao.xml"/>
<!--package标签在这里是用于指定dao接口所在的包,当指定之后就不在写mapper以及resource或者class了-->
<package name="cn.itcast.dao"/>
</mappers>
</configuration>
具体说明
- configuration:这个标签代表了这是mybatis的住配置文件
- settings:配置一些mybatis的参数,例如:开启Mybatis支持延迟加载,name属性:“lazyLoadingEnabled” 属性value值:true,例如:属性name值:“cacheEnabled” ,属性value值:"true"开启mybatis的二级缓存功能。
- environments:配置数据库环境,可以有默认值default
- environment:具体的一个数据库环境,以mysql为例
- transactionManager:事务类型,我们一般是JDBC
- dataSource:配置数据源,我们的数据源一般有三类POOLED、UNPOOLED、JNDI,mybatis框架内部自己依照池的原理实现了自己的连接池,所以我们一般选用POOLED。
- property:这个标签是配置参数,比较常见,有name、value属性,这里还可以引入外部文件properties文件,value可使用el表达式。
- properties:可用来引入外部配置文件,有resource()引当前类路径下文件)和url(以文件协议引入外部文件)两个属性,例如resource属性值为"jdbcConfig.properties"
- mappers:指定映射配置文件的位置,映射配置文件指的是每个dao独立的配置文件 ,属性是resource时表示对应接口使用的是xml配置,属性是class时表示对应接口使用的是注解配置。
- typeAliases:使用typeAliases配置别名,他只能配置domain中类的别名
- typeAlias:typeAliases的子标签,用于配置别名,type属性指定的实体类全限定类名,alias属性指定别名,就像int/INT/Intager等等那样使用了
- package:用于指定要配置别名的包,当指定之后该包下的实体类都会被注册别名,并且类名就是别名,不再区分大小写
一个实体Dao的Mapper配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--一定要说明名称空间,mybatis才能明白你这个类是给那个接口的实现-->
<mapper namespace="cn.itcast.dao.UserDao">
<!--配置查询结果的列名和实体类属性名对应关系-->
<resultMap id="userMap" type="cn.itcast.domain.User">
<!--主字段对应-->
<id property="userId" column="id"></id>
<!--非主键字段的对应-->
<result property="userName" column="name"></result>
<result property="userAddress" column="adress"></result>
<result property="userbrithday" column="birthday"></result>
</resultMap>
<!--开启user支持二级缓存-->
<cache/>
<!--=============================================================================-->
<!--查询所有-->
<select id="findAll" resultType="cn.itcast.domain.User">
select * from user
</select>
<!--保存用户-->
<insert id="insertUser" parameterType="cn.itcast.domain.User">
<!--keyproperty是实体类的属性名,keycolumn是数据库表中的列名,order是执行时机-->
<selectKey keyProperty="id" keyColumn="id" resultType="int" order="AFTER">
select last_insert_id()
</selectKey>
insert into user (name,gender,age,address,qq,email,username,password)values (#{name},#{gender},#{age},#{address},#{qq},#{email},#{username},#{password})
</insert>
<!--删除用户-->
<delete id="deleteUser" parameterType="int">
delete from user where id=#{uid}
</delete>
<!--更新-->
<update id="updateUser" parameterType="cn.itcast.domain.User">
update user set name=#{name},gender=#{gender},age=#{age},address=#{address},
qq=#{qq},email=#{email},username=#{username},password=#{password} where id=#{id}
</update>
<!--根据id查询用户-->
<select id="findUserById" parameterType="int" resultType="cn.itcast.domain.User">
select * from user where id=#{id}
</select>
<!--模糊查询(姓名)-->
<select id="findUserByName" parameterType="string" resultType="cn.itcast.domain.User">
select * from user where name like #{name}
</select>
<!--聚合函数-->
<select id="findCount" resultType="int">
select count(*) from user
</select>
<!--根据queryVo为条件查询-->
<select id="findUserByVo" parameterType="cn.itcast.domain.QueryVo" resultType="cn.itcast.domain.User">
select * from user where name like #{user.name}
</select>
</mapper>
- mapper :一个接口对应的配置文件,属性namespace说明了是哪个具体的接口
- resultMap :配置查询结果的列名和实体类属性名对应关系,我们一直是建议实体类属性名、和数据库表名一致的,但是难免有可能你的实体类属性名、和数据库表名不一致这个标签就是解决这个问题,定义完成这个后,在后面的CRUD操作中就可以利用属性id使用这个类型了,
- id : resultMap的子标签,用于主键
- result : resultMap的子标签,用于普通键
- collection : resultMap的子标签,用于一个实体类中包含另一个实体而且可能是多个的情况,一般用于表与表直接是一对多,多对多关系的情况
- association : resultMap的子标签,用于一个实体类中包含另一个实体而且该实体对象是一个的情况,一般用于表与表直接是一对一,多对一关系的情况
- constructor : resultMap的子标签,不常用
- discriminator : resultMap的子标签,不常用
- cache:表示这个Mapper对应的接口是支持二级缓存的
- select、update、delete、insert:CRUD这里不在说明了
- id:CRUD的属性对应接口中的那个方法,和mapper 的namespace值组合在一起,mybatis的代理对象就知道找个方法。
- parameterType:参数类型,如果是已经在主配置文件中的package 标签配置了这个实体类型的包名,那么就不用关心大小写问题了
- resultType:返回结果类型
- useCache:该条sql语句支持二级缓存功能,boolean类型
- resultMap:可以使用定义的
- selectKey : select last_insert_id()语句的专属标签,可以用来为默认增长主键赋值。
- keyproperty:selectKey 的属性,是实体类的属性名
- keycolumn:selectKey 的属性,是数据库表中的列名
- order:selectKey 的属性,是执行时机
动态sql语句
if、where、foreach、标签
<select id="findUserByCondition" resultMap="userMap" parameterType="cn.itcast.domain.User">
<!--动态sql方式1-->
select * from user where 1=1
<if test="name!=null">
and name=#{name}
</if>
<if test="gender!=null">
and gender=#{gender}
</if>-->
<!--动态sql方式2 不用写1=1 -->
select * from user
<where>
<if test="name!=null">
and name=#{name}
</if>
<if test="gender!=null">
and gender=#{gender}
</if>
</where>
</select>
<select id="findUserByIds" resultMap="userMap" parameterType="queryvo">
select * from user
<where>
<if test="ids !=null and ids.size()>0">
<foreach collection="ids" open="and id in (" close=")" item="uid" separator=",">
#{uid}
</foreach>
</if>
</where>
</select>
<!--定义重复sql语句-->
<sql id="defaultSql">
select * from user
</sql>
<!--查询所有-->
<select id="findAll" resultType="cn.itcast.domain.User">
<include refid="defaultSql"/>
</select>
- foreach标签,可以执行查询in类型sql,例如:select * from user where id in (5,6,7),属性collection就是那个集合,属性open就是开始的部分,属性close就是结束,item就是遍历名,separator是分隔符,标签体内容#{item名}
- if标签:判断,test属性是判断条件
- sql标签:可以定义一些可以重用的sql语句,并且起一个名字,就可以用这个名字直接使用了