一.何为ORM,MyBatis与其他ORM框架的区别
- ORM:对象关系映射,Java是面向对象编程的,而我们一般操作的数据库都是关系型数据库,所以ORM就是将Java与数据库建立映射
- MyBatis和JPA(hibernate)的区别
1.hibernate:hibernate不用我们去写sql,所以开发速度更快,但是也因为sql无法控制,而其为了兼容多种数据库,无法选择最优方案,所以运行速度较慢
2.MyBatis:MyBatis选哟我们自己去写sql,所以运行速度更快,但相对的开发速度较慢
二.使用MyBatis完成CRUD功能
1.需要导入相应jar包
MyBatis的核心包,MyBatis的依赖包以及数据库驱动包
2.配置MyBatis的核心配置文件—mybatis-config.xml
<configuration>
<!--引入properties文件 ,数据库信息配置文件-->
<properties resource="jdbc.properties"></properties>
<environments default="development">
<!--环境-->
<environment id="development">
<transactionManager type="JDBC"/> JDBC是事务类型
<dataSource type="POOLED"> 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>
<!--对应ORM相应的映射文件,即写sql语句的文件-->
<mapper resource="cn/zr/domain/ProductMapper.xml"/>
</mappers>
</configuration>
3.配置sql的xml文件,将sql写在xml中—如ProductMapper.xml
<!--
namespace:命名空间,在使用时根据命名空间和id值确定sql
-->
<mapper namespace="cn.zr.domain.Product">
<!--查询一条-->
<select id="findOne" parameterType="long" resultType="Product">
select * from product where id = #{id}
</select>
<!--查询所有-->
<select id="findAll" resultType="cn.zr.domain.Product">
select * from product
</select>
<!--添加
useGeneratedKeys="true"
keyColumn="id"
keyProperty="id" 添加时加上这三个属性可以立即获取到添加数据的id
-->
<insert id="save" parameterType="cn.zr.domain.Product"
useGeneratedKeys="true"
keyColumn="id"
keyProperty="id">
INSERT INTO product(productName,dir_id,salePrice,supplier,brand,cutoff,costPrice)
VALUES(#{productName},#{dir_id},#{salePrice},#{supplier},#{brand},#{cutoff},#{costPrice})
</insert>
<!--修改-->
<update id="update" parameterType="cn.zr.domain.Product">
UPDATE product SET productName=#{productName},dir_id=#{dir_id},salePrice=#{salePrice},supplier=#{supplier},brand=#{brand},cutoff=#{cutoff},costPrice=#{costPrice}
WHERE id=#{id}
</update>
<!--删除-->
<delete id="delete" parameterType="long">
DELETE FROM product WHERE id=#{id}
</delete>
</mapper>
4.测试功能
- 读取核心配置文件
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
- 创建SqlSessionFactory对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
- 创建SqlSession对象
SqlSession sqlSession = factory.openSession();
- 通过SqlSession对象就可以调用方法
查询一条 --- cn.zr.domain.Product.findOne:mapper.xml中的命名空间和sql的id
Product one = sqlSession.selectOne("cn.zr.domain.Product.findOne", 1L);
查询所有
List<Product> list = sqlSession.selectList("cn.zr.domain.Product.findAll");
添加
sqlSession.insert("cn.zr.domain.Product.save",product);
修改
sqlSession.update("cn.zr.domain.Product.update",product);
删除
sqlSession.delete("cn.zr.domain.Product.delete",id);
三.MyBatis的使用细节
- 想要在添加数据之后立即拿到id
在mapper.xml文件中的添加sql中加上三个属性
<insert id="save" parameterType="cn.zr.domain.Product"
useGeneratedKeys="true" 是否使用主键
keyColumn="id" 数据库的主键
keyProperty="id"> domain的主键属性
- 为整个包中的类配置别名
在核心配置文件中
<typeAliases>
<package name="cn.zr.domain"></package>
</typeAliases>
- 如果表中的列与类中的字段对应不上,进行手动映射
1.在mapper.xml中配置映射
<!--手动映射表中列与类中字段
type:映射的对象类型
column:表中的列
property:对象的字段
将sql属性中的resultType改为resultMap,且id中的值放入resultMap中
-->
<resultMap id="myresult" type="Product">
<result column="dir_id" property="dirId"></result>
</resultMap>
<!--查询一条-->
<select id="findOne" parameterType="long" resultMap="myresult">
select * from product where id = #{id}
</select>