前言
前面对于Java SE 的学习以及JavaWEB的学习之后,先对于当前相对于基础的框架的学习,可以更好地简化代码,提高效率。
Mybatis基础
概述: mybatis 是一个优秀的基于java的持久层框架,它内部封装了jdbc,使开发者只需要关注sql语句本身,而不需要花费精力去处理加载驱动、创建连接、创建statement等繁杂的过程。
过程:
- 首先:mybatis通过xml或注解的方式将要执行的各种 statement配置起来,并通过java对象和statement中sql的动态参数进行映射生成最终执行的sql语句。
- 最后:mybatis框架执行sql并将结果映射为java对象并返回。采用ORM思想解决了实体和数据库映射的问题,对jdbc 进行了封装,屏蔽了jdbc api 底层访问细节,使我们不用与jdbc api 打交道,就可以完成对数据库的持久化操作。
Mybatis的基础开发步骤
主要分为五个步骤:
①添加MyBatis的坐标
环境搭建(添加MyBatis的坐标):
②创建user数据表(请自行创建)
③编写User实体类 (请自行创建)
④编写映射文件UserMapper.xml
x <?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="userMapper">
<select id="findAll" resultType="com.itheima.domain.User">
select * from User
</select></mapper>
图片详细:
⑤编写核心文件SqlMapConfig.xml
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN“ "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/itheima/mapper/UserMapper.xml"/>
</mappers>
</configuration>
⑥编写测试类
测试相关代码
//加载核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
//获得sqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new
SqlSessionFactoryBuilder().build(resourceAsStream);
//获得sqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行sql语句
List<User> userList = sqlSession.selectList("userMapper.findAll");
//打印结果
System.out.println(userList);
//释放资源
sqlSession.close();
增删改查操作 注意事项:
- 插入操作注意问题
<mapper namespace="userMapper">
<insert id="add" parameterType="com.itheima.domain.User">
insert into user values(#{id},#{username},#{password})
</insert>
</mapper>
/* 在映射文件中使用parameterType属性指定要插入的数据类型
Sql语句中使用#{实体属性名}方式引用实体中的属性值
插入操作使用的API是sqlSession.insert(“命名空间.id”,实体对象);
插入操作涉及数据库数据变化,所以要使用sqlSession对象显示的提交事务,即sqlSession.commit();
*/
- 修改操作注意问题:
<mapper namespace="userMapper">
<update id="update" parameterType="com.itheima.domain.User">
update user set username=#{username},password=#{password} where id=#{id}
</update>
</mapper>
/* 修改语句使用update标签
修改操作使用的API是sqlSession.update(“命名空间.id”,实体对象);
修改操作涉及数据库数据变化,所以要使用sqlSession对象显示的提交事务,即
sqlSession.commit() */
- 删除操作注意问题:
<mapper namespace="userMapper">
<delete id="delete" parameterType="java.lang.Integer">
delete from user where id=#{id}
</delete>
</mapper>
/* 删除语句使用delete标签
Sql语句中使用#{任意字符串}方式引用传递的单个参数
删除操作使用的API是sqlSession.delete(“命名空间.id”,Object);
删除操作涉及数据库数据变化,所以要使用sqlSession对象显示的提交事务,即
sqlSession.commit()
*/
Mybatis的Dao层实现
代理开发方式
概念: 采用 Mybatis 的代理开发方式实现 DAO 层的开发,例如:Mapper 接口开发方法只需要程序员编写Mapper 接口(相当于Dao 接口),由Mybatis 框架根据接口定义创建接口的动态代理对象,代理对象的方法体同上边Dao接口实现类方法。
Mapper 接口开发需要遵循以下规范:
1) Mapper.xml文件中的namespace与mapper接口的全限定名相同
2) Mapper接口方法名和Mapper.xml中定义的每个statement的id相同
3) Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql的parameterType的类型相同
4) Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同
如图所示:
MyBatis映射文件深入
1.动态sql语句
- <if>
其中where标签相当于已经提供了where 语句字段
<select id="findByCondition" parameterType="user" resultType="user">
select * from User
<where>
<if test="id!=0">
and id=#{id}
</if>
<if test="username!=null">
and username=#{username}
</if>
</where>
</select>
- <foreach>
<select id="findByIds" parameterType="list" resultType="user">
select * from User
<where>
<foreach collection="array" open="id in(" close=")" item="id" separator=",">
#{id}
</foreach>
</where>
</select>
/*
•collection:代表要遍历的集合元素,注意编写时不要写#{}
•open:代表语句的开始部分
•close:代表结束部分
•item:代表遍历集合的每个元素,生成的变量名
•sperator:代表分隔符*/
- 相同sql语句的抽取
<!--抽取sql片段简化编写-->
<sql id="selectUser" select * from User</sql>
<select id="findById" parameterType="int" resultType="user">
<include refid="selectUser"></include> where id=#{id}
</select>
<select id="findByIds" parameterType="list" resultType="user">
<include refid="selectUser"></include>
<where>
<foreach collection="array" open="id in(" close=")" item="id" separator=",">
#{id}
</foreach>
</where>
</select>
注解开发
注解开发步骤:
1.相关注解开发主要分为:
@Insert:实现新增
@Insert("insert into user values (#{id},#{name},#{password},#{address},#{phone},null)")
public void save(User user);
@Update:实现更新
@Update(" update user set name=#{name},password=#{password} where id = #{id}")
public void update(User user);
@Delete:实现删除
@Delete("delete from user where id = #{id};")
public void delete(int id);
@Select:实现查询
@Select(" select * from user")
public User findAll();
1.2MyBatis的注解实现复杂映射开发
2.扫描使用的注解
- 使用注解的类
<mappers>
<!--扫描使用注解的类-->
<mapper class="com.itheima.mapper.UserMapper"></mapper>
</mappers>
- 使用接口的所在包形式
<mappers>
<!--扫描使用注解的类所在的包-->
<package name="com.itheima.mapper"></package>
</mappers>