SSH框架实现简单CRUD

package com.ssh.entities;
public class Department {
    private Integer id;
    private String departmentName;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getDepartmentName() {
        return departmentName;
    }
    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }
}

package com.ssh.entities;
import java.util.Date;
public class Employee {
    private Integer id;
    private String lastName;
    private String email;
    private Date birth;
    private Date createTime;
    private Department department;
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public Date getBirth() {
        return birth;
    }
    public void setBirth(Date birth) {
        this.birth = birth;
    }
    public Date getCreateTime() {
        return createTime;
    }
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
    public Department getDepartment() {
        return department;
    }
    public void setDepartment(Department department) {
        this.department = department;
    }
}

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2020??1??9?? ????9:08:43 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="com.ssh.entities.Department" table="SSH_DEPARTMENT">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="departmentName" type="java.lang.String">
            <column name="DEPARTMENT_NAME" />
        </property>
    </class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2020??1??9?? ????9:08:43 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="com.ssh.entities.Employee" table="SSH_EMPLOYEE">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="lastName" type="java.lang.String">
            <column name="LASTNAME" />
        </property>
        <property name="email" type="java.lang.String">
            <column name="EMAIL" />
        </property>
        <property name="birth" type="java.util.Date">
            <column name="BIRTH" />
        </property>
        <property name="createTime" type="java.util.Date">
            <column name="CREATE_TIME" />
        </property>
        <many-to-one name="department" class="com.ssh.entities.Department">
            <column name="DEPARTMENT_ID" />
        </many-to-one>
    </class>
</hibernate-mapping>

hibernate配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test?useSSL=false&amp;serverTimezone=Asia/Shanghai</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">123456</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <mapping resource="com/ssh/entities/Department.hbm.xml"/>
        <mapping resource="com/ssh/entities/Employee.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

数据操作
package com.ssh.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository  
@Transactional
public class BaseDao {
    @Autowired 
    private SessionFactory sessionFactory;
      public void setSessionFactory(SessionFactory sessionFactory) {
          this.sessionFactory = sessionFactory; 
      }
     
    public Session getSession() {
        return this.sessionFactory.getCurrentSession();
    }
}


package com.ssh.dao;
import java.util.List;

import com.ssh.entities.Department;

public class DepartmentDao extends BaseDao {
    public List<Department> getAll() {
        String hql = "FROM Department";
        return getSession().createQuery(hql).list();
    }
}


package com.ssh.dao;

import java.util.List;

import com.ssh.entities.Employee;

public class EmployeeDao extends BaseDao{
    public void delete(Integer id) {
        String hql = "DELETE From Employee e where e.id=?";
        getSession().createQuery(hql).setParameter(0,id).executeUpdate();
    }

    public List<Employee> getAll() {
        //String hql = "From Employee e LEFT OUTER JOIN FETCH e.department";
        String hql = "From Employee";
        return getSession().createQuery(hql).list();
    }
    
    public void saveOrUpdate(Employee employee) {
        getSession().saveOrUpdate(employee);
    }
    
    public Employee get(Integer id) {
        return (Employee)getSession().get(Employee.class,id);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值