目录
概念
1.框架
框架是一个半成品,已经对基础的代码进行了封装并提供相应的API,开发者在使用框架 是直接调用封装好的api可以省去很多代码编写,从而提高工作效率和开发速度。
2.什么是orm
ORM,即Object-Relational Mapping(对象关系映射),它的作用是在关系型数据库和业务实体对象之间作一个映射,这样,我们在具体的操作业务对象的时候,就不需要再去和复杂的SQL语句打交道,只需简单的操作对象的属性和方法。
3.什么是mybatis框架
MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀的ORM框架。MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及对结果集的检索封装。MyBatis 可以使用简单的 XML 或注解用于配置和原始映射,将接口和 Java 的 POJO(Plain Old Java Objects,普通的 Java 对象)映射成数据库中的记录.,由于mybatis必须手动写sql语句,所以它属于半自动化框架 。
如何使用mybatis
首先需要在本地虚拟机mysql中准备一张表,我这里准备的是一个book表。
操作流程
第一步,打开idea创建一个maven的项目并引入相关的jar包
第二步,创建一个book实体类
第三步,创建一个dao方法。(CRUD方法以及多参时如何声明参数)
第四步,创建一个properties文件(用于指向本地数据库)
第五步,编写全局配置文件(主要是配置数据源信息)
第六步,编写mapper映射以及SQL语句
SQL语句
<!--添加-->
<insert id="addbook">
insert into books values (#{id},#{tid},#{name},#{author},#{publisher},#{price})
</insert>
<!--删除-->
<delete id="deletebook">
delete from books where book_id=#{id}
</delete>
<!--修改-->
<update id="updatebook">
update books set type_id=#{tid},book_name=#{name},author=#{author},publisher=#{publisher},price=#{price} where book_id=id
</update>
<!--根据ID查询-->
<select id="selectById" resultMap="bookmap">
select * from books where book_id=#{id}
</select>
<!--查询全部-->
<select id="selectAll" resultMap="bookmap">
select * from books
</select>
<!--根据价格区间查询(多参)-->
<!--有些特殊字符会识别错误(比如 &,<,>,',"),因此可以使用<![CDATA[特殊字符或者是SQL语句]]>-->
<select id="selectByprice" resultMap="bookmap">
<![CDATA[select * from books where price>#{min} and price<#{max}]]>
</select>
第七步,进行测试
我们还可以加入日志文件
首先,引入日志jar包
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
然后创建日志配置文件(输出日志信息到文件夹的盘符路径可以自己定义)
### 设置###
log4j.rootLogger = debug,stdout,D,E
### 输出信息到控制抬 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
### 输出DEBUG 级别以上的日志到=E://logs/error.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = D://logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 输出ERROR 级别以上的日志到=E://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =D://logs/error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
运行效果展示
进阶之动态SQL语句
作用:SQL语句可以根据条件的改变而发生改变
元素 | 作用 | 描述 |
if | 条件判断 | 单条件判断 |
choose(when,otherwise) | 条件选择,相当于java when | 多条件分支判断 |
where,set | 辅助作用 | 处理SQL语句拼接问题 |
foreache | 循环 | 循环 |
使用方法
1,if加上where使用(if和where同时使用时,会自动添加一个where并去除第一个满足条件的and)
<select id="selectAll2" resultMap="bookmap">
select * from books
<where>
<if test="id!=null and id!=''">
and book_id=#{id}
</if>
<if test="tid!=null and tid!=''">
and type_id=#{tid}
</if>
<if test="name!=null and name!=''">
and book_name=#{name}
</if>
<if test="author!=null and author!=''">
and author=#{author}
</if>
<if test="publisher!=null and publisher!=''">
and publisher=#{publisher}
</if>
<if test="price!=null and price!=''">
and price=#{price}
</if>
</where>
</select>
测试
2,if和set一起用(当if和set一起使用的时候,会自动添加一个set,并且会将最后一个满足的条件的逗号去掉)
<update id="updatebook2" >
update books
<set>
<if test="tid!=null and tid!=''">
type_id=#{tid},
</if>
<if test="name!=null and name!=''">
book_name=#{name},
</if>
<if test="author!=null and author!=''">
author=#{author},
</if>
<if test="publisher!=null and publisher!=''">
publisher=#{publisher},
</if>
<if test="price!=null and price!=''">
price=#{price},
</if>
</set>
where book_id=#{id}
</update>
测试
当choose +where一起使用时( when:当条件满足时不会执行下面的when和other 都不满足则执行otherwise)
<select id="selectAll3" resultMap="bookmap">
select * from
<where>
<choose>
<when test="id!=null and id!=''">
and book_id=#{id}
</when>
<when test="tid!=null and tid!=''">
and type_id=#{tid}
</when>
<when test="name!=null and name!=''">
and book_name=#{name}
</when>
<when test="author!=null and author!=''">
and author=#{author}
</when>
<when test="publisher!=null and publisher!=''">
and publisher=#{publisher}
</when>
<otherwise>
and book_price>35
</otherwise>
</choose>
</where>
</select>