An enum class has been added to the JDK1.5 that can be used to map to database tables. And it's very convenient to implement.
Here's an example:
Development environment:
JDK 1.6 U2
Eclipse 3.3
There is a table in the database: Useruser
It has two fields: 1, id;2, username,
We map out an enum:
public enum UserUser {
username,
id;
}
Now we're going to use it to write the SQL statement:
public class UserTest {
public static void main(String [] args)
{
//构建SQL语句
String sql="select * from "+UserUser.class.getSimpleName() +
" where "+UserUser.username.toString()+"=aksdkk";
System.out.println(sql);
}
}
is not very convenient, and if the database table structure has been changed, in these places in JDK 1.5 to implement the real object-oriented form of SQL statements
Some improvements have been made here:
1. Realize idbtable, let system know it is database table mapping class;
2. Implement the ToString () method so that the field changes in the datasheet;
3. If the table has a primary key, you can return the primary key field Getidcol ();
4. If you have a single primary key (not a composite primary key) and are not automatically growing, you can get the method of ID generation by Idgenerator ().
5. Because the INSERT statement of the table is the same throughout the system, I have added a getinsertsql () function in the mapping class to get the Add data function to improve development efficiency and reduce maintenance effort.