Simple application of Eclipse+struts+hibernate

Source: Internet
Author: User
Tags generator ssh

On the basis of the last struts, the integration of Hibernate, still refer to the online case of the Great God Code, due to the issue of the version of the place has been modified.

Eclipse+struts address--point here

On this basis, need to use the relevant version information (need to leave a message ^.^):

Hibernate:hibernate-release-4.3.10.final

Mysql:mysql Server 5.5

Jdbc:mysql-connector-java-5.1.26-bin.jar

Recommendation: Everyone in use when the package name is best changed to their own so in the process of modification, you can better understand the process of mapping.

The steps here draw on the original narrative, very detailed, the great God of perceptual Pathfinder.

1. Copy the jar package under lib/required in Hibernate and the jar package under LIB/JPA to the Sshproject project's lib directory.

2. Add the Com.tiny.ssh.dao and Com.tiny.ssh.daoImpl packages based on the struts project above. Create the Basedao interface and the Userdao class, respectively.

Organize the structure diagram (note the location and name of the corresponding file):


3. Create the corresponding table user in the database to write to the users to be registered.

CREATE TABLE User (
userId int auto_increment,
userName varchar () not NULL,
password varchar ( ) Not NULL,
gender int not NULL,
primary KEY (UserId)
);


4. Create a hibernate mapping file for the user table. Because this is where you create the Pojo class for the user table, So here I change the previous Com.tiny.ssh.beans package into a com.tiny.ssh.forms package, which is used to store the form data passed through the page, and create a new Com.tiny.ssh.beans package to create a Pojo object to correspond to the tables in the database. Path: Pojo a similar directory.

User.java:

Package Com.tiny.ssh.beans;

public class User {
    
    private int userId;
    Private String username;
    private String password;
    private int gender;
    
    public int getUserId () {
        return userId;
    }
    
    public void Setuserid (int userId) {
        This.userid = userId;
    }
    
    Public String GetUserName () {
        return username;
    }

    public void Setusername (String username) {
        this.username = username;
    }

    Public String GetPassword () {
        return password;
    }

    public void SetPassword (String password) {
        this.password = password;
    }

    public int Getgender () {
        return gender;
    }

    public void Setgender (int gender) {
        this.gender = gender;
    }

}

User.hbm.xml:

<?xml version= "1.0" encoding= ' UTF-8 '?>
<! DOCTYPE hibernate-mapping public
                            "-//hibernate/hibernate mapping DTD 3.0//en"
                            "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">


5. Create a Hibernate profile. Path: src directory.

Hibernate.cfg.xml (you need to change your database connection and the bottom-most mapping):

<?xml version= ' 1.0 ' encoding= ' utf-8 '?>
<! DOCTYPE hibernate-configuration public
"-//hibernate/hibernate configuration DTD 3.0//en"
"http// Www.hibernate.org/dtd/hibernate-configuration-3.0.dtd ">

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.