ORM模型简介:
传统JDBC程序的设计缺陷:
Mybatis概述:
MyBatis的历史:
-------------------------------------------------------------------------------------------------------------------------------------
Mybaits环境配置文件:
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">
<configuration>
<!--properties配置,用于加载外部的properties配置文件-->
<properties resource="db.properties"></properties>
<!--environments 主要用于进行数据源的配置
可以配置多个数据源~ 通过default属性来指定当前项目运行过程中使用的是哪个数据源 -->
<environments default="development">
<!--environment 用于配置一个具体的独立的数据源
id属性用于给当前数据源定义一个名称,方便我们的项目指定-->
<environment id="development">
<!--transactionManager用于配置事务管理,默认情况下使用的是JDBC事务管理-->
<transactionManager type="JDBC"/>
<!--dataSource具体数据源的链接信息;
type属性用于指定是否使用连接池 可以用于指定线上线下不同数据库信息的配置-->
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
<environment id="product">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
<environment id="test">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!-- mappers主要用于配置我们外部的映射配置文件 在主配置文件中需要引入加载映射配置文件-->
<mappers>
<!-- mapper主要配置引入某一个具体的映射文件,resource进行路径方式的引入 -->
<mapper resource="mapper/usersMapper.xml"/>
</mappers>
</configuration>
usersMapper.xml
<?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">
<!--
mapper 用于定义一个映射配置文件的根节点
namespace属性是用来配置命名空间,主要进行session级别的缓存管理
命名空间默认情况下,使用我们当前操作的实体类的全路径
-->
<mapper namespace="com.damu.entity.Users">
<!--动态SQL语句-->
<select id="findUsers" resultMap="forUsers">
select * from users
<if test="id != null">
where id = #{id}
</if>
</select>
<!-- 自定义映射关系集合:主要包含对于一些自定义操作的配置,如数据库字段名和属性名不一致
查询的时候无法进行映射 -->
<resultMap id="forUsers" type="com.damu.entity.Users">
<!-- 绑定id主键 -->
<id property="id" column="id"></id>
<!-- result配置,主要配置普通属性,column表示配置的是数据库字段名称
property配置的是实体类的属性名称 -->
<result column="username" property="name"></result>
<!--collection 标签主要用于 实体类里有类似List<Address> addresses; 这样
的集合属性,需要对其进行配置。 这个List属性的泛型为某个实体类 将和下面的<select >标签一起使用-->
<collection property="addresses" column="id" ofType="com.damu.entity.Address" select="getAddress"></collection>
</resultMap>
<select id="getAddress" resultType="com.damu.entity.Address">
select * from address where userid = #{id}
</select>
<sql id="user_fields">
username, userpass, nickname, age, gender, email, phone, createTime, updateTime, lastLogin, userstatus, remark
</sql>
<insert id="addUser" useGeneratedKeys="true" keyProperty="id">
insert into users( <include refid="user_fields"></include>)
values(#{name},#{userpass}, #{nickname}, #{age}, #{gender}, #{email}, #{phone}, #{createTime}, #{updateTime}, #{lastLogin}, #{userStatus}, #{remark})
</insert>
<update id="updateUser">
update users
<set>
<if test="name != null">username = #{name},</if>
<if test="userpass != null">userpass = #{userpass},</if>
<if test="nickname != null">nickname = #{nickname},</if>
<if test="age != null">age = #{age},</if>
<if test="gender != null">gender = #{gender},</if>
<if test="email != null">email = #{email},</if>
<if test="phone != null">phone = #{phone},</if>
<if test="createTime != null">createTime = #{createTime},</if>
<if test="updateTime != null">updateTime = #{updateTime},</if>
<if test="lastLogin != null">lastlogin = #{lastLogin},</if>
<if test="userStatus != null">userStatus = #{userStatus},</if>
<if test="remark != null">remark = #{remark},</if>
</set>
where id = #{id}
</update>
<delete id="delUser">
delete from users where id = #{id}
</delete>
</mapper>
db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=UTF-8
username=root
password=root
关于namespace命名空间:在MyBatis中,mapper中的namespace用于绑定Dao接口的,即面向接口编程。它的好处在于当使用了namespace之后就可以不用写接口实现类,业务逻辑会直接通过这个绑定寻找到相对应的SQL语句进行对应的数据处理。而命名空间默认情况下使用我们操作实体类的全路径,但是使用userDao也是没有问题的。
Mybatis的流程:
Mybatis的多参数传递问题:
传入不同的参数有不同的处理方式。
当调用方法的形参为下图时:有两个参数
但是调用时,无法在XML里直接使用#{username}、#{gender}这样的写法。如下图所示将会报错,MyBatis默认处理多参数情况只有下图红框中的四个参数可以使用,这四个参数是Mybatis根据参数的位置自定义的。
多参数传递可以使用JavaBean的方式进行传递:
如下图,向参数传递了一个名为Person的对象,在XML中则可以使用 #{属性名}的方式取值。
多参数传递也可以使用Map的方式进行传递:
多参数传递也可以使用@param的方式进行传递:
当传入的参数为集合类型的时候:
当传入Collection类型时:
如果Collection为list类型时也可以如下图写法:
如果传入的参数为数组类型:
如果不想用array[0]这种方式可以起名字:
入参处理总结:
Mybatis以数组为传递参数进行数据的查询:
如果SQL语句需要这样写 select * from person where id in (1,2,3,4); 其中1,2,3,4的参数如果有很多个的话,每次取数据都要类似 array[0],array[1],array[2] 这样写 多了不好写,所以可以用到foreach元素。
collection:遍历的数组名字 item:当前所遍历的对象 index:当前遍历对象的索引
关于JDBC的批处理写法:
关于Mybatis的批处理写法:
第一种方式:
第一种方式使用的时候,会把SQL拼接成很长的字符串,对于程序解析的负担会很重
第二种方式:ExecutorType方式
基于Mybatis的拦截器的分页:
Mybatis的四大对象: mybaits的增删改查本质上就是借助于下面四大对象来完成数据库的操作并返回结果的。
。。。。。有心情再往下记录。