[TOC]
mybatis注解的使用
為什么學(xué)習(xí)注解?學(xué)習(xí)注解有什么好處?學(xué)完能做什么?
1、能夠讀懂別人寫的代碼,特別是框架相關(guān)的代碼
2、讓編程更加簡(jiǎn)潔,代碼更加清晰
注解作用
1、傳遞數(shù)據(jù)
2、標(biāo)記
注解概念
Java 提供一種原程序中的元素關(guān)聯(lián)任何信息和任何元數(shù)據(jù)的途徑和方法
注解分類

1、增刪改查的注解

這些注解中的每一個(gè)代表了執(zhí)行的真實(shí)SQL。它們每一個(gè)都使用字符串?dāng)?shù)組(或單獨(dú)的字符串)。如果傳遞的是字符串?dāng)?shù)組,它們由每個(gè)分隔它們的單獨(dú)空間串聯(lián)起來。這就當(dāng)用Java代碼構(gòu)建SQL 時(shí)避免了“丟失空間”的問題。然而,如果你喜歡,也歡迎你串聯(lián)單獨(dú)的字符串。屬性:value,這是字符串 數(shù)組用來組成單獨(dú)的SQL語句。
@Select("SELECT * FROM user WHERE username=#{username} AND password=#{password}")
User login(User user);
2、映射的注解
// 接口
@ResultMap("dao.GoodMapper.AllGoodResult")
@Select("SELECT * FROM Good WHERE gid=#{value}")
Good selectGoodByGid(int gid);
<!-- xml -->
<!-- 商品表resultMap -->
<resultMap type="Good" id="AllGoodResult">
<id column="gid" property="gid" />
<association property="type" javaType="Type" resultMap="AllTypeResult"></association>
</resultMap>
<!-- 商品類型表resultMap -->
<resultMap type="Type" id="AllTypeResult">
<id column="typeid" property="typeid" />
</resultMap>
// 接口
@Results(value = {
@Result(column = "tid", property= "typeid"),
@Result(column = "tname", property= "typename")
})
@Select("select typeid as tid,typename as tname from type where typeid=#{value}")
Type selectType1(int typeid);
其它、參數(shù)和SQL語句創(chuàng)建器

自定義注解
開發(fā)步驟
1、創(chuàng)建一個(gè)@interface
2、String value();抽象方法可以接收數(shù)據(jù)
3、使用元注解,描述自定義注解
4、使用元注解,描述自定義注解
- @Target指定注解可以添加在哪里
-ElementType.TYPE:可在類和接口上面
-ElementType.METHOD:可在方法上
-ElementType.FIELD:可在屬性上
5、@Retention指定注解在什么時(shí)候用
-RetentionPolicy.RUNTIME:注解保留到運(yùn)行時(shí)
-RetentionPolicy.ClASS:注解保留到Class文件中
-RetentionPolicy.SOURCE:注解保留到j(luò)ava編譯時(shí)期
6、@inherited可以被繼承
代理模式
任意對(duì)象都可以統(tǒng)計(jì)它的運(yùn)行之間
動(dòng)態(tài)代理:
優(yōu)點(diǎn):可以不修改原來對(duì)象的基礎(chǔ)上添加新功能
1、jdk代理
2、cglib
JDK動(dòng)態(tài)代理
1、被代理類必須實(shí)現(xiàn)一個(gè)接口,任意接口
public class Bus implements Runnable{
@Override
public void run() {
System.out.println("bus啟動(dòng)");
}
}
2、創(chuàng)建一個(gè)類實(shí)現(xiàn)invocationHandler,該類用來對(duì)象代理對(duì)象進(jìn)行方法的增強(qiáng)
public class TimeInvocation implements InvocationHandler{
private Object target;//被代理對(duì)象
public TimeInvocation(Object target){
this.target = target;
}
}
3、在invoke()方法中調(diào)用被代理對(duì)象的方法,并添加增強(qiáng)的代碼
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
long time = System.currentTimeMillis();
//被調(diào)用代理的方法
method.invoke(target,args);
long time1 = System.currentTimeMillis();
System.out.println(time1 - time);
return null;
}
4、通過Proxy.newProxyInstance(ClasLoader, Class, InvovationHandler)創(chuàng)建代理對(duì)象
調(diào)用代理對(duì)象的方法
public class ProxyTest {
Bus bus = new Bus();
TimeInvocation invocation = new TimeInvocation(bus);
Class<?> cla = bus.getClass();
Runnable s1= (Runnable)Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), time);
s1.run();
}
mybatis緩存的使用
一級(jí)緩存:
默認(rèn)是開啟狀態(tài)
只要session不關(guān)閉,都會(huì)從數(shù)據(jù)庫中獲取數(shù)據(jù)
同一個(gè)sqlsession,執(zhí)行相同的sql語句先找緩存
二級(jí)緩存:默認(rèn)是關(guān)閉狀態(tài)
特點(diǎn):相同sql找緩存
同一個(gè)namespace下的sqlsession執(zhí)行sql語句先找緩存

mybatis逆向工程
把數(shù)據(jù)庫中的表映射成Java文件