目录
环境:eclipse+spring+struts2+hibernate
2. model包中,新建一个javabean Student,并且在此包中新建一个xml文件,名字叫 Student.hbn.xml
3. junit测试,add jar junit新建一个test包,包中新建一个class myapp来测试
4.run as junit即可实现将该student信息插入到表student中
5,数据库表curricula 数据库=========================================学生Student(id,name,pwd,phone,grade,photo)
多表关联查询:select 图书名称,名称 ,价格 from 图书表,图书类别表 where 图书表.类别=图书类别表.id
sql与hqlsql:查找数据库记录hql:hibernate query language 查找对象
4.$.ajax({});//默认是异步的,需要设置属性 async:"false",此时同步
犯下的错:注解应该放到get前面,不要放到set前面,会报错的
环境:eclipse+spring+struts2+hibernate
hibernate学习
第一部分环境配置:
-
1.添加hibernate的jar包 lib/required
-
2.添加struts2的jar包 apps/解压一个项目blank/lib
-
3.添加mysql数据库连接驱动
-
4.添加struts2的配置文件struts.xml
-
5.添加hibernate的配置文件hibernate.cfg.xml
1. hibernate.cfg.xml
<!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 >
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/curricula</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="show_sql">true</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect 指定与数据库打交道的语句类型-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
2. model包中,新建一个javabean Student,并且在此包中新建一个xml文件,名字叫 Student.hbn.xml
该文件配置如下(配置的是一个数据表student):
<?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="model">
<!-- table 可省略,默认是类名 -->
<class name="Student" table="student">
<id name="id" column="id">
<generator class="native"/><!-- native相当于自增长 -->
</id>
<property name="name" column="name" type="string" length="20"></property>
<property name="pwd" column="pwd" type="string" length="50"></property>
<property name="phone" column="phone" type="string" length="50"></property>
<property name="grade" column="grade" type="string" length="50"></property>
<property name="photo" type="string" length="150"></property>
</class>
</hibernate-mapping>
3. junit测试,add jar junit
新建一个test包,包中新建一个class myapp来测试
myapp 类代码如下:
public class MyApp {
@Test
public void testSave()
{
Session session=HibernateUtil.openSession();
Student student=new Student();
student.setName("张三");
student.setPwd("123456778");
student.setGrade("greade1");
student.setPhone("17863110675");
Transaction tx=session.beginTransaction();
session.save(student);
tx.commit();
}
}
4.run as junit即可实现将该student信息插入到表student中