JSF+Spring+JPA(Hibernate实现)的环境搭建(2)

WEB-INF下面加入jsf_core.tldhtml_basic.tldtomahawk.taglib.xml文件、新建一个faces-config.xml文件,内容配置如下:

<!--配置facelets-->

    <application>

       <locale-config>

           <default-locale>zh_CN</default-locale>

       </locale-config>      <view-handler>com.sun.facelets.FaceletViewHandler</view-handler>

    </application>

 

<!—JSFSpring集成-->

    <application>

       <variable-resolver>

           org.springframework.web.jsf.DelegatingVariableResolver

    </variable-resolver>

    </application>

 

再加入facelets框架的标签支持包,整个环境包大致如下图所示:

JSF+Spring+JPA(Hibernate实现)的环境搭建(2)

 

4.       上面的配置好了,JSFSpring就集成起来了。下面我们加入JPA支持,并且将JPASpring集成起来。

首先加入persistence.xml文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>

<persistence xmlns="http://java.sun.com/xml/ns/persistence"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence

    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"

    version="1.0">

 

    <persistence-unit name="JSJDemoPU"

       transaction-type="RESOURCE_LOCAL">

       <provider>org.hibernate.ejb.HibernatePersistence</provider>

       <class>test.vo.Test</class>

       <properties>

           <!-- Hibernate 方言(只有Hibernate 才需要设置) -->

           <property name="hibernate.dialect"

              value="org.hibernate.dialect.MySQLDialect" />

           <!-- Hibernate 显示调试 SQL -->

           <property name="hibernate.show_sql" value="true" />

           <!-- Hibernate 格式化sql -->

           <property name="hibernate.format_sql" value="true" />

           <property name="hibernate.hbm2ddl.auto" value="update" />

       </properties>

    </persistence-unit>

 

</persistence>

 

之后建立一个值对象,代码如下:

package mangerTeam.vo;

 

import java.io.Serializable;

import java.util.Set;

 

import javax.persistence.Basic;

import javax.persistence.CascadeType;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.FetchType;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.JoinColumn;

import javax.persistence.JoinTable;

import javax.persistence.Lob;

import javax.persistence.ManyToMany;

import javax.persistence.ManyToOne;

import javax.persistence.Table;

 

/**

 * 运动员实体

 *

 * @author 刘岩

 */

@Entity

@Table(name = "player")

public class PlayersVO implements Serializable {

 

    @Id

    @GeneratedValue(strategy = GenerationType.AUTO)

    @Column(name = "id", unique = true, nullable = false)

    private Integer id;

 

    @Column(name = "name")

    private String name;

 

    @ManyToOne(optional=true, fetch = FetchType.LAZY)

    @JoinColumn(name = "teamId")

    private TeamVO teamVO;

 

    @ManyToOne(fetch = FetchType.LAZY)

    @JoinColumn(nullable=false,name = "nationalityId")

    private NationalityVO nationalityVO;

 

    @Lob

    @Basic(fetch = FetchType.LAZY)

    @Column(name = "pic")

    private byte[] pic;

 

    @Lob

    @Basic(fetch = FetchType.LAZY)

    @Column(name = "mess")

    private String mess;

 

    @ManyToMany(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)

    @JoinTable(name = "plays_position", joinColumns = { @JoinColumn(name = "playerId") }, inverseJoinColumns = { @JoinColumn(name = "positionId") })

    private Set<PositionVO> positions;

 

    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 Set<PositionVO> getPositions() {

       return positions;

    }

 

    public void setPositions(Set<PositionVO> positions) {

       this.positions = positions;

    }

 

    public TeamVO getTeamVO() {

       return teamVO;

    }

 

    public void setTeamVO(TeamVO teamVO) {

       this.teamVO = teamVO;

    }

 

    public NationalityVO getNationalityVO() {

       return nationalityVO;

    }

 

    public void setNationalityVO(NationalityVO nationalityVO) {

       this.nationalityVO = nationalityVO;

    }

 

    public byte[] getPic() {

       return pic;

    }

 

    public void setPic(byte[] pic) {

       this.pic = pic;

    }

 

    public String getMess() {

       return mess;

    }

 

你可能感兴趣的:(spring,sql,Hibernate,JSF,jpa)