您现在的位置是:首页 >

mybatis多表查询用对象接收 hibernate关联关系-多对一

火烧 2023-01-07 03:26:22 1044
hi er ate关联关系-多对一   模型 员工Em loyee — 部门De artme t  Java代码   ackage Domai    u lic cla Em loyee {   u

hibernate关联关系-多对一  

  模型 员工Employee — 部门Department

  Java代码

  package Domain;

  public class Employee {

  public int getId() {

  return id;

  }

  public void setId(int id) {

  this id = id;

  }

  public String getName() {

  return name;

  }

  public void setName(String name) {

  this name = name;

  }

  public Department getDepart() {

  return depart;

  }

  public void setDepart(Department depart) {

  this depart = depart;

  }

  private int id;

  private String name;

  private Department depart;

  }

  Java代码

  package Domain;

  import java util Set;

  public class Department {

  public int getId() {

  return id;

  }

  public void setId(int id) {

  this id = id;

  }

  public String getName() {

  return name;

  }

  public void setName(String name) {

  this name = name;

  }

  public Set<Employee> getEmps() {

  return emps;

  }

  public void setEmps(Set<Employee> emps) {

  this emps = emps;

  }

  private int id;

  private String name;

  private Set<Employee> emps ;

  }

  Xml代码

  <?xml version= ?>

  <!DOCTYPE hibernate mapping PUBLIC

   //Hibernate/Hibernate Mapping DTD //EN

   mapping dtd >

  <hibernate mapping package= Domain >

  <class name= Employee table= employee >

  <id name= id >

  <generator class= native />

  </id>

  <property name= name unique= true />

  <many to one name= depart column= depart_id />

  </class>

  </hibernate mapping>

  Xml代码

  <?xml version= ?>

  <!DOCTYPE hibernate mapping PUBLIC

   //Hibernate/Hibernate Mapping DTD //EN

   mapping dtd >

  <hibernate mapping package= Domain >

  <class name= Department table= department >

  <id name= id >

  <generator class= native />

  </id>

  <property name= name unique= true />

  <set name= emps >

  <key column= depart_id />

  <one to many class= Employee />

  </set>

  </class>

  </hibernate mapping>

  Java代码

  package Dao;

  import Domain Employee;

  public interface EmployeeDAO {

  public void saveEmployee(Employee emp);

  public Employee findEmployeeByName(String name);

  public Employee findEmployeeById(int id);

  public void updateEmployee(Employee emp);

  public void removeEmployee(Employee emp);

  }

  Java代码

  package Dao;

  import Domain Department;

  public interface DepartmentDAO {

  public void saveDepartment(Department depart);

  public Department findDepartmentByName(String name);

  public Department findDepartmentById(int id);

  public void updateDepartment(Department depart);

  public void removeDepartment(Department depart);

  }

  Java代码

  package Dao Impl;

  import hibernate HibernateException;

  import hibernate Query;

  import hibernate Session;

  import hibernate Transaction;

  import Utils hibernateUtil;

  import Dao EmployeeDAO;

  import Domain Employee;

  public class EmployeeDAOImpl implements EmployeeDAO {

  // 保存员工

  public void saveEmployee(Employee emp) {

  Session s = null;

  Transaction tx = null;

  try{

  s = hibernateUtil getSession();

  tx = s beginTransaction();

  s save(emp);

  mit();

  }catch (HibernateException e) {

  if(tx != null){

  tx rollback();

  }

  throw e;

  }finally{

  if(s != null){

  s close();

  }

  }

  }

  // 根据姓名查询员工

  public Employee findEmployeeByName(String name) {

  Session s = null ;

  try{

  s = hibernateUtil getSession();

  String hql = from Employee as emp where emp name=:name ;

  Query query = s createQuery(hql);

  query setString( name name);

  Employee emp = (Employee) query uniqueResult();

  return emp;

  }finally{

  if(s != null){

  s close();

  }

  }

  }

  // 根据员工id查询员工

  public Employee findEmployeeById(int id) {

  Session s = null ;

  try{

  s = hibernateUtil getSession();

  Employee emp = (Employee) s get(Employee class id);

  return emp;

  }finally{

  if(s != null)

  {

  s close();

  }

  }

  }

  // 更新员工信息

  public void updateEmployee(Employee emp) {

  Session s = null;

  Transaction tx = null;

  try{

  s = hibernateUtil getSession();

  tx = s beginTransaction();

  s update(emp);

  mit();

  }catch (HibernateException e) {

  if(tx != null){

  tx rollback();

  }

  throw e;

  }finally{

  if(s != null){

  s close();

  }

  }

  }

  // 删除员工

  public void removeEmployee(Employee emp) {

  Session s = null;

  Transaction tx = null;

  try{

  s = hibernateUtil getSession();

  tx = s beginTransaction();

  s delete(emp);

  mit();

  }catch (HibernateException e) {

  if(tx != null){

  tx rollback();

  }

  throw e;

  }finally{

  if(s != null){

  s close();

  }

  }

  }

  }

  Java代码

  package Dao Impl;

  import hibernate HibernateException;

  import hibernate Query;

  import hibernate Session;

  import hibernate Transaction;

  import Dao DepartmentDAO;

  import Domain Department;

  import Utils hibernateUtil;

  public class DepartmentDAOImpl implements DepartmentDAO {

  // 保存部门

  public void saveDepartment(Department depart) {

  Session s = null;

  Transaction tx = null;

  try{

  s = hibernateUtil getSession();

  tx = s beginTransaction();

  s save(depart);

  mit();

  }catch (HibernateException e) {

  if(tx != null){

  tx rollback();

  }

  throw e;

  }finally{

  if(s != null){

  s close();

  }

  }

  }

  // 根据name查找部门

  public Department findDepartmentByName(String name) {

  Session s = null ;

  try{

  s = hibernateUtil getSession();

  String hql = from Department as depart where depart name=:name ;

  Query query = s createQuery(hql);

  query setString( name name);

  Department depart = (Department) query uniqueResult();

  return depart;

  }finally{

  if(s != null){

  s close();

  }

  }

  }

  // 根据id查找部门

  public Department findDepartmentById(int id) {

  Session s = null ;

  try{

  s = hibernateUtil getSession();

  Department depart = (Department) s get(Department class id);

  return depart;

  }finally{

  if(s != null)

  {

  s close();

mybatis多表查询用对象接收 hibernate关联关系-多对一

  }

  }

  }

  // 更新部门

  public void updateDepartment(Department depart) {

  Session s = null;

  Transaction tx = null;

  try{

  s = hibernateUtil getSession();

  tx = s beginTransaction();

  s update(depart);

  mit();

  }catch (HibernateException e) {

  if(tx != null){

  tx rollback();

  }

  throw e;

  }finally{

  if(s != null){

  s close();

  }

  }

  }

  // 删除部门

  public void removeDepartment(Department depart) {

  Session s = null;

  Transaction tx = null;

  try{

  s = hibernateUtil getSession();

  tx = s beginTransaction();

  s delete(depart);

  mit();

  }catch (HibernateException e) {

  if(tx != null){

  tx rollback();

  }

  throw e;

  }finally{

  if(s != null){

  s close();

  }

  }

  }

  }

  Java代码

  package Dao Test;

  import hibernate Session;

  import hibernate Transaction;

  import Utils hibernateUtil;

  import Domain Department;

  import Domain Employee;

  public class Many OneTest {

  public static void main(String[] args) {

  add();

  }

  public static Department add(){

  Session s = null ;

  Transaction tx = null;

  try{

  Department depart = new Department();

  depart setName( xuan chuan bu );

  Employee emp = new Employee();

  emp setDepart(depart);// 对象模型 建立两个对象间的关联关系

  emp setName( zhang zuoqiang );

  s = hibernateUtil getSession();

  tx = s beginTransaction();

  s save(depart);

  s save(emp);

  mit();

  return depart;

  }finally{

  if(s != null){

  s close();

  }

  }

  }

lishixinzhi/Article/program/Java/ky/201311/28141  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码