下面我将为你详细讲解“Hibernate的Annotation版Hello world实例”的完整攻略:
1. 构建项目
首先,我们需要构建一个Maven项目,在项目的pom.xml
文件中添加相关依赖:
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.28.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.4.28.Final</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
</dependency>
</dependencies>
2. 创建Java实体类
创建一个Java实体类Student
,并添加注解@Entity
标识它是一个实体类,@Table
指定表名,@Id
标识主键,@GeneratedValue
指定主键生成策略,@Column
指定属性类型和列名。
@Entity
@Table(name = "student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
@Column(name = "age")
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
3. 配置数据源和Hibernate的注解配置文件
在src/main/resources
目录下创建一个名为application.properties
的文件,配置数据源和Hibernate的注解配置信息:
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
hibernate.format_sql=true
hibernate.hbm2ddl.auto=create
4. 创建DAO层代码
需要创建一个接口StudentDao
,定义CRUD操作的接口方法。
public interface StudentDao {
void save(Student student);
List<Student> getAll();
}
并创建一个实现类StudentDaoImpl
,使用@Repository
注解标识它是一个DAO层的实现类,使用@PersistenceContext
注解注入EntityManager
对象。
@Repository
public class StudentDaoImpl implements StudentDao {
@PersistenceContext
private EntityManager entityManager;
@Override
public void save(Student student) {
entityManager.persist(student);
}
@Override
public List<Student> getAll() {
TypedQuery<Student> query = entityManager.createQuery("select s from Student s", Student.class);
return query.getResultList();
}
}
5. 编写测试用例
创建一个测试类StudentDaoImplTest
,测试学生的新增和查询。
@RunWith(SpringRunner.class)
@SpringBootTest
public class StudentDaoImplTest {
@Autowired
private StudentDao studentDao;
@Test
public void testSave() {
Student student = new Student();
student.setName("张三");
student.setAge(18);
studentDao.save(student);
Assert.assertTrue(student.getId() > 0);
}
@Test
public void testGetAll() {
List<Student> studentList = studentDao.getAll();
Assert.assertFalse(studentList.isEmpty());
}
}
6. 运行测试用例
最后运行测试用例,测试结果应该如下:
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.example.demo.dao.StudentDaoImplTest
Hibernate:
create table student (
id integer generated by default as identity (start with 1),
age integer not null,
name varchar(255),
primary key (id)
)
Hibernate:
insert
into
student
(age, name, id)
values
(?, ?, ?)
Hibernate:
select
student0_.id as id1_0_,
student0_.age as age2_0_,
student0_.name as name3_0_
from
student student0_
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.194 s - in com.example.demo.dao.StudentDaoImplTest
[INFO]
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Hibernate的Annotation版Hello world实例 - Python技术站