标签:etag 参数 ide 位置 OLE manager 没有 开发效率 ack
学习BeetlSQL总结(1)1.创建mevan工程(BeetlSQL)
我已经创建好了mevan工程,结构如下:
2.添加BeetlSQL框架(配置pom.xml)
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.com.dhcc.beetl</groupId>
<artifactId>BeetlSQL</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>BeetlSQL Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- 2.日志 -->
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- Beetl SQL框架相关包 -->
<!-- https://mvnrepository.com/artifact/com.ibeetl/beetl -->
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>2.8.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.ibeetl/beetlsql -->
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetlsql</artifactId>
<version>2.10.31</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
</dependencies>
<build>
<finalName>BeetlSQL</finalName>
</build>
</project>
3.因为我们是要对数据库进行操作,所以第二步我们创建数据库及数据库表
CREATE DATABASE beetlsql;
USE beetlsql;
CREATE TABLE USER(
id INT(11) NOT NULL AUTO_INCREMENT,
NAME VARCHAR(64) DEFAULT NULL,
age INT(4) DEFAULT NULL,
username VARCHAR(64) DEFAULT NULL COMMENT ‘用户名‘,
roleId INT(11) DEFAULT NULL COMMENT ‘用户角色‘,
create_date DATETIME NULL DEFAULT NULL,
PRIMARY KEY(id)
)ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
4.接下来编写数据表user对应的entity实体类(后面将展示代码自动生成)
package cn.com.dhcc.beetlsql.entity;
import java.util.Date;
//用户实体类
public class User {
private Integer id;
private Integer age;
//用户角色
private Integer roleId;
private String name;
//用户名称
private String userName;
private Date createDate;
public User() {
}
public User(Integer id, Integer age, Integer roleId, String name, String userName, Date createDate) {
super();
this.id = id;
this.age = age;
this.roleId = roleId;
this.name = name;
this.userName = userName;
this.createDate = createDate;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getRoleId() {
return roleId;
}
public void setRoleId(Integer roleId) {
this.roleId = roleId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
@Override
public String toString() {
return "User [id=" + id + ", age=" + age + ", roleId=" + roleId + ", name=" + name + ", userName=" + userName
+ ", createDate=" + createDate + "]";
}
}
5.下来我们写一个main方法来对BeetlSQL进行功能测试以及理解
(1)建立数据库的连接,这里我们没有写配置文件,各项数据库连接所需参数就在主方法中设定
String mysqlDriver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/beetlsql?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false";
String userName="root";
String password="qitao1996";
ConnectionSource source=ConnectionSourceHelper.getSimple(mysqlDriver, url, userName, password);
DBStyle mysql=new MySqlStyle();
//SQL语句放于classpath的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()});
(2)接下里我们就先使用内置的sql语句对数据库表进行操作,代码如下
//1.使用内置的生成的sql新增用户,如果需要获取主键,可以传入KeyHolder
System.out.println("开始使用内置sql进行用户添加...");
User user=new User();
user.setAge(19);
user.setName("曼斯坦因");
int num=sqlManager.insert(user);
if(num>0) {
System.out.println("使用内置sql用户添加成功!!!!");
}
//2.使用内置sql查询用户
int id=1;
System.out.println("开始使用内置sql进行用户查询...");
user=sqlManager.unique(User.class, id);
System.out.println("使用内置sql进行用户查询完毕!!");
//3.更新(修改)数据,仅仅根据id更新值不为null的列
System.out.println("开始使用内置sql更新用户信息...");
User newUser=new User();
newUser.setId(1);
newUser.setName("朱可夫");
int num1=sqlManager.updateTemplateById(newUser);
if(num1>0) {
System.out.println("使用内置sql更新用户信息成功!!!!");
}
//4.模板查询
User query=new User();
query.setName("曼斯坦因");
System.out.println("开始进行模板查询...");
List<User> userList=sqlManager.template(query);
System.out.println("打印查询结果:");
for(User u:userList) {
System.out.println(u);
}
System.out.println("使用模板查询成功...");
执行结果如下:
开始使用内置sql进行用户添加...
┏━━━━━ Debug [user._gen_insert] ━━━
┣ SQL: insert into user
(name
,age
,create_date
) VALUES (?,?,?)
┣ 参数: [曼斯坦因, 19, null]
┣ 位置: main.TestBeetlSQL.main(TestBeetlSQL.java:52)
┣ 时间: 319ms
┣ 更新: [1]
┗━━━━━ Debug [user._gen_insert] ━━━
使用内置sql用户添加成功!!!!
开始使用内置sql进行用户查询...
┏━━━━━ Debug [user._gen_selectById] ━━━
┣ SQL: select * from user
where id
= ?
┣ 参数: [1]
┣ 位置: main.TestBeetlSQL.main(TestBeetlSQL.java:60)
┣ 时间: 37ms
┣ 结果: [User [id=1, age=19, roleId=null, name=朱可夫, userName=null, createDate=null]]
┗━━━━━ Debug [user._gen_selectById] ━━━
使用内置sql进行用户查询完毕!!
开始使用内置sql更新用户信息...
┏━━━━━ Debug [user._gen_updateTemplateById] ━━━
┣ SQL: update user
set name
=? where id
= ?
┣ 参数: [朱可夫, 1]
┣ 位置: main.TestBeetlSQL.main(TestBeetlSQL.java:68)
┣ 时间: 168ms
┣ 更新: [1]
┗━━━━━ Debug [user._gen_updateTemplateById] ━━━
使用内置sql更新用户信息成功!!!!
开始进行模板查询...
┏━━━━━ Debug [user._gen_selectByTemplate] ━━━
┣ SQL: select * from user
where 1=1 and name
=?
┣ 参数: [曼斯坦因]
┣ 位置: main.TestBeetlSQL.main(TestBeetlSQL.java:77)
┣ 时间: 91ms
┣ 结果: [10]
┗━━━━━ Debug [user._gen_selectByTemplate] ━━━
打印查询结果:
User [id=2, age=19, roleId=null, name=曼斯坦因, userName=mansitanying, createDate=null]
User [id=3, age=19, roleId=null, name=曼斯坦因, userName=deguoyuanshuai, createDate=null]
User [id=4, age=19, roleId=null, name=曼斯坦因, userName=null, createDate=null]
User [id=5, age=19, roleId=null, name=曼斯坦因, userName=null, createDate=null]
User [id=6, age=19, roleId=null, name=曼斯坦因, userName=null, createDate=null]
User [id=7, age=19, roleId=null, name=曼斯坦因, userName=null, createDate=null]
User [id=8, age=19, roleId=null, name=曼斯坦因, userName=null, createDate=null]
User [id=9, age=19, roleId=null, name=曼斯坦因, userName=null, createDate=null]
User [id=10, age=19, roleId=null, name=曼斯坦因, userName=null, createDate=null]
User [id=11, age=19, roleId=null, name=曼斯坦因, userName=null, createDate=null]
使用模板查询成功...
开始使用sql文件中sql语句进行查询....
┏━━━━━ Debug [user.select] ━━━
┣ SQL: select * from user where 1=1
┣ 参数: []
┣ 位置: main.TestBeetlSQL.main(TestBeetlSQL.java:88)
┣ 时间: 78ms
┣ 结果: [11]
┗━━━━━ Debug [user.select] ━━━
User [id=1, age=19, roleId=null, name=朱可夫, userName=null, createDate=null]
User [id=2, age=19, roleId=null, name=曼斯坦因, userName=mansitanying, createDate=null]
User [id=3, age=19, roleId=null, name=曼斯坦因, userName=deguoyuanshuai, createDate=null]
User [id=4, age=19, roleId=null, name=曼斯坦因, userName=null, createDate=null]
User [id=5, age=19, roleId=null, name=曼斯坦因, userName=null, createDate=null]
User [id=6, age=19, roleId=null, name=曼斯坦因, userName=null, createDate=null]
User [id=7, age=19, roleId=null, name=曼斯坦因, userName=null, createDate=null]
User [id=8, age=19, roleId=null, name=曼斯坦因, userName=null, createDate=null]
User [id=9, age=19, roleId=null, name=曼斯坦因, userName=null, createDate=null]
User [id=10, age=19, roleId=null, name=曼斯坦因, userName=null, createDate=null]
User [id=11, age=19, roleId=null, name=曼斯坦因, userName=null, createDate=null]
(3)展示代码生成:
##1.我们先在数据库中建立一张person表,用以生成Pojo类以及sql
CREATE TABLE person(
id INT(11) NOT NULL AUTO_INCREMENT,
NAME VARCHAR(64) DEFAULT NULL,
age INT(4) DEFAULT NULL,
pername VARCHAR(64) DEFAULT NULL COMMENT ‘人名‘,
roleId INT(11) DEFAULT NULL COMMENT ‘个人角色‘,
create_date DATETIME NULL DEFAULT NULL,
PRIMARY KEY(id)
)ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
##2.用以生成代码的代码:
//利用genPojoCodeToConsole 生成代码
System.out.println("开始生成代码:");
try {
sqlManager.genPojoCodeToConsole("person");
sqlManager.genSQLTemplateToConsole("person");
sqlManager.genSQLTemplateToConsole("user");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("代码生成结束....");
执行结果:
开始生成代码:
package com.test;
import java.math.*;
import java.util.Date;
import java.sql.Timestamp;
import org.beetl.sql.core.annotatoin.Table;
/*
gen by beetlsql 2018-08-17br/>*/
@Table(name="beetlsql.person")
public class Person {
private Integer id ;
private Integer age ;
/
个人角色
/
private Integer roleid ;
private String name ;
/
人名
/
private String pername ;
private Date createDate ;
public Person() {
}
public Integer getId(){
return id;
}
public void setId(Integer id ){
this.id = id;
}
public Integer getAge(){
return age;
}
public void setAge(Integer age ){
this.age = age;
}
/**
public String getName(){
return name;
}
public void setName(String name ){
this.name = name;
}
/**
public Date getCreateDate(){
return createDate;
}
public void setCreateDate(Date createDate ){
this.createDate = createDate;
}
}
注释
select #use("cols")# from person where #use("condition")#
id,NAME,age,pername,roleId,create_date
id=#id#,NAME=#name#,age=#age#,pername=#pername#,roleId=#roleid#,create_date=#createDate#
1 = 1
@if(!isEmpty(id)){
and id=#id#
@}
@if(!isEmpty(name)){
and NAME=#name#
@}
@if(!isEmpty(age)){
and age=#age#
@}
@if(!isEmpty(pername)){
and pername=#pername#
@}
@if(!isEmpty(roleid)){
and roleId=#roleid#
@}
@if(!isEmpty(createDate)){
and create_date=#createDate#
@}
sample
===
注释
select #use("cols")# from user where #use("condition")#
id,name,age,username,roleId,create_date
id=#id#,name=#name#,age=#age#,username=#username#,roleId=#roleid#,create_date=#createDate#
1 = 1
@if(!isEmpty(id)){
and id=#id#
@}
@if(!isEmpty(name)){
and name=#name#
@}
@if(!isEmpty(age)){
and age=#age#
@}
@if(!isEmpty(username)){
and username=#username#
@}
@if(!isEmpty(roleid)){
and roleId=#roleid#
@}
@if(!isEmpty(createDate)){
and create_date=#createDate#
@}
(4)利用外部sql文件进行数据库表的操作
##1.编写md格式的sql文件结构以及内容如下(以查询为例)
select
====
select * from user where 1=1
@if(!isEmpty(age)){
and age=#age#
@}
@if(!isEmpty(name)){
and name=#name#
@}
##2.主方法代码
//5.利用sql文件中sql语句进行查询
User query2 = new User();
query.setName("xiandafu");
System.out.println("开始使用sql文件中sql语句进行查询....");
List<User> list2 = sqlManager.select("user.select",User.class,query2);
for(User u:list2) {
System.out.println(u);
}
System.out.println("sql文件中sql语句进行查询成功...");
执行结果:
开始使用sql文件中sql语句进行查询....
┏━━━━━ Debug [user.select] ━━━
┣ SQL: select * from user where 1=1
┣ 参数: []
┣ 位置: main.TestBeetlSQL.main(TestBeetlSQL.java:88)
┣ 时间: 78ms
┣ 结果: [11]
┗━━━━━ Debug [user.select] ━━━
User [id=1, age=19, roleId=null, name=朱可夫, userName=null, createDate=null]
User [id=2, age=19, roleId=null, name=曼斯坦因, userName=mansitanying, createDate=null]
User [id=3, age=19, roleId=null, name=曼斯坦因, userName=deguoyuanshuai, createDate=null]
User [id=4, age=19, roleId=null, name=曼斯坦因, userName=null, createDate=null]
User [id=5, age=19, roleId=null, name=曼斯坦因, userName=null, createDate=null]
User [id=6, age=19, roleId=null, name=曼斯坦因, userName=null, createDate=null]
User [id=7, age=19, roleId=null, name=曼斯坦因, userName=null, createDate=null]
User [id=8, age=19, roleId=null, name=曼斯坦因, userName=null, createDate=null]
User [id=9, age=19, roleId=null, name=曼斯坦因, userName=null, createDate=null]
User [id=10, age=19, roleId=null, name=曼斯坦因, userName=null, createDate=null]
User [id=11, age=19, roleId=null, name=曼斯坦因, userName=null, createDate=null]
sql文件中sql语句进行查询成功...
BeetlSQL框架学习(一)——初识BeetlSQL,特点,内置sql的使用,Pojo代码生成等
标签:etag 参数 ide 位置 OLE manager 没有 开发效率 ack
原文地址:http://blog.51cto.com/13501268/2161053