package beetlsql;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import org.beetl.sql.core.ClasspathLoader;
import org.beetl.sql.core.ConnectionSource;
import org.beetl.sql.core.ConnectionSourceHelper;
import org.beetl.sql.core.Interceptor;
import org.beetl.sql.core.SQLLoader;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.beetl.sql.core.UnderlinedNameConversion;
import org.beetl.sql.core.db.DBStyle;
import org.beetl.sql.core.db.MySqlStyle;
import org.beetl.sql.core.query.Query;
import org.beetl.sql.ext.DebugInterceptor;
import beetlsql.model.User;
public class DBUtils {
public static void main(String[] args) {
// add();
// del();
// update();
query();
// query2();
// query3();
// queryForJDBC();
}
public static void add() {
SQLManager sqlManager = getSQLManager();
// 使用内置的生成的sql 新增用户,如果需要获取主键,可以传入KeyHolder
User user = new User();
user.setAge(25);
user.setName("张盛13333333");
user.setCreateDate(new Date());
sqlManager.insert(user);
}
public static void del() {
SQLManager sqlManager = getSQLManager();
User user = new User();
user.setId(6);
sqlManager.deleteObject(user);
}
public static void update() {
SQLManager sqlManager = getSQLManager();
User user = new User();
user.setId(6);
user.setName("张盛111111");
user.setCreateDate(new Date());
sqlManager.updateTemplateById(user);
}
public static void query() {
SQLManager sqlManager = SQLManageFactory.getSQLManager();
User user = new User();
user.setName("xiandafu");
List<User> list = sqlManager.template(user);
list.forEach(item -> System.out.println(item));
}
public static void query2() {
SQLManager sqlManager = getSQLManager();
Query userQuery = sqlManager.query(User.class);
List<User> users = userQuery.andEq("name","张盛").select();
users.forEach(item -> System.out.println(item));
}
public static void query3() {
使用user.md 文件里的select语句
SQLManager sqlManager = getSQLManager();
User query = new User();
query.setName("xiandafu");
List<User> users = sqlManager.select("user.select",User.class,query);
users.forEach(item -> System.out.println(item));
}
public static void queryForJDBC() {
使用user.md 文件里的select语句
SQLManager sqlManager = getSQLManager();
String sql = "select u.*,a.name as address_name from user u left join address a on u.address_id = a.id";
List<User> users = sqlManager.execute(new SQLReady(sql),User.class);
users.forEach(item -> System.out.println(item));
}
public static SQLManager getSQLManager() {
Properties prop = getProp();
ConnectionSource source = ConnectionSourceHelper.getSimple(
prop.getProperty("driver"),
prop.getProperty("url"),
prop.getProperty("username"),
prop.getProperty("password"));
DBStyle mysql = new MySqlStyle();
// sql语句放在classpagth的/sql 目录下
SQLLoader loader = new ClasspathLoader("/sql");
// 数据库命名跟java命名一样,所以采用DefaultNameConversion,还有一个是UnderlinedNameConversion,下划线风格的,
UnderlinedNameConversion nc = new UnderlinedNameConversion();
// 最后,创建一个SQLManager,DebugInterceptor 不是必须的,但可以通过它查看sql执行情况
SQLManager sqlManager = new SQLManager(mysql, loader, source, nc, new Interceptor[] { new DebugInterceptor() });
return sqlManager;
}
public static Properties getProp() {
Properties prop = new Properties();
try (InputStream in = DBUtils.class.getResourceAsStream("/jdbc.properties")) {
prop.load(in);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return prop;
}
}