标签:factor demo webapp 数据 cte show build drive username
1. 什么是hibernate(MyBatis)
ORM框架/持久层框架
ORM(Object Relational Mapping):对象关系映射。
对象与关系型数据间之间的映射管理框架
优势:跨数据库的无缝移植(SqlServer、Oracle、MySql)
如何使用hibernate
1.在项目中添加hibernate(5.2.12.Final)支持(手动添加)
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>zking</groupId> <artifactId>hibernate01</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>hibernate01 Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <junit.vesion>4.12</junit.vesion> <servlet.vesion>4.0.0</servlet.vesion> <!--hibernate(5.2.12 Final) --> <hibernate-vesion>5.2.12.Final</hibernate-vesion> <!--mysql+ --> <mysql.version>5.1.44</mysql.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.vesion}</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet.vesion}</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate-vesion}</version> </dependency> <!--mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> </dependencies> <build> <finalName>hibernate01</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
2.在resource目录下添加hibernate.cfg.xml(核心配置文件)
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> <!--1.数据库相关 --> <!--(connection.username|connection.password|connection.url|connection.driver_class|dialect) --> <!--数据库账号 --> <property name="connection.username">root</property> <!--数据库密码 --> <property name="connection.password">123</property> <!--数据库连接的url --> <!--特殊字符!!! 特殊字符!!! 特殊字符!!!--> <property name="connection.url"> jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=UTF-8 </property> <!-- 数据库连接驱动 --> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <!--数据库方言 --> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <!--2.调试相关 --> <!--(show_sql|format_sql) --> <property name="show_sql">true</property> <property name="format_sql">true</property> <!--3.实体映射相关 --> <mapping resource="com/hibernate01/entity/Book.hbm.xml"/> </session-factory> </hibernate-configuration>
实体类
public class Book implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private Integer bid; private String bname; private Float price; private String image; public String getImage() { return image; } public void setImage(String image) { this.image = image; } public Integer getBid() { return bid; } public void setBid(Integer bid) { this.bid = bid; } public String getBname() { return bname; }
实体映射文件
Book.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> <!-- name:对应实体类的全路径名 table:实体类的数据库表名 --> <class name="com.hibernate01.entity.Book" table="t_mvc_book"> <!-- id:实体属性名 type:实体属性类型 column:对应列名 --> <id name="bid" type="java.lang.Integer" column="bid"> <generator class="increment"/> </id> <property name="bname" type="java.lang.String" column="bname"/> <property name="price" type="java.lang.Float" column="price"/> <property name="image" type="java.lang.String" column="image"/> </class> </hibernate-mapping>
接下来测试一个Demo完成对t_mvc_book 的增删改查
public class Demo { public static void main(String[] args) { // 4.1 CRUD操作步骤 // 4.1.1 读取配置 // 4.1.2 创建SessionFactory // 4.1.3 打开Session // 4.1.4 开启事务 // 4.1.5 CURD // 4.1.6 提交事务/回滚事务 // 4.1.7 关闭Session //1.读取配置,加载hibernate核心配置文件 Configuration config=new Configuration(); config.configure("hibernate.cfg.xml"); //2.创建SessionFactory(1) SessionFactory SessionFactory = config.buildSessionFactory(); //3.打开Session //注:此Session与jsp/Servlet的session要区别对待 Session session= SessionFactory.openSession(); //4.开启事务 Transaction trans = session.beginTransaction();
新增一本书
//5.crud Book book=new Book(); //5.1 save(保存) // book.setBname("历史"); book.setPrice(999f); session.save(book);
查询单个
删除历史
修改资治通鉴为本草纲目
标签:factor demo webapp 数据 cte show build drive username
原文地址:https://www.cnblogs.com/xmf3628/p/11281800.html