SpringMVC+Hibernate+Junit4+json基本框架近乎0配置

来源:互联网 发布:淘宝代购链接怎么做 编辑:程序博客网 时间:2024/06/08 08:30

From:http://blog.csdn.net/qqhjqs/article/details/46821735
公司是做APP开发的,需要后台来提供接口,于是乎,这个任务就交给我,经过反复的尝试,学习和参考别人的demo,终于搭出自己还算满意的框架,SpringMVC+Sping3+Hibernate4+Junit4,没有使用Maven。下面一步一步的搭建吧! 
首先是选择jar包,我把我用到的jar包上传到了网上,点我下载 
我使用的事Eclipse,创建好webDynamic project之后,把lib直接粘贴到webcontent下就ok了! 
第一步,配置web.xml,一切请求和初始化都是经过web.xml,代码为
 <servlet>    <servlet-name>spring</servlet-name>    <!-- servlet文件配置 -->    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>      <param-name>contextConfigLocation</param-name>      <param-value>                classpath:spring-servlet.xml            </param-value>    </init-param>    <!-- 优先级,正数越小,优先级越高,如果相同,则按顺序加载 -->    <load-on-startup>1</load-on-startup>  </servlet>  <!-- 请求拦截,只要是带".do"的请求,都被拦截到此 -->  <servlet-mapping>    <servlet-name>spring</servlet-name>    <url-pattern>*.do</url-pattern>  </servlet-mapping>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

接下来是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>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

开始spring的配置 
applicationContext.xml

<!-- 扫描注解配置的包 -->    <context:component-scan base-package="com.dw.*" />    <!-- 配置Spring 用于以后制作Web端的页面-->    <bean        class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/view/" />        <property name="suffix" value=".jsp" />    </bean>    <!-- 配置数据源 -->    <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:mysql://localhost:3306/db_dw" />        <property name="user" value="root" />        <property name="password" value="root" />        <!-- 初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default:3 -->        <property name="initialPoolSize" value="1" />        <!-- 连接池中保留的最小连接数 -->        <property name="minPoolSize" value="1" />        <!-- 连接池中保持的最大连接数。Default:15 -->        <property name="maxPoolSize" value="300" />        <!-- 最大空闲时间,60秒内未使用则被丢弃。若为0则永不丢弃。Default:0 -->        <property name="maxIdleTime" value="60" />        <!-- 当连接池中的连接耗尽的时候c3p0一次性同时获取的连接数。Default:3 -->        <property name="acquireIncrement" value="5" />        <!-- 每60秒检查所有连接池中的空闲连接。Default:0 -->        <property name="idleConnectionTestPeriod" value="60" />    </bean>    <bean id="nativeJdbcExtractor"        class="org.springframework.jdbc.support.nativejdbc.C3P0NativeJdbcExtractor">    </bean>    <!-- hibernate4的配置 -->    <bean id="sessionFactory"        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">        <property name="dataSource" ref="dataSource" />        <property name="annotatedClasses">            <list>                <!-- 带有注解的实体类                <value>com.dw.entity.User</value>                <value>com.dw.entity.Test</value>                <value>com.dw.entity.StrTest</value>                 -->                <!-- <value>tv.ssdm.haoxiang.bean.WlBean</value> -->                <!-- <value>tv.ssdm.haoxiang.bean.WmBean</value> -->                <!-- <value>tv.ssdm.haoxiang.bean.WsBean</value> -->                <!-- <value>tv.ssdm.haoxiang.bean.XHSItemBean</value> -->                <!-- <value>tv.ssdm.haoxiang.bean.THItemBean</value> -->            </list>        </property>        <property name="hibernateProperties">            <value>                property name=”dialect”>org.hibernate.dialect.MySQL5Dialect                hibernate.hbm2ddl.auto=update                hibernate.show_sql=false                hibernate.format_sql=true                <!-- %%%%%%%%%%%%%%%%%%%%%%%%%%使用EHCache二级缓存%%%%%%%%%%%%%%%%%%%%%%%%%% -->                hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider                hibernate.cache.use_query_cache=true;                <!-- hibernate4Session -->                hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext            </value>        </property>    </bean>    <!-- 事务配置 -->    <bean id="transactionManager"        class="org.springframework.orm.hibernate4.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory" />    </bean>    <bean id="txManager"        class="org.springframework.orm.hibernate4.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory" />    </bean>    <!-- 使用基于注解方式管理事务 -->    <tx:annotation-driven transaction-manager="txManager" />    <tx:advice id="txAdvice"><!-- 默认引用 transactionManager -->        <tx:attributes>            <!-- 方法名以add开头,必须开启事务 -->            <tx:method name="add*" propagation="REQUIRED" />            <tx:method name="insert*" propagation="REQUIRED" />            <tx:method name="delete*" propagation="REQUIRED" />            <tx:method name="update*" propagation="REQUIRED" />        </tx:attributes>    </tx:advice>    <aop:config>        <!-- com.dw包下 所有名字以I*Service结尾的类中 所有方法 -->        <aop:pointcut expression="execution(* com.dw..*Service.*(..))"            id="allServiceMethod" />        <!-- 为 allServiceMethod 增强 txAdvice -->        <aop:advisor advice-ref="txAdvice" pointcut-ref="allServiceMethod" />    </aop:config>    <!-- spring 依赖注入 -->    <!-- 该 BeanPostProcessor 将自动对标注 @Autowired 的 Bean 进行注入 srping2.5之后 -->    <bean        class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
  • 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
  • 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

这里的service,dao,controller全部使用注解的方式,就是为了减少配置操作 
spring-servlet.xml文件

<!-- 指定使用注解方式配置,配置自动扫描的包名,base-package指定自己应用中控制器所在的包目录<context:component-scan/>         扫描指定的包中的类上的注解,常用的注解有:@Controller 声明Action逐渐@Service声明Service 声明Action组件@Services         声明Service组件@Service("myMovieLister") @Repository 声明Dao组件@Component泛指组件,当不好归类时,@RequestMapping("/menu")请求映射         @Resource用于注入,(j2ee提供)默认按名称装配,@Resource(name ="beanName")@Autowired用于注入,(spring提供的)         默认按类型装配@Transactional(rollbackFor = {Exception.class})事物管理@ResponseBody @scope("prototype")设定成bean的作用域 -->    <!-- 扫描注解配置的包 -->    <context:component-scan base-package="com.dw.controller" />    <!-- 默认的注解映射的支持 -->    <!-- 日期全局转换配置,注册自己实现的DateConverter类 -->    <bean id="conversionService"        class="org.springframework.format.support.FormattingConversionServiceFactoryBean">        <property name="converters">            <list>                <bean class="com.dw.common.DateConverter"></bean>            </list>        </property>    </bean>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

最后就是hibernate.cfg.xml

<hibernate-configuration>    <session-factory>        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>        <property name="connection.url">jdbc:mysql://localhost:3306/db_dw</property>        <property name="connection.username">root</property>        <property name="connection.password">root</property>        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>        <property name="myeclipse.connection.profile">db_dw</property>        <!-- 在控制台显示hibernate运行的sql -->        <property name="show_sql">false</property>        <property name="format_sql">true</property>        <!-- 映射 -->        <!-- 如果数据库里没有数据表,则创建 -->        <!-- create-drop|create|update|validate -->        <property name="hibernate.hbm2ddl.auto">update</property>        <mapping class="com.dw.entity.Test"/>        <mapping class="com.dw.entity.User" />        <mapping class="com.dw.entity.StrTest"/>    </session-factory></hibernate-configuration>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

实体使用注解的方式后,还是要配置mapping,题目中所谓的达不到0配置就是在这里,每添加一个类,就需要在这里mapping一个。但是相比较不适用注解的方式,这样减少了好多操作! 
整个框架的代码结构如下 
这里写图片描述 
Controllser里写了一个测试请求,打印出的事gson格式的数据,因为是为手机端提供接口,所以,gson格式的数据是首选,代码为:

@Controller@RequestMapping(value="/test")public class TestController {    @Autowired    TestService testService;    @Autowired    UserService userService;    @RequestMapping(value="/add")    public String add(HttpServletRequest request, HttpServletResponse response) throws Exception{        response.setContentType("text/html;charset=utf-8");        PrintWriter out = response.getWriter();        int id = 1;        User user = userService.findById(id);        Test test = new Test();        test.setRemark("添加备注");        test.setUser(user);        testService.addTest(test);        out.print("添加用户");        return null;    }    @RequestMapping(value="/list")    public String list(HttpServletRequest request, HttpServletResponse response) throws Exception{        response.setContentType("text/html;charset=utf-8");        PrintWriter out = response.getWriter();        List<Test> test = testService.list();        Gson g = new Gson();        out.print(g.toJson(test));        return null;    }}
  • 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
  • 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

注意链接的格式,。。。。。/test/list.do 
里面还有Junit测试这里写图片描述 
这样无论是为手机端专门编写接口,还是后来在添加web端,这个框架都能应付的来。 
大家如果感兴趣就下载(http://download.csdn.net/detail/qqhjqs/8885205)看看,如果感觉不错,嘿嘿,点个赞!


原创粉丝点击