spring入門(五) spring mvc+hibernate

核心是讓SessionFactory由Spring管理html

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:Customerjava

 以上見  IntelliJ IDEA使用hibernatemysql

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 }
相關文章
相關標籤/搜索