小试ssh

小试ssh

pom.xml




  4.0.0

  com.wanmait
  hospitalHiber
  1.0-SNAPSHOT
  war

  hospitalHiber Maven Webapp
  
  http://www.example.com

  
    UTF-8
    1.8
    1.8
    
    5.1.8 RELEASE
  

  
    
      junit
      junit
      4.11
      test
    
    
      javax.servlet.jsp
      javax.servlet.jsp-api
      2.3.3
      provided
    
    
      javax.servlet
      javax.servlet-api
      4.0.1
      provided
    
    
      mysql
      mysql-connector-java
      8.0.16
      runtime
    
    
    
      org.springframework
      spring-webmvc
      ${spring.version}
    
    
      org.springframework
      spring-test
      ${spring.version}
      test
    
    
      org.springframework
      spring-jdbc
      ${spring.version}
    

    
      com.alibaba
      druid
      1.1.17
    
    
      commons-fileupload
      commons-fileupload
      1.4
    
    
      commons-io
      commons-io
      2.6
    
    
      com.fasterxml.jackson.core
      jackson-databind
      2.9.9
    
    
      com.fasterxml.jackson.core
      jackson-annotations
      2.9.9
    
    
      org.apache.logging.log4j
      log4j-core
      2.11.2
    
    
      javax.servlet
      jstl
      1.2
    
    
      commons-codec
      commons-codec
      1.12
    
    
      com.gitee.qdbp.thirdparty
      ueditor
      1.4.3.3
    
    
      org.json
      json
      20180813
    
    
      com.github.pagehelper
      pagehelper
      5.1.10
    
    
      org.aspectj
      aspectjweaver
      1.9.4
    
    
      net.sf.ehcache
      ehcache
      2.10.6
    
    
    
      org.hibernate
      hibernate-core
      5.4.3.Final
    
    
      org.springframework
      spring-orm
      ${spring.version}
    

  
  
    hospitalHiber
    
      
        
          maven-clean-plugin
          3.1.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.8.0
        
        
          maven-surefire-plugin
          2.22.1
        
        
          maven-war-plugin
          3.2.2
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
      
    
  


spring.xml




    
    
    
        
        
        
        
        
        
        
        
        
    

    
    
        
        
            
                org.hibernate.dialect.MySQL8Dialect
                true
                true
                jdbc:mysql://localhost:3306/hospital
                com.mysql.cj.jdbc.Driver
            
        
        
            
                classpath:mapping/*.hbm.xml
            
        
    

    
    
        
    
    

    
    
    


小试ssh_第1张图片
小试ssh_第2张图片
小试ssh_第3张图片
小试ssh_第4张图片
小试ssh_第5张图片
小试ssh_第6张图片
小试ssh_第7张图片

Department.hbm.xml





    
        
        
            
        

        
        
        
        


        
        
        
            
                
                
            
            
        
    

Doctor.hbm.xml





    
        
        
            
        
        
        
        
        
        
        
            
        
    

DepartmentDAOImpl

package com.wanmait.ssh.dao.impl;

import com.wanmait.ssh.dao.DepartmentDAO;
import com.wanmait.ssh.pojo.Department;
import com.wanmait.ssh.pojo.Doctor;
import com.wanmait.ssh.service.DepartmentService;
import com.wanmait.ssh.util.Pager;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/*让spring管理*/
@Repository
public class DepartmentDAOImpl implements DepartmentDAO {

    /*注入session工厂*/
    @Resource
    private SessionFactory sessionFactory;
    public Session getSession()
    {
        return this.sessionFactory.getCurrentSession();
    }


    /*插入一条新数据*/
    @Override
    public void insert(Department object) {
        //临时对象
        this.getSession().save(object);//持久化对象 持久化对象在事务结束时会和数据库进行同步
        object.setTitle("喉科");
        //this.getSession().update(object);  //更新
        //事务结束session关闭  对象变为脱管对象


        /*婚姻的三种状态:未婚 已婚 离异

        对象的三种状态:
        临时对象/瞬时对象: 	对象和session没有任何关系,未经session操作
        持久对象、持久化对象:
        持久化对象在事务结束时会和数据库进行同步
        经过session进行save update get .....
        脱管对象、离线对象:
        session关闭之后*/
    }

    /*更新*/
    @Override
    public void update(Department object) {
        this.getSession().update(object);
    }

    /*假删除*/
    @Override
    public void delete(Integer id) {
        String hql = "update Department set visible=false where id=:id";
        this.getSession().createQuery(hql).setParameter("id",id).executeUpdate();

    /*        Query query = this.getSession().createQuery(hql);
        query.setParameter("id",id);
        query.executeUpdate();*/

        /*真删*/
        /*this.getSession().delete(questionType);*/
    }

    @Override
    public void delete(String ids) {

    }


    /*根据Id查询单条数据*/
    @Override
    public Department findById(Integer id) {
       /* String hql = "from Department dp left join fetch dp.doctors where dp.id=:id";
        Queryquery = this.getSession().createQuery(hql,Department.class);
        query.setParameter("id",id);
        return query.uniqueResult();*/

        /*return (Department) this.getSession().get("com.wanmait.ssh.pojo.Department",id);*/

/*============================================================================*/
           /*Department department = this.getSession().load(Department.class,id);
            Hibernate.initialize(department.getDoctors());
            return department;*/
/*============================================================================*/
        /*load 延迟查询,懒加载,用到的时候再发送sql语句取查询
                先把id给一个代理对象
        如果查询时session已经关闭,会报异常:
        org.hibernate.LazyInitializationException: could not initialize proxy  - no Session
        如果给定的id查询不到对象,报异常:
        org.hibernate.ObjectNotFoundException: No row with the given identifier exists*/
/*============================================================================*/
       /*使用Hibernate.intialize()初始化
            findById: Hibernate.initialize(question.getQuestionType());
            findAll:
            questions =  query.list();
            for (Question question : questions) {
                Hibernate.initialize(question.get    QuestionType());
            }*/
/*============================================================================*/
        /*设置lazy=false
            
                
            
        findById: 发送2条sql语句,一条sql语句查询question,一条sql语句查询questionType
        findAll:如果有n条数据,那么发送1+m(m<=n)条sql语句

        不建议使用,不管什么情况,只要查询question就会同时把questionType查询出来*/
/*============================================================================*/

        return this.getSession().get(Department.class,id);
    }


    /*默认查全部且去重复*/
    @Override
    public List findAll() {
        Listdepartments = null;
        /*distinct 去重复*/
        String hql = "select distinct dp from Department dp left join fetch dp.doctors where dp.visible=true";
        Query query = this.getSession().createQuery(hql,Department.class);
        departments = query.list();

        return departments;
    }

    @Override
    public List findAll(Department object) {
        return null;
    }

    @Override
    public List findAll(Pager pager) {
        return null;
    }

    @Override
    public List findAll(Pager pager, Department object) {
        return null;
    }

    /*查询数据总数*/
    @Override
    public int getDataCount() {
        int count = 0;
        String hql = "select count(*) from Department where visible=true";
        count = (this.getSession().createQuery(hql,Long.class).uniqueResult()).intValue();

        return count;
    }

    @Override
    public int getDataCount(Department object) {
        return 0;
    }
}

测试类

package com.wanmait.ssh.service.impl.test;

import com.wanmait.ssh.dao.DepartmentDAO;
import com.wanmait.ssh.pojo.Department;
import com.wanmait.ssh.service.DepartmentService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class DepartmentServiceImplTest {
    @Resource
    private DepartmentService departmentService;

    /*默认查全部且去重复*/
    @Test
    public void testFindAll()
    {
        Listdepartments = departmentService.findAll();
        departments.forEach(department -> System.out.println(department.getTitle()));
    }

    /*根据Id查询单条数据*/
    @Test
    public void testFindById()
    {
        Department department = departmentService.findById(1);
        System.out.println(department.getTitle());
    }

    /*查询数据总数*/
    @Test
    public void testGetCount()
    {
        System.out.println(departmentService.getDataCount());
    }

    /*新增一条数据*/
    @Test
    public void testInsert()
    {
        Department department = new Department();
        department.setDoctorCount(23);
        department.setTitle("疯牛病");

        departmentService.insert(department);
    }

    /*假删除*/
    @Test
    public void testDelete()
    {
        departmentService.delete(1);
    }
}

DoctorDAOImpl

package com.wanmait.ssh.dao.impl;

import com.wanmait.ssh.dao.DoctorDAO;
import com.wanmait.ssh.pojo.Doctor;
import com.wanmait.ssh.util.Pager;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/*让spring管理*/
@Repository
public class DoctorDAOImpl implements DoctorDAO {

    /*注入session工厂*/
    @Resource
    private SessionFactory sessionFactory;
    public Session getSession()
    {
        return this.sessionFactory.getCurrentSession();
    }


    @Override
    public void insert(Doctor object) {

    }

    @Override
    public void update(Doctor object) {

    }

    @Override
    public void delete(Integer id) {

    }

    @Override
    public void delete(String ids) {

    }


    /*根据Id查询单条数据*/
    @Override
    public Doctor findById(Integer id) {
        return this.getSession().createQuery("from Doctor d left join fetch d.department where d.id=:id",Doctor.class).setParameter("id",id).uniqueResult();
    }

    /*默认查全部*/
    @Override
    public List findAll() {
        Listdoctors = null;
        String hql = "from Doctor d left join fetch d.department dp where d.visible=true and dp.visible=true order by d.id desc";
        Query query = this.getSession().createQuery(hql,Doctor.class);
        doctors = query.list();

        return doctors;
    }

    @Override
    public List findAll(Doctor object) {
        return null;
    }

    @Override
    public List findAll(Pager pager) {
        return null;
    }

    @Override
    public List findAll(Pager pager, Doctor object) {
        return null;
    }

    @Override
    public int getDataCount() {
        return 0;
    }

    @Override
    public int getDataCount(Doctor object) {
        return 0;
    }


    /*根据关联表的Id查询数据*/
    @Override
    public List findAllById(Integer departmentId) {
        return this.getSession().createQuery("from Doctor d where department.id=:departmentId and d.visible=true",Doctor.class).setParameter("departmentId",departmentId).list();
    }
}

测试类

package com.wanmait.ssh.service.impl.test;

import com.wanmait.ssh.pojo.Doctor;
import com.wanmait.ssh.service.DoctorService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class DoctorServiceImplTest {

    @Resource
    private DoctorService doctorService;

    /*默认查全部*/
    @Test
    public void testFindAll()
    {
        Listdoctors = doctorService.findAll();
        doctors.forEach(doctor -> System.out.println(doctor.getName()));
    }


    /*根据Id查询单条数据*/
    @Test
    public void testFindById()
    {
        System.out.println(doctorService.findById(1).getDepartment().getTitle());
    }

    /*根据关联表Id查询数据*/
    @Test
    public void testFindAllById()
    {
       Listdoctors = doctorService.findAllById(2);
       doctors.forEach(doctor -> System.out.println(doctor.getName()));
    }
}

你可能感兴趣的:(ssh,Hibernate,Maven)