[Spring MVC菜鸟之旅] Spring MVC 与 Hibernate整合


应用结构

先贴上一张本文最终运行成功的Spring MVC程序的结构图

配置项目

使用Maven导入SpringMVC所依赖的包,修改pom.xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<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.nicklaus.springmvc</groupId>
<artifactId>springmvc</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>springmvc Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.3.0.RELEASE</spring.version>
<hibernate.version>4.3.11.Final</hibernate.version>
<mysql.version>5.1.29</mysql.version>
<c3p0.version>0.9.1.2</c3p0.version>
</properties>
<dependencies>
<!-- Spring start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</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-webmvc</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-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring end -->
<!--hibernate-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!--数据库连接池C3P0-->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0.version}</version>
</dependency>
<!-- Mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- jstl for jsp page -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>springmvc</finalName>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.1.v20140609</version>
</plugin>
</plugins>
</build>
</project>

修改WEB-INF下的web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>SpringMVCDemo</display-name>
<!-- 配置spring应用上下文 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置spring核心servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 过滤器 -->
<filter>
<filter-name>encodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

保持之前dispatcher-servlet.xml内容不变

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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:mvc="http://www.springframework.org/schema/mvc"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
<!-- 处理静态资源 -->
<mvc:resources location="resources" mapping="/resources/**"/>
<!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->
<mvc:annotation-driven />
<!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->
<context:component-scan base-package="com.niklaus.springmvc" />
<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

在resource目录下创建applicationContext.xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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: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">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:dev/config.properties</value>
</list>
</property>
</bean>
<import resource="spring-db.xml" />
</beans>

在resource目录下创建spring-db.xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl">
<value>${jdbc.url}</value>
</property>
<property name="user">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
<property name="minPoolSize">
<value>${jdbc.pool.min}</value>
</property>
<property name="maxPoolSize">
<value>${jdbc.pool.max}</value>
</property>
<property name="initialPoolSize">
<value>${jdbc.pool.init}</value>
</property>
<property name="maxStatements">
<value>${jdbc.pool.maxStatements}</value>
</property>
<property name="maxIdleTime">
<value>${jdbc.pool.maxIdleTime}</value>
</property>
<property name="idleConnectionTestPeriod">
<value>${jdbc.pool.idleConnectionTestPeriod}</value>
</property>
<property name="acquireRetryAttempts">
<value>${jdbc.pool.acquireRetryAttempts}</value>
</property>
<property name="breakAfterAcquireFailure">
<value>${jdbc.pool.breakAfterAcquireFailure}</value>
</property>
<property name="testConnectionOnCheckout">
<value>${jdbc.pool.testConnectionOnCheckout}</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.niklaus.springmvc.pojo</value>
</list>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 定义事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 定义方法的过滤规则 -->
<tx:attributes>
<!-- 所有方法都使用事务 -->
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<!-- 定义一个切入点 -->
<aop:pointcut id="services" expression="execution (* com.niklaus.springmvc.service.*Service.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="services" />
</aop:config>
</beans>

将配置数据源的配置信息写入config.properties文件中

1
2
3
4
5
6
7
8
9
10
11
12
13
#mysql configuration
jdbc.url=jdbc\:mysql\://10.1.1.00\:3306/xxxxx?autoReconnect\=true&useUnicode\=true&characterEncoding\=UTF-8
jdbc.username=root
jdbc.password=
jdbc.pool.min=5
jdbc.pool.max=50
jdbc.pool.maxIdleTime=180
jdbc.pool.init=5
jdbc.pool.idleConnectionTestPeriod=1800
jdbc.pool.maxStatements=100
jdbc.pool.acquireRetryAttempts=30
jdbc.pool.breakAfterAcquireFailure=false
jdbc.pool.testConnectionOnCheckout=false

编写数据交互的代码

创建数据表

1
2
3
4
5
6
7
8
9
10
CREATE TABLE `auth_user` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(30) NOT NULL,
`email` VARCHAR(75) NOT NULL,
`password` VARCHAR(128) NOT NULL,
`last_login` DATETIME NOT NULL,
`date_joined` DATETIME NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `username` (`username`)
)

Pojo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.niklaus.springmvc.pojo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
/**
* Created by nicholas.chi on 2016/7/2.
*/
@Entity
@Table(name = "auth_user")
public class Test {
private long id;
private String userName;
private String email;
private String password;
private Date lastLogin;
private Date dateJoined;
@Id
@Column(name = "id", unique = true, nullable = false)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Column(name = "username")
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Column(name = "email")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Column(name = "password")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name = "last_login")
public Date getLastLogin() {
return lastLogin;
}
public void setLastLogin(Date lastLogin) {
this.lastLogin = lastLogin;
}
@Column(name = "date_joined")
public Date getDateJoined() {
return dateJoined;
}
public void setDateJoined(Date dateJoined) {
this.dateJoined = dateJoined;
}
@Override
public String toString() {
return "Test{" +
"id=" + id +
", userName='" + userName + '\'' +
", email='" + email + '\'' +
", password='" + password + '\'' +
", lastLogin=" + lastLogin +
", dateJoined=" + dateJoined +
'}';
}
}

Dao层

接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.niklaus.springmvc.dao;
import com.niklaus.springmvc.pojo.Test;
import java.util.List;
/**
* Created by nicholas.chi on 2016/7/2.
*/
public interface TestDao {
public void testAdd(Test test);
public void testDelete(Test test);
public void testUpdate(Test test);
public Test getTestById(long id);
public List<Test> getAll();
}

实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.niklaus.springmvc.dao.Impl;
import com.niklaus.springmvc.dao.TestDao;
import com.niklaus.springmvc.pojo.Test;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.List;
/**
* Created by nicholas.chi on 2016/7/2.
*/
@Repository
public class TestDaoImpl implements TestDao{
@Resource
HibernateTemplate hibernateTemplate;
public void testAdd(Test test) {
hibernateTemplate.save(test);
}
public void testDelete(Test test) {
hibernateTemplate.delete(test);
}
public void testUpdate(Test test) {
hibernateTemplate.update(test);
}
public Test getTestById(long id) {
Test test = hibernateTemplate.get(Test.class,id);
return test;
}
public List<Test> getAll() {
List<?> list = hibernateTemplate.find("from Test where id > 0");
return (List<Test>)list;
}
}

Service层

接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.niklaus.springmvc.service;
import com.niklaus.springmvc.pojo.Test;
import java.util.List;
/**
* Created by nicholas.chi on 2016/7/2.
*/
public interface TestService {
public List<Test> getAllUser();
public void addUser(Test test);
}

实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.niklaus.springmvc.service.Impl;
import com.niklaus.springmvc.dao.TestDao;
import com.niklaus.springmvc.pojo.Test;
import com.niklaus.springmvc.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Created by nicholas.chi on 2016/7/2.
*/
@Service
public class TestServiceImpl implements TestService{
@Autowired
TestDao testDao;
public List<Test> getAllUser() {
return testDao.getAll();
}
public void addUser(Test test) {
testDao.testAdd(test);
}
}

Controller层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.niklaus.springmvc.controller;
import com.niklaus.springmvc.pojo.Test;
import com.niklaus.springmvc.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.Date;
import java.util.List;
/**
* Created by nicholas.chi on 2016/7/2.
*/
@Controller
public class TestController {
@Autowired
TestService testService;
@RequestMapping(value="/test",method = RequestMethod.GET)
public String test(){
List<Test> list = testService.getAllUser();
for(int i = 0;i<list.size();i++){
System.out.println("Test Num : "+i);
System.out.println("Test content : "+list.get(i).toString());
}
return "index";
}
@RequestMapping(value="/testAdd",method = RequestMethod.GET)
public String testAdd(){
Test test = new Test();
test.setUserName("Tests");
test.setEmail("test@teat.com");
test.setPassword("XXXXXXXXXXXXXXXXXXXXXX");
test.setDateJoined(new Date());
test.setLastLogin(new Date());
testService.addUser(test);
return "index";
}
}

效果验证

在浏览器中访问http://localhost:10086/springmvc/test,会发现IDE的Console会打印如下信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Hibernate: select test0_.id as id1_0_, test0_.date_joined as date_joi2_0_, test0_.email as email3_0_, test0_.last_login as last_log4_0_, test0_.password as password5_0_, test0_.username as username6_0_ from auth_user test0_ where test0_.id>0
Test Num : 0
Test content : Test{id=1, userName='kerry', email='kerry.liu@yuyutechnology.com', password='e10adc3949ba59abbe56e057f20f883e', lastLogin=2016-02-03 14:48:12.0, dateJoined=2016-02-02 18:23:13.0}
Test Num : 1
Test content : Test{id=2, userName='a', email='a@a.com', password='e10adc3949ba59abbe56e057f20f883e', lastLogin=2016-02-03 14:38:22.0, dateJoined=2016-02-03 11:29:23.0}
Test Num : 2
Test content : Test{id=3, userName='b', email='b@b.com', password='e10adc3949ba59abbe56e057f20f883e', lastLogin=2016-02-03 11:48:30.0, dateJoined=2016-02-03 11:48:30.0}
Test Num : 3
Test content : Test{id=4, userName='test', email='test@test.com', password='e10adc3949ba59abbe56e057f20f883e', lastLogin=2016-02-03 12:04:32.0, dateJoined=2016-02-03 12:03:29.0}
Test Num : 4
Test content : Test{id=5, userName='test2', email='test2@test.com', password='e10adc3949ba59abbe56e057f20f883e', lastLogin=2016-02-03 12:07:00.0, dateJoined=2016-02-03 12:07:00.0}
Test Num : 5
Test content : Test{id=6, userName='c', email='c@c.com', password='202cb962ac59075b964b07152d234b70', lastLogin=2016-02-03 12:21:44.0, dateJoined=2016-02-03 12:21:44.0}
Test Num : 6
Test content : Test{id=7, userName='123', email='123@123.com', password='dddd', lastLogin=2016-07-15 18:00:27.0, dateJoined=2016-07-15 18:00:27.0}

由以上的信息可以看出,我们成功连接到了数据库,并且能从中获取我们想要的数据,但是这并不意味这我们将Spring MVC 和Hibernate整合成功了,如果我们此时去访问http://localhost:10086/springmvc/testAdd,就会发现页面提示500错误信息。

其中我们从org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove ‘readOnly’ marker from transaction definition.错误提示中可以知道,当前正处于read-only状态,只允许读操作,不允许写操作。

解决问题

修改dispatcher-servlet.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?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:mvc="http://www.springframework.org/schema/mvc"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
<!-- 处理静态资源 -->
<mvc:resources location="resources" mapping="/resources/**"/>
<!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->
<mvc:annotation-driven />
<!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->
<!--<context:component-scan base-package="com.niklaus.springmvc" />-->
<context:component-scan base-package="com.niklaus.springmvc" use-default-filters="false" >
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

修改applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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: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">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:dev/config.properties</value>
</list>
</property>
</bean>
<context:component-scan base-package="com.niklaus.springmvc" >
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
<import resource="spring-db.xml" />
</beans>

保存好修改内容之后,再次访问 http://localhost:10086/springmvc/testAdd ,发现此时不再报错,然后查看数据库发现确实增加了username为Tests的 一条数据

结语

直到此时我们才将Spring MVC 和 Hibernate框架初步整合,支持增删改查等一系列数据交互的操作。如果需要更加强大的功能,还需要后续的步骤。(其实我想说,你需要力量吗,氪金吧O(∩_∩)O)

参考&引用

更新时间

发布时间 : 2016-07-02

看你的了!