这里采用的是eclipse创建的基础web项目,并在其基础上加入spring支持,并未采用官方的Maven和Gradle项目。
包结构:![spring4(一) -- web项目指引 - Hfei - Ghost的寧靜家園](http://img1.ph.126.net/CnFuGYT52haKtjnioe80yw==/6597930684263126254.png)
要建立spring项目,首先需要在web.xml加入spring的配置:
这里设定了spring的主配置文件位于classpath的根目录,项目里与src同级。<filter> <filter-name>charsetFilter</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>charsetFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
这里使用的c3p0的数据源,也可采用其他数据源。<?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:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 启用注解,该项可不要 --> <context:annotation-config /> <!-- 扫描该包下的类注解 --> <context:component-scan base-package="com.spring"/>
<!-- 属性文件读入 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath*:jdbc.properties</value> </list> </property>
</bean>
<!-- jdbc数据源 --> <!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${c3p0.driverClass}" /> <property name="url" value="${c3p0.url}" /> <property name="username" value="root" /> <property name="password" value="db#123" /> </bean> --> <!-- c3p0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${c3p0.driverClass}"></property> <property name="jdbcUrl" value="${c3p0.url}"></property> <property name="user" value="${c3p0.user}"></property> <property name="password" value="${c3p0.password}"></property> <property name="acquireIncrement" value="${c3p0.acquireIncrement}"></property> <property name="initialPoolSize" value="${c3p0.initialPoolSize}"></property> <property name="maxIdleTime" value="${c3p0.maxIdleTime}"></property> <property name="maxPoolSize" value="${c3p0.maxPoolSize}"></property> <property name="minPoolSize" value="${c3p0.minPoolSize}"></property> <property name="acquireRetryDelay" value="1000"></property> <property name="acquireRetryAttempts" value="60"></property> <property name="breakAfterAcquireFailure" value="false"></property>
</bean>
<!-- 事务配置 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/>
</bean> <tx:annotation-driven transaction-manager="txManager"/> </beans>
controller以@Controller注解标注,@RequestMapping定义了方法的url访问路径。HttpServletResponse response参数在需要用到response时传入,如果用不到的话不传。@autowired以依赖注入的方式标注了两个引用,service和dao。@Controller public class SpringTestController { @RequestMapping("/show") public void showMsg(HttpServletResponse response){ mng.getUserInfo(); //dao.getUserInfo("100"); try { response.getWriter().write("11111"); } catch (IOException e) { e.printStackTrace(); } } @Autowired private SpringTestService mng; @Autowired private SpringTestDao dao; }
Service以@Service注解标注@Service public class SpringTestService { public void getUserInfo(){ Map<String, Object> map = dao.getUserInfo("100"); } @Autowired private SpringTestDao dao; }
Dao需要以@Repository标注。@Repository public class SpringTestDao { private JdbcTemplate sjc; @Autowired public void setDataSource(DataSource dataSource) { this.sjc = new JdbcTemplate(dataSource); } public Map<String, Object> getUserInfo(String userNo){ String sql = "select * from vyuan_user where userno = ?"; final Object[] params = new Object[] { userNo }; Map<String, Object> map = new HashMap<String, Object>(); map = sjc.queryForMap(sql, params); /*sjc.query(sql, params, new RowCallbackHandler() { @Override public void processRow(ResultSet rs) throws SQLException { //map.put("cno", rs.getInt("cno") + ""); map.put("cno", rs.getString("uname")); // map.put("cno", rs.getDouble("score")); } });*/ System.out.println(map.get("uname")); return map; } }