hibernate在xml配置中的crud

maven依赖添加5的核心版本即可

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.demo</groupId>
    <artifactId>orm-l</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <properties>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
    </properties>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.48</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.15.Final</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.4.3</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest> <!-- 注意 此为设置程序的主入口-->
                            <mainClass>HouseRun</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

hibernate配置文件hibernate.cfg.xml

<?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">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.username">aa</property>
        <property name="hibernate.connection.password">aa</property>
        <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:1234/lwjdb?useUnicode=true&amp;characterEncoding=utf8&amp;zeroDateTimeBehavior=convertToNull&amp;rewriteBatchedStatements=false&amp;autoReconnect=true</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.autocommit">true</property>

        <mapping resource="User.hbm.xml"/>
        <mapping resource="House.hbm.xml"/>

    </session-factory>
</hibernate-configuration>

 house实体

public class House {
    private Integer houseId;
    private String address;
    private String type;
    private float area;
    private int rental;

    public Integer getHouseId() {
        return houseId;
    }

    public void setHouseId(Integer houseId) {
        this.houseId = houseId;
    }

    public String getAddress() {
        return address;
    }

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

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public float getArea() {
        return area;
    }

    public void setArea(float area) {
        this.area = area;
    }

    public int getRental() {
        return rental;
    }

    public void setRental(int rental) {
        this.rental = rental;
    }

    @Override
    public String toString() {
        return "House{" +
                "houseId=" + houseId +
                ", address='" + address + '\'' +
                ", type='" + type + '\'' +
                ", area=" + area +
                ", rental=" + rental +
                '}';
    }
}

 

house.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="House" table="house" dynamic-update="true">
        <id name="houseId" column="id">
            <generator class="native"/>
        </id>
        <property name="address" column="address" type="string"/>
        <property name="type" column="type" type="string"/>
        <property name="area" column="area" type="float"/>
        <property name="rental" column="rental" type="int"/>
    </class>
</hibernate-mapping>

 

通用crud方法

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;

import java.util.List;

public class HiberUtils {

    private static Session session;
    private static SessionFactory sessionfactory;

    static {
        init();
    }
    private static void init(){
        //读取配置文件
        Configuration conf=new Configuration().configure();
        //根据配置创建factory
        sessionfactory=conf.buildSessionFactory();
        //获得操作数据库的session对象
        session=sessionfactory.openSession();
    }

    public static void save(Object obj){
        if(session == null) init();
        session.save(obj);
    }

    public static void close(){
        //关闭资源
        session.close();
        sessionfactory.close();
    }

    public static <T> List<T> getAll(Class<T> t){
        Query query = session.createQuery("from "+t.getName());
        return query.list();
    }

    public static <T> void delObj(Class<T> t,Integer id){
        session.beginTransaction();
        session.delete(getObj(t,id));
        session.getTransaction().commit();
    }
    public static <T> T getObj(Class<T> t,Integer id){
        return session.get(t,id);
    }

    public static void updateObj(Object obj){
        session.beginTransaction();
        session.update(obj);
        session.getTransaction().commit();
    }

}

 

增加获取根据session直接可以拿到

而update和del操作则需要事务,否则不成功

测试类

import java.util.List;

public class HouseRun {
    public static void main(String[] args) {
        House house = new House();
        house.setAddress("北京一区");
        house.setArea(78.9f);
        house.setRental(123);
        house.setType("一室一厅");
        //addHouse(house);
        System.out.println(getAllHost());
        //System.out.println(getByid(1));
        //updateRental(1,666);
        delHouse(2);
        HiberUtils.close();
    }

    //添加
    public static void addHouse(House house){
        HiberUtils.save(house);
    }
    //获取所有
    public static List<House> getAllHost(){
        return HiberUtils.getAll(House.class);
    }
    //删除
    public static void delHouse(Integer id){
        HiberUtils.delObj(House.class,id);
    }
    //获取单个
    public static House getByid(Integer id){
        return HiberUtils.getObj(House.class,id);
    }
    //修改租金
    public static void updateRental(Integer id,Integer newRental){
        House house = getByid(id);
        house.setRental(newRental);
        HiberUtils.updateObj(house);
    }

}

 

整体来说,hibernate的crud操作比较方便快捷,适合场景范围一般,但也提供了类似mybatis的通用sql操作,数据和实体之间映射也必将方便,但缺点是,不了解底层,结果有时候不可控

 ¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥

感谢阿贝云提供的免费云服务器和免费虚拟主机,1C1G5M配置,搭配内网穿透,真香,看视频听歌曲无压力,*

运行起来也相当流畅,网速个人使用是真的赞,欢迎大家使用

¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥

 

 

 

 

hibernate.cfg.xml

上一篇:LeetCode 198:House Robber


下一篇:hibernate在通用crud