基于注解的实现SpringMVC+MySQL(2)

1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee " version="3.1"> 3 <display-name>mydb2</display-name> 4 <welcome-file-list> 5 <welcome-file>welcome.html</welcome-file> 6 </welcome-file-list> 7 8 <!-- 这里是一个总控制器 --> 9 <servlet> 10 <servlet-name>spring</servlet-name> 11 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 12 </servlet> 13 <servlet-mapping> 14 <servlet-name>spring</servlet-name> 15 <url-pattern>*.do</url-pattern> 16 </servlet-mapping> 17 18 <!-- 解决POST提交乱码问题 --> 19 <filter> 20 <filter-name>EncodingName</filter-name> 21 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 22 <init-param> 23 <param-name>encoding</param-name> 24 <param-value>utf-8</param-value> 25 </init-param> 26 </filter> 27 <filter-mapping> 28 <filter-name>EncodingName</filter-name> 29 <url-pattern>/*</url-pattern> 30 </filter-mapping> 31 32 </web-app>

web.xml

spring-servlet.xml类

1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation="http://www.springframework.org/schema/mvc 7 http:// 8 http:// "> 9 10 11 <mvc:annotation-driven/> 12 13 <context:component-scan base-package="com.spring"></context:component-scan> 14 15 <!-- 获取properties配置文件 --> 16 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 17 <property> 18 <list> 19 <value>classpath:db-config.properties</value> 20 </list> 21 </property> 22 </bean> 23 24 <!-- 获取数据源 --> 25 <bean class="org.apache.commons.dbcp.BasicDataSource"> 26 <property> 27 <value>${db.dirverClass}</value> 28 </property> 29 <property> 30 <value>${db.url}</value> 31 </property> 32 <property> 33 <value>${db.username}</value> 34 </property> 35 <property> 36 <value>${db.password}</value> 37 </property> 38 </bean> 39 40 <!-- 41 给jdbc模板注入数据源 42 在JdbcTemplate里有一个对应的私有属性dataSource 43 --> 44 <bean class="org.springframework.jdbc.core.JdbcTemplate"> 45 <property ref="dataSource"></property> 46 </bean> 47 48 <!-- 49 给userDao(负责和数据库打交道)注入模板 50 在com.spring.db.UserDao里应该设有一个JdbcTemplate jdbcTemplate的私有属性,并且setter 51 --> 52 <bean class="com.spring.db.UserDao"> 53 <property ref="jdbcTemplate"></property> 54 </bean> 55 56 <!--定义视图 通过internalResourceView来表示 使用的是Servlet/jsp技术--> 57 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 58 <property> 59 <value>org.springframework.web.servlet.view.InternalResourceView</value> 60 </property> 61 <!--jsp存放的目录--> 62 <property> 63 <value>/view/</value> 64 </property> 65 <!--jsp文件的后缀--> 66 <property> 67 <value>.jsp</value> 68 </property> 69 </bean> 70 </beans>

spring-servlet.xml

db-config.properties类

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/9cedc346fd0b3a9529323b26767eadd1.html