手机版
你好,游客 登录 注册
背景:
阅读新闻

Spring MVC 注释 + iBATIS 整合

[日期:2014-01-01] 来源:Linux社区  作者:chenlong [字体:大 中 小]

上面的有几个注释要说明一下:

@Controller:

1:spring mvc 中将 controller  认为是 MVC中的C --控制层

2:规范命名 类名  xxxController

3:如果不基于注解:  该类需要继承  CommandController  或者 其他很多 参见  spring帮助

如果基于注解:在类名前  加上        @controller

4:补充:将类名前加上该注解,当spring启动 或者web服务启动  spring会自动扫描所有包(当然,这个可以设置)

作用:  就是告诉服务器  这个类是MVC中的C    这个类可以接收用户请求  处理用户请求

说白了就是定义界面想Java类中的请求,类似struts中的action。只在controller中,也就是控制层使用。如果用到其他类里面,也能使用,就是太乱了。

@RequestMapping:

定义名称。负责跳转使用的。TestAction/ helloWorld.html /前面的是类里面的,helloword是方法中的。这样跳转方便。

@Autowired:

定义变量,只要在其他的类中,有用注释定义类的,让spring资源管理器处理的,用这个就能获取到。

@RequestMapping(value="helloWorld",method=RequestMethod.POST)

这个在后面中,定义了请求方法。如果是非定义的请求方法,就找不到地方了。

Return返回, 定义好返回的jsp界面以及文件就好了。

Ibatis整合

ApplicationContext-xml配置文件

Ibatis整合:

配置application.xml文件,用来定义数据源。数据库的访问,整体上跟ibatis的方式差不多。

Application.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:mvc="http://www.springframework.org/schema/mvc"
      xmlns:p="http://www.springframework.org/schema/p"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      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/aop
            http://www.springframework.org/schema/aop/spring-aop-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/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">
           
    <tx:annotation-driven transaction-manager="txManager" />
   
<!--    <import resource="com/zy/ywyd/gameoperators/dao/applicationContext-daoImpl.xml"/>  -->
 <!-- mySQL 数据库配置 -->
 <bean id="mysqldataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="preferredTestQuery" value="select 1"/>
    <property name="idleConnectionTestPeriod" value="18000"/> 
    <property name="testConnectionOnCheckout" value="true "/>
    <property name="driverClass" value="com.mysql.jdbc.Driver"></property> 
    <!-- 126测试      --> 
  <property name="jdbcUrl" value="jdbc:mysql://192.168.1.126:3306/databaseName"></property> 
  <property name="user"><value>user</value></property>
    <property name="password"><value>password</value></property>   
 </bean> 
 
 <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocation"><value>classpath:sql-map-config.xml</value> </property>
        <property name="mappingLocations">
        <value>classpath:com/zy/ywyd/gameoperators/dao/*-ibatis.xml</value>
    </property>           
    <property name="dataSource"><ref bean="mysqldataSource" /></property>
    </bean> 
</beans>

linux
相关资讯       iBATIS  Spring MVC 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款