mybatis是什么
是一个基于java的持久层框架,也是一个ORM框架
特点
1)是一个支持普通sql查询,存储过程和高级映射的优秀持久层框架
2)消除了几乎所有的jdbc代码和手工设置参数和处理结果集的检索
(3)MyBatis 使用简单的 ****XML*或 注解 用于配置和原始映射,将接口和 Java 的*POJOs****(Plain Old Java Objects,普通的 Java对象 Employee/User/Entity/Domain)映射成数据库中的记录
jdbc和mybatis优缺点?
(1)jdbc操作步骤比较繁琐,比较执行贾琏欲执事 而mybatis不用 消除这个代码
(2)jdbc需要自己手动设置 参数 以及处理返回结果集 --而mybatis 不需要
(3)mybatis 可以采用 xml 和 注解 来写sql …jdbc 把sql 写到代码里面, 可以提高维护性
(4)jdbc 它是操作数据库最底层的代码,mybatis也是从jdbc上面封装出来框架 mybatis支持缓存
如何使用mybatis
第一步:
导包、数据库建表,建立domain,dao,service
第二步: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>
<!-- 开发环境-->
<environments default="development">
<!--
一个环境 id:为这个环境取唯一一个id名称
-->
<environment id="development">
<!--
事务管理 type:JDBC(支持事务)/MANAGED(什么都不做)
mysql: innodb MyISAM
-->
<transactionManager type="JDBC" />
<!-- 数据源, 连接池 type(POOLED):MyBatis自带的连接池 -->
<dataSource type="POOLED">
<!-- 连接数据库的参数 四大金刚 -->
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql:///mybatis" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<!-- 这个mappers代表的是相应的ORM映射文件 -->
<mappers>
<mapper resource="cn/itsource/mybatis/domain/ProductMapper.xml" />
</mappers>
</configuration>
第三步:ProductMapper.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:命名空间
以后:通过namespace+id 调用对应的语句
-->
<mapper namespace="cn.itsource.mybatis.domain.ProductMapper">
<!--
select:表示查询标签
id: 代表给标签取个名称 必须唯一
resultType:返回值类型
-->
<select id="findAll" resultType="cn.itsource.mybatis.domain.Product">
select * from product
</select>
</mapper>
第四步:测试
@Override
public List<Product> findAll() throws IOException {
//读取配置
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
//SqlSessionFactory -->EntityManagerFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
//SqlSession -->EntityManager
SqlSession sqlSession = sqlSessionFactory.openSession();
//调用findAll +namespace+id
List<Product> products = sqlSession.
selectList("cn.itsource.mybatis.domain.ProductMapper.findAll");
return products;
}
第五步:CRUD
//查询所有
<select id="findAll" resultType="cn.itsource.mybatis.domain.Product">
select * from product
</select>
//保存
<insert id="save" parameterType="cn.itsource.mybatis.domain.Product">
insert into product(productName,salePrice,costPrice,cutoff)
values(#{productName},#{salePrice},#{costPrice},#{cutoff})
</insert>
//更新
<update id="update" parameterType="cn.itsource.mybatis.domain.Product">
update product set productName = #{productName},salePrice = #{salePrice}
where id = #{id}
</update>
根据id删除
<delete id="delete" parameterType="long">
delete from product where id = #{id}
</delete>
//根据id查找
<select id="findOne" parameterType="long" resultType="cn.itsource.mybatis.domain.Product">
select * from product where id = #{id}
</select>
//根据名字查询
<select id="findByName" parameterType="cn.itsource.mybatis.query.ProductQuery"
resultType="cn.itsource.mybatis.domain.Product">
select * from product where productName = #{productName}
</select>
<sql id="whereSql">
<where>
<if test="productName != null">
and productName = #{procutName}
</if>
<if test="id!=null">
and id = #{id}
</if>
</where>
</sql>
<!-- 按条件查询-->
<select id="findByCondition" parameterType="cn.itsource.mybatis.query.ProductQuery"
resultType="cn.itsource.mybatis.domain.Product">
select * from product
<include refid="whereSql"></include>
</select>
<!-- 批量删除-->
<delete id="deleteBatch" parameterType="java.util.List">
delete from product where id in
<foreach collection="list" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
<!-- 批量添加-->
<insert id="insertBatch" parameterType="java.util.List">
insert into product (productName,salePrice,costPrice)
values
<foreach collection="list" item="product" separator=",">
(#{product.productName},#{product.salePrice},#{product.costPrice})
</foreach>
</insert>
<!-- 批量更新-->
<update id="updateBatch" parameterType="map">
update product set productName = #{productName} where id in
<foreach collection="list" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</update>
注意
mapper映射配置 namespace:命名空间
以后:通过namespace+id 调用对应的语句
resultType:返回值类型
parameterType 参数类型
useGeneratedKeys是否:要得到主键
keyColumn:数据库主键的列
keyProperty:对应实体类的属性
#和 $区别:
(1) $ 取得对象里面的某个属性 比如 ${id} Long getId
# 都可以
(2)重点 # select * from product where productName = ?
预编译的对象
$ select * from product where productName = 罗技G500
拼接字符串方式
# 推荐使用,因为 $ 有sql注入问题
(3)后面使用$的 orderby+limit 后面就不需要添加引号
扩展 Log4J
导包-> log4j.properties
日志等级: OFF level > FATAL(致命) > ERROR(错误) > WARN (警告)>INFO (提示)> DEBUG (调试)> trace > ALL level(所有配置)