62 lines
1.6 KiB
Java
62 lines
1.6 KiB
Java
package test;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
import java.lang.reflect.Field;
|
|
import java.sql.SQLException;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import org.junit.Assert;
|
|
import org.junit.Test;
|
|
|
|
import bean.Author;
|
|
import utils.DbUtil;
|
|
|
|
public class DbUtilTest {
|
|
DbUtil db = DbUtil.createDBUtil();
|
|
@Test
|
|
public void testGetModel() throws Exception {
|
|
Map<String, String> fs = db.getModel("bean.Book");
|
|
Assert.assertEquals("price", fs.get("price"));
|
|
Assert.assertEquals("update_time", fs.get("updateTime"));
|
|
}
|
|
|
|
@Test
|
|
public void testAuthor() throws Exception {
|
|
Author t = db.get(Author.class, "select * from author where id = ?",1);
|
|
// assertNotNull(t);
|
|
if(t != null) {
|
|
assertEquals(1, t.getId());
|
|
System.out.println(t);
|
|
}
|
|
List<Author> list = db.queryList(Author.class, "select * from author limit 1");
|
|
assertEquals(1, list.size());
|
|
}
|
|
|
|
@Test
|
|
public void testAddAuthor() throws SQLException {
|
|
int ret = db.save("insert into author (name,create_time,update_time) values (?,now(),now())","lisi");
|
|
assertEquals(1, ret);
|
|
}
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
// testGetModel();
|
|
|
|
Class<Author> cls = Author.class;
|
|
List<Field> fs = new ArrayList<Field>();
|
|
fs.addAll(Arrays.asList(cls.getDeclaredFields()));
|
|
Class<?> parent = cls.getSuperclass();
|
|
if (parent.getName().equalsIgnoreCase("bean.BaseModel")) {
|
|
fs.addAll(Arrays.asList(parent.getDeclaredFields()));
|
|
}
|
|
for (Field f : fs) {
|
|
System.out.println(f.getName());
|
|
}
|
|
}
|
|
}
|