spring4 -- web项目指引
2015-07-21 13:14
363 查看
这里采用的是eclipse创建的基础web项目,并在其基础上加入spring支持,并未采用官方的Maven和Gradle项目。
包结构:
其中mysql和c3p0是根据具体情况加的,其他基本是spring需要的基本包了。
要建立spring项目,首先需要在web.xml加入spring的配置:
[/code]
这里设定了spring的主配置文件位于classpath的根目录,项目里与src同级。
spring-mvc.xml,这里主要是基于注解的配置,基于xml的配置不涉及:
这里使用的c3p0的数据源,也可采用其他数据源。
配置文件完成后就可以开始结构性代码的开发了,基于mvc的代码由3部分组成,分别是controller、service、dao,
Controller:
controller以@Controller注解标注,@RequestMapping定义了方法的url访问路径。HttpServletResponse response参数在需要用到response时传入,如果用不到的话不传。@autowired以依赖注入的方式标注了两个引用,service和dao。
Service:
Service以@Service注解标注
Dao:
Dao需要以@Repository标注。
这是一个简单的项目指引,细节性的问题,后面慢慢梳理。
最后附上完整的项目代码:http://pan.baidu.com/s/1kTpBeib
包结构:
其中mysql和c3p0是根据具体情况加的,其他基本是spring需要的基本包了。
要建立spring项目,首先需要在web.xml加入spring的配置:
<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>
[/code]
这里设定了spring的主配置文件位于classpath的根目录,项目里与src同级。
spring-mvc.xml,这里主要是基于注解的配置,基于xml的配置不涉及:
<?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>
这里使用的c3p0的数据源,也可采用其他数据源。
配置文件完成后就可以开始结构性代码的开发了,基于mvc的代码由3部分组成,分别是controller、service、dao,
Controller:
@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; }
controller以@Controller注解标注,@RequestMapping定义了方法的url访问路径。HttpServletResponse response参数在需要用到response时传入,如果用不到的话不传。@autowired以依赖注入的方式标注了两个引用,service和dao。
Service:
@Service public class SpringTestService { public void getUserInfo(){ Map<String, Object> map = dao.getUserInfo("100"); } @Autowired private SpringTestDao dao; }
Service以@Service注解标注
Dao:
@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; } }
Dao需要以@Repository标注。
这是一个简单的项目指引,细节性的问题,后面慢慢梳理。
最后附上完整的项目代码:http://pan.baidu.com/s/1kTpBeib
相关文章推荐
- Struts2.3.24框架入门之一环境配置及入门实例HelloWorld
- SpringMVC拦截器
- spring4 -- 基于jdbcTemplate的分页
- spring4 -- JdbcTemplate数据库操作
- springMVC详细配置+Mysql+jdbcTemplate实现分页功能
- dubbo接口address从diamond获取按spring的BeanPostProcessor代理实现
- java.lang.String小测试
- Spring架构使用JdbcTemplate进行数据库功能开发
- Spring AOP在pointcut expression解析表达式 并匹配多个条件
- Spring中的JDBCTemplate使用(非常详细的配置过程)
- Spring JdbcTemplate 使用总结与经验分享
- spring框架学习(六)AOP
- spring框架学习(五)注解
- spring框架学习(四)自动装配
- spring框架学习(三)junit单元测试
- spring框架学习(二)依赖注入
- spring框架学习(一)
- Spring JdbcTemplate方法详解
- static class 静态类(Java)
- java中string与byte[]的转换