上面的有几个注释要说明一下:
@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>
