Hibernate的CRUD以及junit测试

1:第一步创建动态工程引包,省略。

2:第二步,创建数据库和数据表,省略。

3:第三步,创建实体类,如User.java,源码如下所示:

  对于实体类,一般实现构造方法,而构造方法一般实现三个:

    一个无参构造方法;

    一个有参的构造方法;

    一个不带id的构造方法;

 1 package com.bie.po;
 2 /** 
 3 * @author BieHongLi 
 4 * @version 创建时间:2017年3月8日 下午5:17:23 
 5 * 
 6 */
 7 public class User {
 8 
 9     private int id;
10     private String name;
11     private String password;
12     private String email;
13     private String phone;
14     public int getId() {
15         return id;
16     }
17     public void setId(int id) {
18         this.id = id;
19     }
20     public String getName() {
21         return name;
22     }
23     public void setName(String name) {
24         this.name = name;
25     }
26     public String getEmail() {
27         return email;
28     }
29     public void setEmail(String email) {
30         this.email = email;
31     }
32     public String getPhone() {
33         return phone;
34     }
35     public void setPhone(String phone) {
36         this.phone = phone;
37     }
38     public String getPassword() {
39         return password;
40     }
41     public void setPassword(String password) {
42         this.password = password;
43     }
44     @Override
45     public String toString() {
46         return "User [> phone
47                 + "]";
48     }
49     public User(String name, String password, String email, String phone) {
50         super();
51         this.name = name;
52         this.password = password;
53         this.email = email;
54         this.phone = phone;
55     }
56     public User(int id, String name, String password, String email, String phone) {
57         super();
58         this.id = id;
59         this.name = name;
60         this.password = password;
61         this.email = email;
62         this.phone = phone;
63     }
64     public User() {
65         super();
66     }
67     
68     
69 }

4:配置数据表和实体类之间的映射,起名如实体类.hbm.xml,源码如下所示:

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC 
 3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5     
 6 <hibernate-mapping package="com.bie.po"> 
 7     <!-- 操作条件:
 8         1:对象与表
 9         2:属性与字段的对应
10         3:类型的对应,类型默认采用属性的类型,type不写的话
11      -->
12     <class name="User" table="user">
13         <!-- 主键,映射 -->
14         <id name="id" column="id">
15             <generator class="native"></generator>
16         </id>
17         
18         <!-- 非主键,映射 -->
19         <property name="name" column="name"></property>
20         <property name="password" column="password"></property>
21         <property name="email" column="email"></property>
22         <property name="phone" column="phone"></property>
23         
24         
25     </class>
26 
27 </hibernate-mapping>

5:完成实体类和数据表之间的配置,就开始配置数据库连接和加载映射,hibernate,cfg.xml

 1 <!DOCTYPE hibernate-configuration PUBLIC
 2     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 3     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 4 
 5 <hibernate-configuration>
 6     <session-factory>
 7         <!-- 
 8             1:数据连接配置
 9             2:加载所有的映射(*.hbm.xml)
10          -->
11          
12          <!-- 1:数据连接配置 -->
13          <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
14          <property name="hibernate.connection.url">jdbc:mysql:///test</property>
15          <property name="hibernate.connection.username">root</property>
16          <property name="hibernate.connection.password">123456</property>
17          <!-- mysql数据库的方言 -->
18          <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
19          
20          <property name="hibernate.show_sql">true</property>
21          
22          <!-- 2:加载所有的映射(*.hbm.xml) -->
23          <mapping resource="com/bie/po/User.hbm.xml"/>
24          
25     </session-factory>
26 </hibernate-configuration>

 6:提取工具类HibernateUtils.java,简化开发:

 1 package com.bie.utils;
 2 
 3 import org.hibernate.Session;
 4 import org.hibernate.SessionFactory;
 5 import org.hibernate.cfg.Configuration;
 6 
 7 /** 
 8 * @author BieHongLi 
 9 * @version 创建时间:2017年3月10日 下午1:47:55 
10 * 创建工具类
11 */
12 public class HibernateUtils {
13 
14     private static SessionFactory sf;
15     static{
16         //加载主配置文件,并且创建session的工厂
17         sf=new Configuration().configure().buildSessionFactory();
18     }
19     
20     //创建session对象
21     public static Session getSession(){
22         return sf.openSession();
23     }
24 }

7:最后完成Dao层的替换,由之前使用的基本的Connection创建连接替换成为session创建连接;

  首先创建接口再实现接口;

  注意:

     更新的时候,索引是从0开始的,不是从1开始的,切记;

 1 package com.bie.dao;
 2 
 3 import java.util.List;
 4 
 5 import com.bie.po.User;
 6 
 7 /** 
 8 * @author BieHongLi 
 9 * @version 创建时间:2017年3月10日 下午1:35:14 
10 * 
11 */
12 public interface UserDao {
13 
14     /***
15      * 用户信息保存的方法
16      * @param user
17      */
18     public void insertUser(User user);
19     
20     
21     /***
22      * 用户信息更改的方法
23      * @param user
24      */
25     public void updateUser(User user);
26     
27     
28     /***
29      * 根据用户的编号用户信息查询的方法
30      * @param id
31      * @return
32      */
33     public User selectUserId(int id);
34     
35     
36     /**
37      * 用户查询所有的信息
38      * @return
39      */
40     public List<User> selectAll();
41     
42     /***
43      * 
44      * @param name
45      * @return
46      */
47     public List<User> selectAll(String name);
48     
49     /***
50      * 分页查询的方法
51      * @param index
52      * @param count
53      * @return
54      */
55     public List<User> selectPage(int index,int count);
56     
57     /***
58      * 删除的方法
59      * @param id
60      */
61     public void deleteUser(int id);
62 }
View Code

相关文章:

  • 2021-07-26
  • 2021-11-04
  • 2021-11-26
猜你喜欢
  • 2021-04-02
  • 2022-12-23
  • 2021-10-15
  • 2022-12-23
  • 2022-01-02
  • 2021-10-03
  • 2022-03-04
相关资源
相似解决方案