核心是让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
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 }