搭建一个java web项目 主要使用spring 表现层用springMVC 持久化层使用hibernate
IDE主要使用的是MyEclipse并配置好Tomcat。
启动IDE,首先新建一个Web Project,紧接着修改WebRoot根目录下WEB-INF 文件夹下的配置文件Web.xml
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <!-- 设置Spring容器加载配置文件路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 加载Spring容器配置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置Spring核心控制器 --> <servlet> <servlet-name>servlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>servlet</servlet-name> <url-pattern>*.do</url-pattern> <!-- 过滤地址类型 --> </servlet-mapping> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app>
因为我们在web.xml里配置了一个叫servlet的核心控制器 所以我们需要在web.xml同级目录下给这个核心控制器编写配置文件,配置文件名就是你的<servlet-name>标签里的名字+‘-servlet’,比如上文的web.xml文件<servlet-name>是‘servlet‘,那这个配置文件的名字就叫servlet-servlet.xml。
当然,你也可以通过向核心控制器的<servlet>标签内部添加标签来指定你的配置文件名称和路径。例如
<servlet> <servlet-name>servlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
这样你的核心控制器指向的就是src根目录下的servlet.xml文件了。
servlet-servlet.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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.project" use-default-filters="false"> <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/> </context:component-scan> <!-- 默认配置 --> <bean id="httpRequestAdapter" class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="order" value="1"/> </bean> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter"/><!-- json转换器 --> </list> </property> </bean> <mvc:interceptors> <!-- 多个拦截器,顺序执行 --> <mvc:interceptor> <mvc:mapping path="/*" /> <!-- 如果不配置或/*,将拦截所有的Controller --> <bean class="com.project1.SystemInterceptor.LoginInterceptor"></bean> </mvc:interceptor> </mvc:interceptors> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/> </beans>
紧接着是最主要的 Spring的配置文件 由于web.xml的配置信息写的是classpath:applicationContext.xml 因此spring的配置文件放在src同级目录下
applicationContext.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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-lazy-init="true"> <!-- 数据源配置 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://192.168.1.3:3306/WeChatDB?useUnicode=true&characterEncoding=utf8"/> <property name="username" value="root"/> <property name="password" value="gaopeng"/> <!-- <property name="alias" value="Pool_dbname"/> <property name="prototypeCount" value="0"/> <property name="maximumConnectionCount" value="50"/> <property name="minimumConnectionCount" value="2"/> <property name="simultaneousBuildThrottle" value="5000"/> <property name="houseKeepingTestSql" value="select sysdate from dual"/> --> </bean> <!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 --> <context:component-scan base-package="com.project"/> <!--配置hibernate框架--> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="packagesToScan" value="com.project.pojo" /> <!-- 配置的hibernate实体类的包的地址 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean> <!-- 使用annotation定义事务 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> <!--配置事务管理器,注意这里的dataSource和SqlSessionFactoryBean的dataSource要一致,不然事务就没有作用了--> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 配置jdbcTemplate --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"><ref bean="dataSource" /></property> </bean> <!-- 上传文件 转换 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- set the file size100MB --> <property name="maxUploadSize"> <value>104857600</value> </property> <property name="maxInMemorySize"> <value>4096</value> </property> </bean> </beans>
以上就是项目配置文件的配置
下面是项目三层结构的类的事例(控制层,服务层和持久化层)
Controller.java
package com.project.controller; import java.util.List; 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.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.project.pojo.UserPo; import com.project.service.Service; @Controller @RequestMapping("/Controller") public class Controller { @Autowired private Service service; @RequestMapping("/getUser2.do") @ResponseBody public String getUserList2(@RequestParam(value = "key1")String key1, @RequestParam(value = "key2")String key2){ System.out.println("key1"+key1+" key2"+key2); return "success"; } }
Service.java
package com.project.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.project.dao.Dao; import com.project.pojo.UserPo; @Service public class Service { @Autowired private Dao dao; @Transactional public List<UserPo> getUserList() { return dao.getUserList(); } }
Dao.java
package com.project.dao; import java.util.List; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.stereotype.Repository; import com.project1base.BaseDao; import com.project.pojo.UserPo; @Repository public class Dao extends BaseDao{ @SuppressWarnings("unchecked") public List<UserPo> getUserList() { return getHibernateTemplate().find("from UserPo where wuid > 0"); } }
Dao.java的父类BaseDao.java
package com.project.base; import java.util.List; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.orm.hibernate3.HibernateTemplate; public class BaseDao { private HibernateTemplate hibernateTemplate; private JdbcTemplate jdbcTemplate; @Autowired public void setSessionFactory(SessionFactory sessionFactory) { hibernateTemplate = new HibernateTemplate(sessionFactory); } @Autowired public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } protected HibernateTemplate getHibernateTemplate() { return this.hibernateTemplate; } protected JdbcTemplate getJdbcTemplate() { return this.jdbcTemplate; } /** * hql查询单条数据 * @param hql * @return */ protected Object findObjectH(String hql) { List<?> list = hibernateTemplate.find(hql); if(list.size() != 1) return null; else return list.get(0); } /** * hql查询单条数据 * @param hql * @return */ protected Object findObjectH(String hql,Object... paramArray) { List<?> list = hibernateTemplate.find(hql,paramArray); if(list.size() != 1) return null; else return list.get(0); } }
实体类User 采用注释方式,免去了编写xx.cfg.xml配置文件的工作
@JsonAutoDetect @Entity @Table(name="wuser") public class UserPo{ private int id; private String wuser; private String wupwd; @Id @GeneratedValue @Column(name="wuid") public int getId() { return id; } public void setId(int id) { this.id = id; } @Column(name="wuuse", nullable=false) public String getWuser() { return wuser; } public void setWuser(String wuser) { this.wuser = wuser; } @Column(name="wupwd", nullable=false) public String getWupwd() { return wupwd; } public void setWupwd(String wupwd) { this.wupwd = wupwd; } }
时间: 2024-10-13 00:24:54