@Override public void init(NutConfig config) { Ioc ioc = config.getIoc(); Dao dao = ioc.get(Dao.class); // 若必要的数据表不存在,则初始化数据库 if (!dao.exists(User.class)) { dao.create(User.class, true); dao.create(Role.class, true); dao.create(Permission.class, true); FileSqlManager fm = new FileSqlManager("init_system_h2.sql"); List<Sql> sqlList = fm.createCombo(fm.keys()); dao.execute(sqlList.toArray(new Sql[sqlList.size()])); // 初始化用户密码(全部都是123)及salt List<User> userList = dao.query(User.class, null); for (User user : userList) { RandomNumberGenerator rng = new SecureRandomNumberGenerator(); String salt = rng.nextBytes().toBase64(); String hashedPasswordBase64 = new Sha256Hash("123", salt, 1024).toBase64(); user.setSalt(salt); user.setPassword(hashedPasswordBase64); dao.update(user); } } // 设置 Shiro 的 securityManager SecurityManager securityManager = ioc.get(SecurityManager.class); SecurityUtils.setSecurityManager(securityManager); }
@Test public void test() { Ioc ioc = new NutIoc(new JsonLoader("ioc.js")); Dao dao = ioc.get(NutDao.class, "rDao"); dao.create(Teacher.class, true); TeacherAction action = ioc.get(TeacherAction.class); action.add("20092009", "张三", "123456", 1L, "*****@*****.**", "13333333333", Teacher.ROLE_ADMIN); action.add( "20102010", "李四", "123456", 1L, "*****@*****.**", "13333333333", Teacher.ROLE_NORMAL); action.add( "20132013", "王五", "123456", 2L, "*****@*****.**", "13333333333", Teacher.ROLE_ADMIN); action.mod("20092009", "张三", "123456", 1L, "*****@*****.**", "13333333333", Teacher.ROLE_ADMIN); action.mod( "20102010", "李四", "123456", 1L, "*****@*****.**", "13333333333", Teacher.ROLE_NORMAL); action.mod( "20132013", "王五", "123456", 2L, "*****@*****.**", "13333333333", Teacher.ROLE_ADMIN); System.out.println(Json.toJson(action.list(1L, "", 1, 10))); System.out.println(Json.toJson(action.list(1L, "张", 1, 10))); System.out.println(Json.toJson(action.list(1L, "10", 1, 10))); // System.out.println(Json.toJson(action.login("20092009","123456"))); // System.out.println(Json.toJson(action.login("*****@*****.**","123456"))); // System.out.println(Json.toJson(action.login("*****@*****.**","12346"))); // action.del("20092009"); // action.del("20102010"); // action.del("20132013"); }
/** * 为特定package下带@Table注解的类调用dao.create(XXX.class, force), 批量建表 * * @param dao Dao实例 * @param packageName package名称,自动包含子类 * @param force 如果表存在,是否先删后建 */ public static void createTablesInPackage(Dao dao, String packageName, boolean force) { for (Class<?> klass : Scans.me().scanPackage(packageName)) { if (klass.getAnnotation(Table.class) != null) dao.create(klass, force); } }