可以根据官网快速上手,(学习新技术的方法)
学习时,明确自己的目标:
目标:通过maven项目利用mybatis技术 查询数据库当中的一张表的内容
1 新建一个maven项目
2 由于项目当中要使用mybatis技术 所以需要添加mybatis的依赖(jar)--全部放到父项目当中
2 由于使用了数据库-mysql 故而需要添加一个mysql的依赖--下载过来
3 不在使用system.out.println 使用日志--添加固定的三个jar--logback.xml当resources文件夹当中
4 测试 日志能不能正常打印 随便键一个类 在main当中打印日志 @Slftj(topic="e")--log.debug
5 完成mybaits的开发
6 构件一个SqlSessionFactroy对象
String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
7 上面代码涉及到一个mybatis-config.xml 目前项目当中没有 新建一个---resources文件夹当中 内容?
8 观察xml 有一些代码需要修改 数据库连接信息 -->BlogMapper.xml 这个也需要修改
9 思考BlogMapper.xml的意义 对应数据库的表,对数据库的表的操作都需要在这个xml当中完成 比如查询语句(sql)
10 目前项目当中没有这个xml 新建一个,因为和这个xml他的特殊意义和mybatis-config.xml的意义不一样,为了区别建议放不同文件夹
11 由于mybatis-config.xml在resoure的根目录下面,那么BlogMapper.xml就再新建一个文件夹专门来存放所有xxxMapper.xml--名字建议mappers
12 还没有创建BlogMapper.xml 于是在mappers创建一个 xml名字不要和官网相同,尽量保持和表名相同-RateMapper.xml
13 里面的内容RateMapper.xml---参考官网
14 思考namespace的值---随便写--和表名完全相同---Rate
15 <select id="自己来" resultType="单个数据=实体类,list=实体类">
16 修改sql 改成自己想要的sql
17 到此为止 两个xml都改好了可以顺利创建SqlSessionFactory
18 运行main方法---
String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
19 如果不报错 表示SqlSessionFactory创建成功;接下来?
20 从 SqlSessionFactory 中获取 SqlSession 为什么需要创建SqlSessionFactory然后再思考为什么需要 中获取 SqlSession
21 sqlSessionFactory.openSession()获取一个session对象
22 BlogMapper mapper = session.getMapper(BlogMapper.class); 这个BlogMapper是什么 哪里来的
23 当前项目当中是没有BlogMapper这个类的于是需要创建BlogMapper类
24 他就相当于dao用来执行sql语句的
25 所以在项目当中去新建一个包 -->dao
26 在dao包当中新建 BlogMapper--名字不能和他一样---表名----RateDao
26 里面写方法----需要查询一条--queryRateByid(int id)
27 把类建好之后 改代码BlogMapper mapper = session.getMapper(BlogMapper.class);
RateMapper mapper = session.getMapper(RateMapper.class);
28 由于自己写的mapper里面有一个queryRateByid方法 于是调用这个方法完成sql的执行
30 出错---不知道执行那个sql;告诉他找哪条sql语句执行;在sql语句当中的id和方法名一样
31 出错--因为他不知道从哪个xml当中找,你也要告诉他从哪个xml当中 把xml当中的namespace的值改成当前类包名+类名一样即可
32 ---完成
动态sql:
mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装。
if
<!-- 传递pojo综合查询用户信息 -->
<select id="findUserList" parameterType="user" resultType="user">
select * from user
where 1=1
<if test="id!=null and id!=''">
and id=#{id}
</if>
<if test="username!=null and username!=''">
and username like '%${username}%'
</if>
</select>
where:
<select id="findUserList" parameterType="user" resultType="user">
select * from user
<where>
<if test="id!=null and id!=''">
and id=#{id}
</if>
<if test="username!=null and username!=''">
and username like '%${username}%'
</if>
</where>
</select>注意:
<where />
可以自动处理第一个and。or
set:
<!-- 修改用户 -->
<update id="updateUser" parameterType="org.csmf.mybatis.entity.User">
update t_user
<set>
<if test="name!=null and name !='' ">
username =#{name},
</if>
<if test="password !=null and password !='' ">
password =#{password},
</if>
<if test="sex!=null and sex !='' ">
sex =#{sex},
</if>
<if test="brithday!=null and brithday !='' ">
brithday =#{brithday},
</if>
<if test="address!=null and address !='' ">
address =#{address},
</if>
</set>
<where>
<if test="id!=null and id!=''">
id = #{id}
</if>
</where>
</update>
<set>
可以处理最后一个逗号
foreach :
第一种:
<!-- 根据一组Id查询用户 -->
<select id="findUserByIds" resultType="User" parameterType="QueryVo">
select * from t_user
<where>
<!--select * from t_user where id=? or id=? or =? ... -->
<foreach collection="list" item="id" open="and ( " close=")" separator="or">
id = #{id}
</foreach>
</where>
</select>第二种:
<!-- 根据一组Id查询用户 -->
<select id="findUserByIds" resultType="User" parameterType="QueryVo">
select * from t_user
<where>
<!--select * from t_user where id in (? ,?, ?...) -->
<foreach collection="list" item="id" open="and id in ( " close=")" separator=" , ">
#{id}
</foreach>
</where>
</select>