具体配置参数:
Spring: spring-framework-4.2.2
Hibernate: hibernate-release-4.2.21.Final
Eclipse : eclipse MARS.2
MySQL : mysql 5.5 +Navicat Premiumd视图器
System: win 8.1
Spring-framework下载(附地址)
需要下载的文件前面两个就够了,包和参考文档
-
spring-framework-4.2.2.RELEASE-dist.zip 包
-
spring-framework-4.2.2.RELEASE-docs.zip 文档
-
spring-framework-4.2.2.RELEASE-schema.zip 配置
导入SSH框架整合所需的jar包
- Spring的jar包
- Hibernate(附地址)的jar包
- 第三方jar包(日志包)
- 数据库的jar包
配置web.xml
手动创建一个config文件夹用与存放配置的文件,这里方便说明记为cf
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:xx.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
其中xx.xml文件是需要你在cf中手动创建的配置文件,里面具体内容后面配置,这里相当于是告诉系统文件在哪,这里为了说明方便命名为spring.xml
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:xxx.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
这里的xxx.xml同上,可自己命名,这里命名为springMVC.xml
其中filter-class里的名字不用记,可以通过ctrl+shift+T输入
CharacterEncodingFilter获得,且这一步需放在所有过滤器最前面,才有效果
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
其中filter-name里的类名可以通过ctrl+shift+T输入HiddenHttpMethodFilter获得,由于浏览器form表单只支持GET与POST请求,而DELETE、PUT等method并不支持,Spring3.0添加了一个过滤器,可以将这些请求转换为标准的http方法,使得支持GET、POST、PUT与DELETE请求,该过滤器为HiddenHttpMethodFilter
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
-
配置SpringMVC
-
导入命名空间
打开我们自己定义的配置文件springMVC.xml选择Namespace,勾选context和MVC -
<context:component-scan base-package="com.jikexueyuancrm" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" /> </context:component-scan>
-
其中class类名可以通过ctrl+shift+T输入InternalResourceViewResolver获得
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean>
-
<mvc:default-servlet-handler />
-
<mvc:annotation-driven />
-
配置Spring
-
导入命名空间
打开我们自己定义的配置文件spring.xml选择Namespace,勾选context(上下文)和tx(事物) -
<context:component-scan base-package="com.jikexueyuancrm" use-default-filters="false"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" /> </context:component-scan>
-
配置数据库
<context:property-placeholder location="classpath:db.properties" /> -
数据库内容
jdbc.user=root
jdbc.passowrd=
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///jianlam
这里用到cp30的包,C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展。
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource"> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.passowrd}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> </bean>
需要orm包-Object Relational Mapping 对象关系映射,POJO(Plain Ordinary Java Object)简单的Java对象,实际就是普通JavaBeans
<bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" id="sessionFactory"> <!-- 配置数据源 --> <property name="dataSource" ref="dataSource"></property> <!-- 找到实体包(pojo) --> <property name="namingStrategy"> <bean class="org.hibernate.cfg.ImprovedNamingStrategy"></bean> </property> <property name="packagesToScan" value="com.jianlam.entity"></property>
-
<property name="hibernateProperties"> <props> <!-- 数据库的方言 --> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean>
-
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean>
-
测试
创建好实体类和数据库表,查看连接操作数据库是否能正常运作,若没有问题则配置成功!
private spring ctx = null;
@Test
public void testDataSource() throws SQLException {
//检查spring配置
ctx = new ClassPathXmlApplicationContext("spring.xml");
// System.out.println(ctx);
//检查数据库连接
DataSource dataSource = ctx.getBean(DataSource.class);
// System.out.println(dataSource.getConnection().toString());
//检查hibernate配置
SessionFactory sessionFactory = ctx.getBean(SessionFactory.class);
System.out.println(sessionFactory);
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
//测试数据库
Users user = new Users ("jianlam", "123456");
session.save(user);
tx.commit();
session.close();
}
如果你的mysql有问题或对于MVC模式下所需的包分类有所困惑可参考:这里
网友评论
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Users {
@ID
@column(name = "id", unique = false, nullable = false)
private int id;
private String name;
private String password;
public Users(String name, String password) {
super();
this.name = name;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
}
再次感谢!
需要maven版的,跟我要Q:190642942
应该是这个,我刚刚调试出来了,数据库可以插入数据。谢谢楼主!
@test
public void testDataSource() throws SQLException {
//检查spring配置
ctx = new ClassPathXmlApplicationContext("spring.xml");
这句没错?我是看不懂!
为什么发完了,有错,不修改???