mybatisはどうやってsqlを注釈する簡単な例を実現しますか?
もっと読む
最近はプロジェクトを始めます。楽屋はmybatisで、mybatisを研究しています。
まずmysqlのtestに時計を作ります。user、フィールドid、username、password、データ1,123,123を挿入します。
一つのPOオブジェクトUserPO
自分で書いただけのデモなので、特にネーミングと構造が苦手です。原理を知るといいです。
runに注意するときはmysql-connector-java-51.25.jarが必要です。 testmybatis.zip(8.6 KB) ダウンロード回数:3
最近はプロジェクトを始めます。楽屋はmybatisで、mybatisを研究しています。
public interface SqlInterface {
@MySelect("select * from t_user where id = 1")
public UserPO select();
}
このような実現はとても面白いと思います。そこで自分でなぞらえ似せて書きました。ここでは読者にjavaの注釈、javaの反射、代理、javaのデータベース操作などの要求があります。コードが読めないなら、先に上記のjavaの基礎を知ることができます。まずmysqlのtestに時計を作ります。user、フィールドid、username、password、データ1,123,123を挿入します。
一つのPOオブジェクトUserPO
public class UserPO {
public long id;
public String userName;
public String password;
selectの注釈インターフェースimport java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface MySelect {
public String value();
}
つのsqlのインターフェイスpublic interface SqlInterface {
@MySelect("select * from t_user where id = 1")
public UserPO select();
}
怠け者なので、直接代理とtest類を書きます。public class TestMain {
public static void main(String[] args) {
try {
//
SqlInterface sqlInterface = (SqlInterface) Proxy.newProxyInstance(SqlInterface.class.getClassLoader(),
new Class[] { SqlInterface.class }, new InvocationHandler() {
public Object invoke(Object arg0, Method method, Object[] arg2) throws Throwable {
// TODO Auto-generated method stub
// sql
String sqlString = null;
if (method.getAnnotation(MySelect.class) != null) {
sqlString = method.getAnnotation(MySelect.class).value();
System.out.println(sqlString);
}
String databaseDriver = "com.mysql.jdbc.Driver";
Connection conn = null;
ResultSet rs = null;
try {
Class.forName(databaseDriver);
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8",
"root", "root");
PreparedStatement stmt = conn.prepareStatement(sqlString);
rs = stmt.executeQuery();
} catch (Exception e) {
e.printStackTrace();
}
//
ResultSetMetaData rsmd = rs.getMetaData();
String[] dataMetas = new String[rsmd.getColumnCount()];
int colcount = rsmd.getColumnCount();
for (int i = 1; i <= colcount; i++) {
//dataMetas index 0 , 1
System.out.println(rsmd.getColumnClassName(i));
dataMetas[i-1] = rsmd.getColumnName(i);
}
// , userPO
Object o = method.getReturnType().newInstance();
while (rs.next()) {
Field[] fields = o.getClass().getDeclaredFields();
for (Field field : fields) {
String fieldname = field.getName();
for (String dateMeta : dataMetas) {
if (fieldname.toLowerCase().equals(dateMeta)) {
field.set(o, rs.getObject(dateMeta));
break;
}
}
}
}
try {
rs.close();
conn.close();
} catch (Exception e) {
}
return o;
}
});
UserPO po = sqlInterface.select();
System.out.println(po.getUserName());
} catch (Exception e) {
e.printStackTrace();
}
}
}
ここではまだ多くの問題があります。例えば、帯域パラメータを調べて、戻る対象がnullかListだったら、どう処理しますか?自分で書いただけのデモなので、特にネーミングと構造が苦手です。原理を知るといいです。
runに注意するときはmysql-connector-java-51.25.jarが必要です。