nutz 直接运行sql语句 mysqlDao java

public class MySqlDao {
    private MySqlDao() {               //防止通过new去实例化
    }

    private static Ioc ioc;
    private static Dao dao;

    public static Dao getSqlDao() {
        if (ioc == null) {
            ioc = new NutIoc(new JsonLoader("dao.js"));
            DataSource ds = ioc.get(DataSource.class);
            if(dao==null) {
                dao = new NutDao(ds);
            }
        }
        return dao;
    }

    /** * 运行sql语句 返回到一个nutz的model list里 * @param sql sql语句 * @param classOfT 模型类名 * @return 一个pojo列表 */
    public static  List excuteSql(String sql, Class classOfT){
        Dao dao = getSqlDao();
        Sql sq = Sqls.create(sql);
        sq.setCallback(Sqls.callback.entities());
        sq.setEntity(dao.getEntity(classOfT));
        dao.execute(sq);
        return sq.getList(classOfT);
    }

    /** * 返回执行sql语句满足条件的行数 * @param sql select count(0) from t_complete .... * @return int */
    public static int excuteSqlCount(String sql){
        Sql sq = Sqls.create(sql);
        sq.setCallback(Sqls.callback.records());
        getSqlDao().execute(sq);
        String r = sq.getString();
        String rr = Re.match(":(\\d+)",r);
        return  Integer.parseInt(rr);
    }

    public static void main(String[] args) {
        int r = excuteSqlCount("select count(0) from t_complete_questionnaire where " +
                "question_id='30' and option_id='90' " +
                "and user_id='23123123'");
        System.out.println(r);
        System.out.println(getSqlDao());
        System.out.println(getSqlDao());
    }
}

你可能感兴趣的:(java)