SSM復習要點
Spring
Spring是一個管理Bean的容器
三個重要概念:IOC,DI,AOP
bean標簽的作用及特點
這個對象在Spring容器加載時即創(chuàng)建該對象
默認是單例的,可以修改scope屬性修改為非單例
scope="prototype"
class是必須指定的,id不是
Autowired注解的作用
可以注入一個對象冒签,默認是按照類型匹配菱鸥,所以要求實現(xiàn)類只能有一個奸笤,如果有多個需要使用@Qulifier
public class GuanYu{
@Autowired
@Qulifier("test2") // 指定test2作為裝配對象
private Weapon weapon;
}
@Component("test1")
public class QingLong implements Weapon {
private int dps;
public void setDps(int dps) {
this.dps = dps;
}
public int getDps() {
return dps;
}
public void showInfo() {
System.out.println("青龍偃月赞庶,攻擊力很高");
}
}
@Component("test2")
public class Fang implements Weapon {
private int dps;
public void setDps(int dps) {
this.dps = dps;
}
public int getDps() {
return dps;
}
public void showInfo() {
System.out.println("方天畫戟,攻擊力很高");
}
}
依賴注入DI的方式有幾種
-
構造器注入
<bean id="guanyu" class="com.neuedu.test.GuanYu"> <constructor-arg name="name" value="關羽"></constructor-arg> <constructor-arg name="weapon" ref="fang"></constructor-arg> <constructor-arg name="name"> <value>關羽</value> </constructor-arg> <constructor-arg name="weapon"> <ref bean="fang"></ref> </constructor-arg> </bean> <bean id="fang" class="com.neuedu.test.Fang"></bean>
-
setter注入
<bean id="guanyu" class="com.neuedu.test.GuanYu"> <property name="name" value="關羽"></property> <property name="weapon" ref="fang"></property> <property name="name"> <value>關羽</value> </property> <property name="weapon"> <ref bean="fang"></ref> </property> </bean> <bean id="fang" class="com.neuedu.test.Fang"></bean>
- 接口注入(Spring不支持)
AOP面向切面編程
其中使用了代理模式
提供了一些注解筹裕,如@Before,@Around,@AfterThrowing等
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 數(shù)據(jù)源dataSource在spring-db.xml中已經(jīng)配置-->
<property name="dataSource" ref="dbcpDS" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!--
第一個 * - 通配 返回值類型
第二個 * - 通配包com.neuedu.service.impl下的class
第三個 * - 通配包com.neuedu.service.impl下的class的方法
第四個 .. - 通配 方法可以有0個或多個參數(shù)
-->
<!-- advice-ref的值是上面tx:advice標簽的id屬性的值 -->
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.neuedu.service.impl.*.*(..))"/>
</aop:config>
Mybatis
mybatis是一個持久層框架
別名設置
<configuration>
<typeAlias type="com.neuedu.pojo.Userinfo1" alias="user1">
<typeAlias type="com.neuedu.pojo.Userinfo2" alias="user2">
<package name="com.neuedu.pojo"></package>
<package name="com.neuedu.utils"></package>
</configuration>
log4j日志较鼓,默認的輸出級別是DEBUG
表與表之間的關系
- 一對一
<assosiation>
- 多對一
<collection>
- 多對多
<collection>
在mapper文件中椎木,常見的標簽select,insert,where,if,set,update,delete,sql,include等,沒有group標簽
<!-- 返回主鍵 -->
<insert id="addEmp" parameterType="Emp">
/*
keyProperty="empno" 主鍵列
mysql order=before oracle order=after
*/
insert into emp(empno,ename,deptno,job) values(#{empno},#{ename},#{deptno},#{job})
<selectKey order="BEFORE" keyProperty="empno" resultType="Integer"></selectKey>
</insert>
mapper文件中namespace對應接口
<mapper namespace="com.neuedu.mapper.UserinfoMapper"></mapper>
mapper文件中#{}和${}的區(qū)別
- #{}會被解析成博烂?香椎,然后使用數(shù)據(jù)綁定,${}是直接把數(shù)據(jù)拼在SQL中
- #{}有單引號禽篱,${}沒有
mapper文件中resultType和resultMap的區(qū)別
-
resultType表示SQL的返回值類型究流,會把查詢結果直接綁定在該類型的同名屬性上
<select id="serchByEmpno" parameterType="Integer" resultType="Emp"> select * from emp where empno = #{value} </select>
public class Emp{ private int empno; private double sal; ... }
-
resultMap表示SQL返回的map映射爵川,需要一一設置對應屬性
<select id="serchByEmpno" parameterType="Integer" resultMap="test"> select empno,sal from emp where empno = #{value} </select> <resultMap id="test" type="Emp"> <id property="a" column="empno"></id> <result property="b" column="sal"></result> </resultMap>
public class Emp{ private int a; private double b; }
SpringMVC
SpringMVC中有個負責處理HTTP請求和響應的DispatcherServlet,它叫中央控制器(前端控制器)曹抬,在web中配置映射
<!--配置springmvc中央控制器(前端控制器)DispatcherServlet-->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
SpringMVC處理器方法需要返回數(shù)據(jù)需要添加注解:@ResponseBody
SpringMVC中,ModelAndView可以保存數(shù)據(jù)并完成轉發(fā)万矾,但不能獲取數(shù)據(jù)
@RequestMapping({"/user/add","/user/insert"})映射多個請求
參數(shù)綁定時不能直接綁定java.util.Date類型
處理器方法是用String類型作為返回值,使用forward
進行轉發(fā)慎框,使用redirect
進行重定向
RESTFUL風格的URL如何獲取參數(shù)
@Controller
public class TestController{
@RequestMapping("/user/{id}")
public void getInfo(@PathVariable("id") int uid){
}
}