Hibernate(二)關於Hibernate的增刪改操做

建立一個maven項目,在pom.xml文件中引入對應的節點java

<?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">
    <parent>
        <artifactId>MY</artifactId>
        <groupId>cn.my</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>06Hibernate</artifactId>
    <packaging>war</packaging>

    <name>06Hibernate Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.3</version>
            <scope>test</scope>
        </dependency>

        <!--hibernate core 核心jar-->
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.0.6.Final</version>
        </dependency>
        <!--hibernate jta 管理事務-->
        <!-- https://mvnrepository.com/artifact/javax.transaction/jta -->
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>
        <!-- 添加Log4J依賴 -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
        <!--slf4j-nop-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-nop</artifactId>
            <version>1.6.4</version>
        </dependency>
        <!-- 添加javassist -->
        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.0.GA</version>
        </dependency>
        <!--hibernate-jpa-->
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
        <!-- mysql數據庫的驅動包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
    </dependencies>

    <build>
       <!--這裏是配置全部的xml文件均可以被讀取到-->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>

在resources下建立一個hibernamte.cfg.xml文件,mysql

<?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.url">jdbc:mysql:///s2228</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>
        <!--是否顯示sql-->
        <property name="show_sql">true</property>
        <!--是否格式化-->
        <property name="format_sql">false</property>
        <!--hbm2ddl   就是把xx.hbm.xml文件中的配置生成數據庫中DDL(數據定義語言)
        create:  每次運行都會刪除 上次生成的表,還會建立新的!
        update: 沒有表會自動建立,有表就增長數據!
        validate: 若是hbm文件和數據庫中的字段對應 就會新增,
                  不然拋出異常!
       create-drop:  每次運行都會刪除 上次生成的表,前提是sessionFactory關閉
        -->
        <property name="hbm2ddl.auto">update</property>
        <!--須要加載的hbm.xml文件  讀取小配置中的文件-->
        <mapping resource="cn/my/entity/Teacher.hbm.xml"></mapping>
    </session-factory>
</hibernate-configuration>  

 

建立一個實體類Teacherweb

package cn.my.entity;

/**
 * Created by mycom on 2018/5/25.
 */
public class Teacher {
    private Integer id;
    private String name;

    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;
    }
}
View Code

編寫對應實體類的一個配置文件Teacher.hbm.xml,這個文件至關於mybatis中的小配置的文件sql

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.my.entity">
    <class name="Teacher" table="teacher" lazy="false">
        <id name="id" column="id">
            <!--generator 主鍵生成策略
            assigned:主鍵是由程序猿本身設置的!
            identity: 針對於mysql數據庫的主鍵自增 (mysql數據庫說了算)
            sequence: 針對於oracle數據庫中的序列
                <generator class="sequence">
                  <param name="sequence">序列名稱</param>
               </generator>
           increment: 首先會產生一條sql select max(id)  from teacher
                      以後id+1  賦值給主鍵
            -->
            <generator class="assigned"></generator>
        </id>
        <property name="name" column="name"></property>
    </class>
</hibernate-mapping>

注意點:數據庫

   Hbm.xml必定要被cfg.xml管理!apache


建立測試類,在測試類中測試增刪改
api

 

 /***
     * 新增數據
     */
    @Test
    public void addTest(){
        //建立對象
        Teacher teacher=new Teacher();
        teacher.setId(2);
        teacher.setName("王老師");
        System.out.println("==================================");
        //真正向數據庫添加
        session.save(teacher);
        System.out.println("==================================");
    }
    /***
     * 刪除數據
     */
    @Test
    public void delTest(){
        //建立對象
        Teacher teacher=new Teacher();
        teacher.setId(2);
        System.out.println("==================================");
        //刪除數據庫中的信息
        session.delete(teacher);
        System.out.println("==================================");
    }
    /***
     * 修改數據
     */
    @Test
    public void updateTest(){
        //建立對象
        Teacher teacher=new Teacher();
        teacher.setId(1);
        teacher.setName("趙進沛");
        System.out.println("==================================");
        //刪除數據庫中的信息
        session.update(teacher);
        System.out.println("==================================");
    }
相關文章
相關標籤/搜索