Spring和hibernate的maven配置
本章内容
-
- java编辑器推荐
-
- maven需要下载的包和说明
-
- spring配置文件
-
- hibernate配置
推荐工具
- hibernate配置
java编辑器
推荐使用 intellij IDEA代替 eclipse,eclipse看起来像上个世纪的软件,使用intellij吧,用起来十分的爽,下面的文章中,本文主要使用他去完成内容。
api接口调试
有个浏览器插件叫RESTClient,在chorme和firefox的应用商店里面都能找到他,但是由于google给墙了,所以推荐使用firefox的RESTClient.调试接口神器。
maven包
包的内容包括spring ,hibernate,mysql ,jackson。这个包可以包含了mvc框架,orm数据模型层,数据库操作。jackson用户restapi的json解析。
<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>com.springapp</groupId>
<artifactId>spring1</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>spring1</name>
<properties>
<spring.version>4.1.1.RELEASE</spring.version>
</properties>
<dependencies>
<!--common-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<!--spring RestApi-->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-lgpl</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-lgpl</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.1</version>
</dependency>
<!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.5.Final</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.31</version>
</dependency>
</dependencies>
<build>
<finalName>spring1</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Tests.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>
### srping 配置
这个配置文件有很多地方需要注意的。
- 1
spring4+版本使用的都是MappingJackson2HttpMessageConverter,之前版本是用的时没有2的版本。 - 2 若设置create-drop,那么每次hibernate连接数据库都会把之前的表个删除重建一次。
- 3 json映射类型要注意,否则返回会出现500错误,网上解决这个错误的方法很多,但是不同spring版本对应的能正真解决的办法不是很多。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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">
<context:component-scan base-package="com.springapp.mvc"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</list>
</property>
</bean>
<!-- Hibernate4 -->
<!-- 加载资源文件 其中包含变量信息,必须在Spring配置文件的最前面加载,即第一个加载-->
<!--<context:property-placeholder location="classpath:persistence-mysql.properties" />-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<!-- 可以加多个包 -->
<value>com.springapp</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<!--每次删除并创建-->
<!--<prop key="hibernate.hbm2ddl.auto">create-drop</prop>-->
<!--每次不处理-->
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- <prop key="hibernate.current_session_context_class">thread</prop> -->
</props>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!--<property name="driverClassName" value="${jdbc.driverClassName}" />-->
<!--<property name="url" value="${jdbc.url}" />-->
<!--<property name="username" value="${jdbc.user}" />-->
<!--<property name="password" value="${jdbc.pass}" />-->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
</beans>
### heibernate 配置
获取session的静态方法。 注意:configure(“../hibernate.cfg.xml”); 一般不需要指定路径,默认是在src目录下去找hibernate.cfg.xml文件
package com.springapp.com.springapp.service;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* Created by zteliuyw on 15/5/18.
*/
public class DatabaseHelper {
private static SessionFactory factory;
static
{
try{
//默认读取的是hibernate.cfg.xml 文件.
Configuration cfg = new Configuration().configure("../hibernate.cfg.xml");
//建立SessionFactory.
factory = cfg.buildSessionFactory();
}catch(Exception e )
{
e.printStackTrace();
}
}
static public Session getSession() {
Session session = factory.openSession();
return session;
}
static public Session context() {
return DatabaseHelper.getSession();
}
//返回工厂类.
public static SessionFactory getSessionFactory()
{
return factory;
}
}