欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring入门(五) spring mvc+hibernate

程序员文章站 2022-04-06 12:01:19
核心是让SessionFactory由Spring管理 1.引入依赖 2.配置 springmvc-config.xml 重点是 dataSource和sessionFactory. 3.建立 *.hbm.xml和实体类Customer mapping:Customer.hbm.xml,model: ......

核心是让sessionfactory由spring管理

1.引入依赖

 1 <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
 2 <dependency>
 3     <groupid>org.springframework</groupid>
 4     <artifactid>spring-orm</artifactid>
 5     <version>5.0.9.release</version>
 6 </dependency>
 7  <dependency>
 8     <groupid>org.hibernate</groupid>
 9     <artifactid>hibernate-core</artifactid>
10     <version>5.3.2.final</version>
11 </dependency>
12 <dependency>
13     <groupid>mysql</groupid>
14     <artifactid>mysql-connector-java</artifactid>
15     <version>5.1.4</version>
16 </dependency>
17 <dependency>
18     <groupid>org.hibernate</groupid>
19     <artifactid>hibernate-c3p0</artifactid>
20     <version>5.3.2.final</version>
21 </dependency>

2.配置 springmvc-config.xml  重点是 datasource和sessionfactory.

 1 <bean name="datasource" class="com.mchange.v2.c3p0.combopooleddatasource">
 2     <property name="jdbcurl" value="jdbc:mysql://localhost:3306/test"></property>
 3     <property name="driverclass" value="com.mysql.jdbc.driver"></property>
 4     <property name="user" value="root"></property>
 5     <property name="password" value="123456"></property>
 6 </bean>
 7 <bean name="sessionfactory" class="org.springframework.orm.hibernate5.localsessionfactorybean">
 8     <property name="datasource" ref="datasource"></property>
 9     <property name="mappingdirectorylocations" value="classpath:mapping"></property>
10     <property name="hibernateproperties">
11         <props>
12             <prop key="hibernate.dialect">org.hibernate.dialect.mysqldialect</prop>
13             <prop key="hibernate.c3p0.min_size">5</prop> <!--在连接池中可用数据库连接的最小数目-->
14             <prop key="hibernate.c3p0.max_size">30</prop> <!--在连接池中所有数据库连接的最大数目-->
15             <prop key="hibernate.c3p0.time_out">1800</prop> <!--设定数据库连接的超时时间-->
16             <prop key="hibernate.c3p0.max_statement">50</prop> <!--可以被缓存的preparedstatement的最大数目-->
17             <prop key="hibernate.show_sql">true</prop>
18         </props>
19     </property>
20 </bean>

3.建立 *.hbm.xml和实体类customer

mapping:customer.hbm.xml,
model:customer

 以上见  intellij idea使用hibernate

4.测试

 1 package com.ice.service;
 2 
 3 import com.ice.model.customer;
 4 import org.hibernate.session;
 5 import org.hibernate.sessionfactory;
 6 import org.hibernate.transaction;
 7 import org.springframework.stereotype.component;
 8 
 9 import javax.annotation.resource;
10 
11 @component
12 public class customerservice {
13 
14     @resource
15     private sessionfactory sessionfactory;
16     public void save(customer customer){
17         session  session=sessionfactory.opensession();
18         //可以根据情况决定是否用事务
19         //transaction tx = session.begintransaction();
20         session.save(customer);
21         //tx.commit();
22     }
23 }

 

 1 package com.ice.controller;
 2 
 3 import com.ice.model.customer;
 4 import com.ice.service.customerservice;
 5 import org.springframework.beans.factory.annotation.autowired;
 6 import org.springframework.stereotype.controller;
 7 import org.springframework.web.bind.annotation.requestmapping;
 8 import org.springframework.web.bind.annotation.responsebody;
 9 
10 @requestmapping("/")
11 @controller
12 public class homecontroller {
13     @autowired
14     private customerservice customerservice;
15 
16     @requestmapping("/")
17     @responsebody
18     public string index(){
19         customer customer = new customer();
20         customer.setid(5);
21         customer.setname("hello!");
22         customerservice.save(customer);
23         return "ok";
24     }
25 }