栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring集成JSF、Hibernate

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring集成JSF、Hibernate

创建一个JSF项目,推荐用Tomcat做服务器

IDEA 2020.2 部署JSF项目

导入依赖项



    4.0.0

    org.example
    jsf-hibernate-01
    1.0-SNAPSHOT

    
        UTF-8
        1.8
        1.8
    

    
        
            junit
            junit
            4.11
            test
        
        
            org.springframework
            spring-core
            4.0.3.RELEASE
        
        
            org.springframework
            spring-webmvc
            4.0.3.RELEASE
        
        
            org.springframework
            spring-tx
            4.0.3.RELEASE
        
        
            javax.inject
            javax.inject
            1
        
        
            aspectj
            aspectjrt
            1.5.4
        
        
            org.springframework
            spring-orm
            4.1.0.RELEASE
        
        
            org.hibernate
            hibernate-core
            4.3.5.Final
        
        
            org.hibernate
            hibernate-entitymanager
            4.3.5.Final
        
        
            mysql
            mysql-connector-java
            8.0.24
        
        
            com.alibaba
            druid
            1.2.4
        
        
            com.sun.faces
            jsf-api
            2.1.7
        
        
            com.sun.faces
            jsf-impl
            2.1.7
        
        
            org.slf4j
            slf4j-api
            2.0.0-alpha1
            compile
        
        
            ch.qos.logback
            logback-classic
            1.2.5
        
        
            javax.servlet
            servlet-api
            2.5
        
        
            javax.servlet.jsp.jstl
            jstl
            1.2
        
        
            javax.servlet.jsp
            jsp-api
            2.2.1-b03
        
        
            com.sun.el
            el-ri
            1.0
        
        
            javax
            javaee-web-api
            8.0.1
        
    

    
        
            
                maven-war-plugin
                3.3.0
                
                    WebContent
                    false
                
            
            
                maven-compiler-plugin
                3.1
                
                    1.8
                    1.8
                
            
        
        ${project.artifactId}
    


创建 faces-config.xml,配置SpringBeanFacesELResolver在faces,进行JSF与Spring框架集成



    
        
            org.springframework.web.jsf.el.SpringBeanFacesELResolver
        
    

配置web.xml


    
        Faces Servlet
        javax.faces.webapp.FacesServlet
        1
    

    
        Faces Servlet
        *.xhtml
    

    
        
            org.springframework.web.context.ContextLoaderListener
        
    
    
        
            org.springframework.web.context.request.RequestContextListener
        
    

    
        javax.faces.PROJECT_STAGE
        Development
    

    
        
            30
        
    

    
        index.xhtml
    

创建实体类
package org.example.pojo;


import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;


@Entity(name = "student")
@ManagedBean(name = "student")
@RequestScoped
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String name;

    private String address;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", address='" + address + ''' +
                '}';
    }
}

创建service
package org.example.service;

import org.example.pojo.Student;

import java.util.List;


public interface StudentService {
    boolean insertStudent(Student student);

    Student selectStudentById(Integer id);

    List selectAllStudents();

    boolean deleteStudentById();
}

实现service

package org.example.service.impl;

import org.example.dao.StudentDAO;
import org.example.dao.impl.StudentDAOImpl;
import org.example.pojo.Student;
import org.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.util.List;


@Service("studentService")
@ManagedBean(name = "studentService")
@SessionScoped
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDAO studentDAO;

    @Override
    public boolean insertStudent(Student student) {
        System.out.println(student);
        return studentDAO.insertStudent(student);
    }

    @Override
    public Student selectStudentById(Integer id) {
        return studentDAO.selectStudentById(id);
    }

    @Override
    @Transactional
    public List selectAllStudents() {
        return studentDAO.selectAllStudents();
    }

    @Override
    public boolean deleteStudentById() {
        return studentDAO.deleteStudentById();
    }

    public StudentDAO getStudentDAO() {
        return studentDAO;
    }

    public void setStudentDAO(StudentDAO studentDAO) {
        this.studentDAO = studentDAO;
    }
}

创建dao
package org.example.dao;

import org.example.pojo.Student;

import java.util.List;


public interface StudentDAO {
    boolean insertStudent(Student student);

    Student selectStudentById(Integer id);

    List selectAllStudents();

    boolean deleteStudentById();
}

实现dao
package org.example.dao.impl;

import org.example.dao.StudentDAO;
import org.example.pojo.Student;
import org.example.util.SessionFactoryUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;

import java.util.List;



@Repository
public class StudentDAOImpl implements StudentDAO {
    @Override
    public boolean insertStudent(Student student) {
        Session session = SessionFactoryUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        try {
            session.save(student);
            transaction.commit();
            return true;
        } catch (Exception a) {
            return false;
        }
    }

    @Override
    public Student selectStudentById(Integer id) {
        return null;
    }

    @Override
    public List selectAllStudents() {
        Session session = SessionFactoryUtil.getSessionFactory().openSession();
        Query query = session.createQuery("FROM org.example.pojo.Student");
        return query.list();
    }

    @Override
    public boolean deleteStudentById() {
        return false;
    }
}

创建工具类
package org.example.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class SessionFactoryUtil {
    static SessionFactory sessionFactory;
    static {
        sessionFactory = new Configuration().configure().buildSessionFactory();
    }

    public static SessionFactory getSessionFactory(){
        return sessionFactory;
    }
}

创建spring 配置文件, applicationContext.xml

    
    

    
    
        
        
        
    

    
    
        
    

    
    
        
        
        
            
                org.example.pojo.Student
            
        
        
        
            
                org.hibernate.dialect.MySQLDialect
                true
            
        
    

    
    

    
        
    

    
    

    
    

配置文件 jdbc.properties
jdbc_url=jdbc:mysql://localhost:3306/esms
jdbc_username=root
jdbc_password=123456

创建页面 index.xhtml





   JSF Spring Hibernate Integration
   


   

Add a Person


Persons List

Student ID Student Name Student Address
${stu.id} ${stu.name} ${stu.address}
建表
create table student
(
	id int auto_increment,
	name varchar(50) null,
	address varchar(50) null,
	constraint student_pk
	primary key (id)
);

部署运行

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/273166.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号