SSH整合(三)

前面已经配置好了spring配置文件,现在需要通过实例来验证spring和hibernate的整合是否成功。

1.  实现将用户信息保存至数据库

     首先需要建立用户信息的实体类,并采用注解的方式实现与数据库的映射关系,代码如下:

    

package com.gyjx19e.om.bean;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity

@Table(name="USER_INFO")

public class Login {

     private String id;

     private String name;

     private String password;

     private String description;

     private String groupId;

     private String createDate;

     private String delFlg;

    

     public Login(){

    

     }

     public Login(String name,String password,String description,String createDate,String delFlg){

     this.name = name;

     this.password = password;

     this.description = description;

     this.createDate = createDate;

     this.delFlg = delFlg;

     }

    

   

    @Id

    @Column(name="ID",length=50)

    @GeneratedValue(generator="idGenerator")

    @GenericGenerator(name="idGenerator",strategy="uuid")

    public String getId() {

       return id;

    }

    public void setId(String id) {

       this.id = id;

    }

   

    @Column(name="NAME",unique=true,nullable=false,length=30)

    public String getName() {

       return name;

    }

    public void setName(String name) {

       this.name = name;

    }

   

    @Column(name="PASSWORD",nullable=false,length=30)

    public String getPassword() {

       return password;

    }

    public void setPassword(String password) {

       this.password = password;

    }

   

    @Column(name="DESCRIPTION",length=100)

    public String getDescription() {

       return description;

    }

    public void setDescription(String description) {

       this.description = description;

    }

   

    @Column(name="GROUP_ID",length=50)

    public String getGroupId() {

       return groupId;

    }

    public void setGroupId(String groupId) {

       this.groupId = groupId;

    }

   

    @Column(name="CREATE_DATE",nullable=false)

    public String getCreateDate() {

       return createDate;

    }

    public void setCreateDate(String createDate) {

       this.createDate = createDate;

    }

   

    @Column(name="DEL_FLG",nullable=false)

    public String getDelFlg() {

       return delFlg;

    }

    public void setDelFlg(String delFlg) {

       this.delFlg = delFlg;

    }

    

}

2. 需要建立业务逻辑层

   接口代码:

  

package com.gyjx19e.om.service;

import com.gyjx19e.om.bean.Login;

public interface LoginService {

    public void save(Login login);

   

}

   实现类代码:

  

package com.gyjx19e.om.service.impl;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import com.gyjx19e.om.bean.Login;

import com.gyjx19e.om.service.LoginService;

@Service @Transactional

public class LoginServiceImpl implements LoginService{

         @Resource SessionFactory sessionFactory;   //注解注入

         public void save(Login login) {

                   System.out.println("2222");

                   sessionFactory.getCurrentSession().persist(login);

         }

}

3. 需要对上述代码进行单元测试,来确定spring和hibernate的集成是否正确

   测试步骤如下:

   a. 新建一个junit单元测试

      右键-new-Junit Test Case- New Junit4 Test

      点击click here 引入包

      包名为junit.test

      命名为LoginTest.java

 

   b. 在LoginTest.java中加入

     

       public class LoginTest{

              private static LoginService loginservice;

            

              @BeforeClass

              public static void setUpBeforeClass() throws Exception{

                    try{

                            ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");

                            loginService = (LoginService)act.getBean("loginServiceImpl");

                     }catch(RuntimeException e){

                            e.printStackTrace();

                      }

               }

              @Test

               public void save(){

                      loginService.save(new Login("w","w","w","w","w"));

                }

              @Test

               public void list(){

                      loginService.list();

               }

相关推荐