ORM 对象关系映射 Mybits vs Hibernate

对象关系映射
(英语:(Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是一种程序技术,用于实现面向对象编程语言里不同类型系统的数据之间的转换 [1] 。从效果上说,它其实是创建了一个可在编程语言里使用的–“虚拟对象数据库”。
面向对象是从软件工程基本原则(如耦合、聚合、封装)的基础上发展起来的,而关系数据库则是从数学理论发展而来的,两套理论存在显著的区别。为了解决这个不匹配的现象,对象关系映射技术应运而生。
对象关系映射(Object-Relational Mapping)提供了概念性的、易于理解的模型化数据的方法。ORM方法论基于三个核心原则: 简单:以最基本的形式建模数据。 传达性:数据库结构被任何人都能理解的语言文档化。 精确性:基于数据模型创建正确标准化的结构。 典型地,建模者通过收集来自那些熟悉应用程序但不熟练的数据建模者的人的信息开发信息模型。建模者必须能够用非技术企业专家可以理解的术语在概念层次上与数据结构进行通讯。建模者也必须能以简单的单元分析信息,对样本数据进行处理。ORM专门被设计为改进这种联系.

简单来说,ORM就是对JDBC进行轻量级封装。
这里写图片描述

普通的mysql 通过connection来进行数据库的访问

//注在使用端设置一个静态static Connection conn=null;
import java.sql.*;
import java.util.Properties;
public class MyConnection extends SQLException{
static Connection inco;
public Connection My () throws SQLException
{
Driver d=new com.mysql.jdbc.Driver();
String url="jdbc:mysql://localhost:3306/ceshi";
Properties info=new Properties();
info.put("user","root");
info.put("password","092248");
inco=d.connect(url,info);
return inco;
}

Mybaits部分
Mybaits SQL语句 通过Mapper.xml经行查询

//查询
<select id="queryEmp" resultType="cn.test.entity.Emp">
select * from emp where 1=1
<choose>
<when test="deptNo!=null"> and deptno=#{deptNo} </when>
<when test="deptName!=null"> and deptname=#{deptName} </when>
<otherwise> and personnum>#{personNum} </otherwise>
</choose>
</select>
注:上面也说了,choose相当于Java中的switch语句;当第一个when满足时;就只执行第一个when中的条件。当when中的条件都不满足时;就会执行默认的的;也就是otherwise中的语句。

//插入
   <!-- useGeneratedKeys设置为"true"表明要MyBatis获取由数据库自动生成的主键;keyProperty="id"指定把获取到的主键值注入到User的id属性 -->  
    <insert id="insertUserInfo"  parameterType="User" useGeneratedKeys="true" keyProperty="id">  
        insert into `user`(userName,userAge,userAddress) values (#{userName},#{userAge},#{userAddress})  
    </insert>  


//更新    
    <update id="updateUserInfo" parameterType="User">  
        update user set userName=#{userName},userAge=#{userAge},userAddress=#{userAddress} where id=#{id}  
    </update>  


//删除 
    <delete id="deleteUserInfo" parameterType="int">  
        delete from `user` where id=#{id}  
    </delete>  

//模糊查询
<select id="likeEmps" parameterType="string" resultType="com.oak.entity.Emp">
select * from emp where ename like '%${value}%'
</select>

Hibernate 部分
通过显示的方式 通过 hibernate.configuration.xml 获取session

//生成Session
private Session session=null;
private SessionFactory sf=null;
//修改的话要提交事务
public Getsession() {
super();
sf =new Configuration().configure().buildSessionFactory();
session = sf.openSession();
}

//上传
public CreateBook(book n)
{
this.b=n;
session=new Getsession().getSession();
//开始上传事务
session.beginTransaction();
session.save(b);
//提交事务
session.getTransaction().commit();
JOptionPane.showMessageDialog(null, "提示", "图书上传成功!", JOptionPane.ERROR_MESSAGE);
}

通过Spring IOC Dao层继承HibernateDaoSupport类
1.查询
getHibernateTemplate().find()
https://www.cnblogs.com/fengjunming/p/7228196.html

2.更新数据
先用get函数获取要修改的对象(而不是新建一个对象)
利用set设置后,再调用update()执行更新
User u =this.getHibernateTemplate().get(User.class,name);
u.setPassword(newpassword);
this.getHibernateTemplate().update(u);
3删除数据
同样是先获取再删除
User u =this.getHibernateTemplate().get(User.class,name);
this.getHibernateTemplate().delete(u);

猜你喜欢

转载自blog.csdn.net/weixin_40990818/article/details/81711427