1.首先在pom.xml文件中添加hibernate的依赖,主要看代码中<!-- Hibernate模块依赖 开始-->以下的内容
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MySpringMVC</groupId>
<artifactId>MVC</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>MVC Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernateVersion>5.3.4.Final</hibernateVersion>
<c3p0Version>0.9.5.2</c3p0Version>
<mysqlVersion>5.1.38</mysqlVersion>
<springVersion>5.1.8.RELEASE</springVersion>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
</dependency>
<!-- Hibernate模块依赖 开始-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernateVersion}</version>
</dependency>
<!-- Hibernate模块依赖 结束-->
<!--C3p0 数据库连接池依赖 开始-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0Version}</version>
</dependency>
<!--C3p0 数据库连接池依赖 结束-->
<!--Jdbc 驱动依赖 开始 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysqlVersion}</version>
</dependency>
<!--Jdbc 驱动依赖 结束 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${springVersion}</version>
</dependency>
</dependencies>
<build>
<finalName>MVC</finalName>
</build>
</project>
注意:springVersion可以直接在version标签中定义,也可以在properties标签中全局定义。
2. 新建一个spring-db.xml,在之前的servlet中import。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- *** 数据库配置信息 *** -->
<!-- <context:property-placeholder location="classpath:/db.properties" />-->
<context:property-placeholder location="/WEB-INF/db.properties" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!--
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="20" />
<property name="initialPoolSize" value="12" />
<property name="checkoutTimeout" value="300"/>
<property name="maxIdleTime" value="60" />
<property name="acquireIncrement" value="5" />
<property name="idleConnectionTestPeriod" value="10" />
<property name="testConnectionOnCheckin" value="true" />
-->
</bean>
<!-- === dataSource end === -->
<!-- *** sessionFactory start *** -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 注解包 <property name="packagesToScan"> -->
<!-- entity模型注解 -->
<property name="annotatedClasses">
<list>
<value>com.springmvc.model.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
</props>
</property>
</bean>
<!-- === sessionFactory end === -->
<!-- 数据库事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
注意:(1) 配置数据库有三种方式,DriverManagerDataSource建立连接是只要有连接就新建一个connection,根本没有连接池的 作用
(2)BasicDataSource与C3P0都使用了连接池,其中C3P0的配置属性要更加丰富
3.创建db.properties的数据库配置文件
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=123456
配置文件完成配置后,在dao层与model层添加注解
4.添加@Entity, @Table, @Id, @Column等注解,使java的模型可以映射到数据库中
package com.springmvc.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="user")
public class User {
@Id
@Column(name = "name")
private String name = null;
@Column(name = "password")
private String password = null;
@Column(name = "age")
private String age = null;
@Column(name = "sex")
private String sex = null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
5.在dao层中添加获取数据库信息的方法,注释部分为hql,两种方法都可以实现需求
package com.springmvc.dao;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.query.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.springmvc.model.User;
@Repository
public class LoginDao {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Transactional
public User getUserInfo() {
String hql = "from User where name='John'";
Query<User> query = sessionFactory.getCurrentSession().createQuery(hql);
List<User> list = query.getResultList();
// String hql = "select * from User where name=? ";
// Query query = sessionFactory.getCurrentSession().createSQLQuery(hql).addEntity(User.class);
// query.setParameter(1, "John");
// List<User> list = query.list();
User user = list.get(0);
return user;
}
}