在eclipse中创建一个简单的hibernate项目

1.在eclipse中新建一个Java web项目

 2.输入项目名称,点击完成

 3.在lib文件中导入jar包

 链接:https://pan.baidu.com/s/1AjhcveNCVokizsrrF_-YLA?pwd=gza8

提取码:gza8
复制这段内容后打开百度网盘手机App,操作更方便哦

4.在项目的src目录下,新建一个空白文件,命名为hibernate.properties

 hibernate.properties代码如下:

hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
hibernate.connection.username = root
hibernate.connection.password = 123456
hibernate.show_sql = true

 注意:上述url连接字符串的&是&符号的转义字符

5.在src目录中创建一个包com.bean.hibernate,并创建一个User类

package com.bean.hibernate;

public class User {
    private String username;
    private String password;
    private String mail;
    private String phone;
    
    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 String getMail() {
        return mail;
    }
    public void setMail(String mail) {
        this.mail = mail;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
}
View Code

6.在MySQL中创建数据库和表

 7.在com.bean.hibernate包中添加一个文件命名为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">

<hibernate-mapping>
    <class name="com.bean.hibernate.User" table="user" catalog="test">
        <id name="username" type="java.lang.String">
            <column name="username" length="45" />
        </id>
        <property name="password" type="java.lang.String">
            <column name="password" length="45" not-null="true" />
        </property>
        <property name="mail" type="java.lang.String">
            <column name="mail" length="45" not-null="true" />
        </property>
        <property name="phone" type="java.lang.String">
            <column name="phone" length="45" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

8.在com.bean.hibernate包中添加一个UserTest类

package com.bean.hibernate;

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

public class UserTest {
    //配置hibrenate,启动时从配置文件读取相应信息
    Configuration con = new Configuration();
    //创建SessionFactory,代表一个数据源
    //User.class是要关联的JavaBean
    SessionFactory sf = con.
      addClass(User.class).buildSessionFactory();
    
    public void addUser(){
        //创建一个Java对象,并给各属性相应值
        User user = new User();
        user.setUsername("张三");
        user.setPassword("zhangsan");
        user.setMail("zhangsan@163.com");
        user.setPhone("12121212");

        //创建Session
        Session session = sf.openSession();
        //开启事务
        Transaction ctx = session.beginTransaction();
        //把user对象保存到表中
        session.save(user);
        //提交事务
        ctx.commit();
        //关闭Session
        session.close();
        System.out.print("插入记录成功,请查看User表数据。 ");    
    }
    
    public void queryUser(){
        Session session = sf.openSession();
        Transaction ctx = session.beginTransaction();
        //从User表中查询相关记录,
        //User.class是要关联的JavaBean,"张三"为表的主键
        User u = (User)session.load(User.class, "张三");
        System.out.println("从User表查询到的数据如下:");
        System.out.print(u.getUsername()+"  ");
        System.out.print(u.getPassword()+"  ");
        System.out.print(u.getMail()+"  ");
        System.out.println(u.getPhone()+"  ");
        ctx.commit();
        session.close();
    }
    
    public void updateUser(){
        Session session = sf.openSession();
        Transaction ctx = session.beginTransaction();
        User u = (User)session.load(User.class, "张三");
        u.setPassword("12345678");
        u.setMail("12345678@qq.com");
        u.setPhone("13710598723");
        //更新User表相应记录
        session.update(u);    
        ctx.commit();
        session.close();
        System.out.print("修改记录成功,请查看User表数据。 ");
        //调上面的queryUser()方法查询记录
        queryUser();
    }
    
    public void deleteUser(){
        Session session = sf.openSession();
        Transaction ctx = session.beginTransaction();
        User u = (User)session.load(User.class, "张三");
        //从User表中删除记录
        session.delete(u);
        ctx.commit();
        session.close();
        System.out.println("删除记录成功,请查看User表数据。");    
    }
    
    public static void main(String[] args){
        UserTest client = new UserTest();
        client.addUser();
        client.queryUser();
        client.updateUser();
        //client.deleteUser();
    }
}

9.测试

 数据库中的记录为:

 数据库表中文字符乱码问题解决,原因在于eclipse中properties文件编码格式的问题。

 

posted @ 2023-04-20 15:44  YorkShare  阅读(101)  评论(0编辑  收藏  举报