String dictInfoCode = dictInfo.getCodeFieldName(); List<ColumnInfo> columnsByTableName = this.getColumnsByTableName(tableName); String sqlString = "SELECT "; List<String> list = new ArrayList<>(); for (ColumnInfo columnInfo : columnsByTableName) { list.add("`" + columnInfo.getColumnName() + "`"); } sqlString += StringUtils.join(list, ','); StringUtils.join(list, ','); sqlString += " FROM " + tableName; sqlString += " WHERE " + "`" + dictInfoCode + "`" + " = " + " '" + code + "' "; //System.out.println(" SQL = " + sqlString); // 查 Map<String, Object> map = this.superManagerSelectOne(sqlString);
时间: 2024-03-29 16:38:47 浏览: 165
这段代码是用来查询数据库中指定表格中某个字段等于给定值的数据行。其中使用了 getColumnsByTableName 方法获取表格中的列信息,使用 StringUtils.join 方法将列名拼接成一个字符串。然后使用 SQL 语句查询符合条件的数据行,并通过 superManagerSelectOne 方法获取查询结果。
相关问题
安卓开发 compose 现在有一个List<Note> 要实现拖动排序功能jetpack room数据库 该如何实现?kotlin完整代码
您好,以下是实现安卓开发 compose 现在有一个List<Note> 要实现拖动排序功能jetpack room数据库的 kotlin 代码示例:
首先,您需要在 build.gradle 文件中添加以下依赖:
```
implementation "androidx.compose.foundation:foundation-layout:1.0.0-beta02"
implementation "androidx.compose.material:material:1.0.0-beta02"
implementation "androidx.compose.runtime:runtime-livedata:1.0.0-beta02"
implementation "androidx.compose.ui:ui:1.0.0-beta02"
implementation "androidx.compose.ui:ui-tooling:1.0.0-beta02"
implementation "androidx.room:room-runtime:2.3.0"
kapt "androidx.room:room-compiler:2.3.0"
```
然后,您可以使用 Jetpack Compose 实现一个可拖动的列表,并使用 Room 数据库存储和加载数据。以下是完整的 Kotlin 代码:
```
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.draggable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@Entity(tableName = "notes")
data class Note(
@PrimaryKey val id: Int,
@ColumnInfo(name = "text") val text: String,
@ColumnInfo(name = "position") var position: Int
)
@Dao
interface NoteDao {
@Query("SELECT * FROM notes ORDER BY position ASC")
fun getNotes(): LiveData<List<Note>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(note: Note)
@Update
suspend fun update(note: Note)
}
@Database(entities = arrayOf(Note::class), version = 2)
abstract class AppDatabase : RoomDatabase() {
abstract fun noteDao(): NoteDao
companion object {
private var instance: AppDatabase? = null
fun getInstance(context: Context): AppDatabase {
return instance ?: synchronized(this) {
instance ?: buildDatabase(context).also { instance = it }
}
}
private fun buildDatabase(context: Context): AppDatabase {
return Room.databaseBuilder(context, AppDatabase::class.java, "notes.db")
.addMigrations(object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE notes ADD COLUMN position INTEGER DEFAULT 0")
}
})
.build()
}
}
}
class NoteViewModel : ViewModel() {
private val appDatabase = AppDatabase.getInstance(MyApplication.context)
private val noteDao = appDatabase.noteDao()
private val _notes = MutableLiveData<List<Note>>()
init {
loadNotes()
}
fun getNotes(): LiveData<List<Note>> {
return _notes
}
fun addNote() {
viewModelScope.launch(Dispatchers.IO) {
val count = noteDao.getNotes().value?.size ?: 0
noteDao.insert(Note(count + 1, "Note ${count + 1}", count + 1))
}
}
fun updateNotePosition(note: Note, position: Int) {
viewModelScope.launch(Dispatchers.IO) {
note.position = position
noteDao.update(note)
}
}
private fun loadNotes() {
viewModelScope.launch(Dispatchers.IO) {
val notes = noteDao.getNotes().value ?: emptyList()
_notes.postValue(notes)
}
}
}
@Composable
fun NotesList(noteViewModel: NoteViewModel) {
val notes by noteViewModel.getNotes().observeAsState(listOf())
Column(modifier = Modifier.padding(16.dp)) {
Button(onClick = { noteViewModel.addNote() }) {
Text("Add Note")
}
for (i in notes.indices) {
val note = notes[i]
Box(
modifier = Modifier
.fillMaxSize()
.alpha(if (note.position == i) 1f else 0.5f)
.background(Color.White)
.draggable(
orientation = Orientation.Vertical,
onDragStarted = { /* Do nothing */ },
onDragStopped = { noteViewModel.updateNotePosition(note, it) }
)
) {
Text(
text = note.text,
modifier = Modifier.padding(16.dp)
)
}
}
}
}
class MainActivity : ComponentActivity() {
private val noteViewModel by viewModels<NoteViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
NotesList(noteViewModel)
}
}
}
```
代码说明:
1.定义 Note 实体类,其中包含 id、text 和 position 字段。
2.定义 NoteDao 接口,其中包含 getNotes()、insert() 和 update() 函数。
3.AppDatabase 是一个 Room 数据库,它包含 NoteDao。
4.NoteViewModel 是一个 ViewModel,它包含一个 LiveData 列表,用于存储 Note 对象列表。
5.NotesList 是一个 Composable 函数,它显示 Note 对象列表,并允许用户重新排序它们。
6.在主 Activity 中使用 NotesList Composable 函数。
该代码依赖于 Jetpack Compose 和 Room 数据库。Jetpack Compose 提供了一种简单的方法来实现可拖动的列表,而 Room 数据库用于保存和加载 Note 数据。
写一个Java代码,使用Druid实现sql解析,并分析字段血缘关系
以下是一个使用Druid解析SQL并分析字段血缘关系的Java代码示例:
```
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.*;
import com.alibaba.druid.sql.ast.statement.*;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.parser.SQLStatementParser;
import java.util.*;
public class DruidSqlParser {
public static void main(String[] args) {
String sql = "SELECT t1.id, t2.name FROM table1 t1 JOIN table2 t2 ON t1.id = t2.id WHERE t1.age > 18";
List<ColumnInfo> columnInfoList = analyzeSql(sql);
for (ColumnInfo columnInfo : columnInfoList) {
System.out.println(columnInfo);
}
}
public static List<ColumnInfo> analyzeSql(String sql) {
List<ColumnInfo> columnInfoList = new ArrayList<>();
SQLStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
for (SQLStatement statement : statementList) {
if (statement instanceof SQLSelectStatement) {
SQLSelectStatement selectStatement = (SQLSelectStatement) statement;
SQLSelectQueryBlock queryBlock = selectStatement.getSelect().getQueryBlock();
analyzeSelectItems(queryBlock.getSelectList(), columnInfoList);
analyzeWhere(queryBlock.getWhere(), columnInfoList);
analyzeJoin(queryBlock.getFrom(), columnInfoList);
}
}
return columnInfoList;
}
private static void analyzeSelectItems(List<SQLSelectItem> selectItems, List<ColumnInfo> columnInfoList) {
for (SQLSelectItem selectItem : selectItems) {
SQLExpr expr = selectItem.getExpr();
String columnName = null;
String tableName = null;
if (expr instanceof SQLPropertyExpr) {
SQLPropertyExpr propertyExpr = (SQLPropertyExpr) expr;
columnName = propertyExpr.getName();
tableName = propertyExpr.getOwner().getName();
} else if (expr instanceof SQLIdentifierExpr) {
SQLIdentifierExpr identifierExpr = (SQLIdentifierExpr) expr;
columnName = identifierExpr.getName();
}
if (columnName != null) {
ColumnInfo columnInfo = new ColumnInfo(columnName, tableName, true);
if (!columnInfoList.contains(columnInfo)) {
columnInfoList.add(columnInfo);
}
}
}
}
private static void analyzeWhere(SQLExpr whereExpr, List<ColumnInfo> columnInfoList) {
if (whereExpr instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) whereExpr;
analyzeWhere(binaryOpExpr.getLeft(), columnInfoList);
analyzeWhere(binaryOpExpr.getRight(), columnInfoList);
} else if (whereExpr instanceof SQLBinaryOpExprGroup) {
SQLBinaryOpExprGroup binaryOpExprGroup = (SQLBinaryOpExprGroup) whereExpr;
for (SQLExpr sqlExpr : binaryOpExprGroup.getItems()) {
analyzeWhere(sqlExpr, columnInfoList);
}
} else if (whereExpr instanceof SQLIdentifierExpr) {
SQLIdentifierExpr identifierExpr = (SQLIdentifierExpr) whereExpr;
String columnName = identifierExpr.getName();
ColumnInfo columnInfo = new ColumnInfo(columnName, null, false);
if (!columnInfoList.contains(columnInfo)) {
columnInfoList.add(columnInfo);
}
}
}
private static void analyzeJoin(SQLTableSource tableSource, List<ColumnInfo> columnInfoList) {
if (tableSource instanceof SQLJoinTableSource) {
SQLJoinTableSource joinTableSource = (SQLJoinTableSource) tableSource;
analyzeJoin(joinTableSource.getLeft(), columnInfoList);
analyzeJoin(joinTableSource.getRight(), columnInfoList);
SQLExpr onExpr = joinTableSource.getCondition();
if (onExpr instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) onExpr;
SQLExpr leftExpr = binaryOpExpr.getLeft();
SQLExpr rightExpr = binaryOpExpr.getRight();
if (leftExpr instanceof SQLPropertyExpr && rightExpr instanceof SQLPropertyExpr) {
SQLPropertyExpr leftPropertyExpr = (SQLPropertyExpr) leftExpr;
SQLPropertyExpr rightPropertyExpr = (SQLPropertyExpr) rightExpr;
String leftColumnName = leftPropertyExpr.getName();
String leftTableName = leftPropertyExpr.getOwner().getName();
String rightColumnName = rightPropertyExpr.getName();
String rightTableName = rightPropertyExpr.getOwner().getName();
ColumnInfo leftColumnInfo = new ColumnInfo(leftColumnName, leftTableName, false);
ColumnInfo rightColumnInfo = new ColumnInfo(rightColumnName, rightTableName, false);
if (columnInfoList.contains(leftColumnInfo)) {
int index = columnInfoList.indexOf(leftColumnInfo);
columnInfoList.get(index).setTableName(leftTableName);
}
if (columnInfoList.contains(rightColumnInfo)) {
int index = columnInfoList.indexOf(rightColumnInfo);
columnInfoList.get(index).setTableName(rightTableName);
}
}
}
} else if (tableSource instanceof SQLSubqueryTableSource) {
SQLSubqueryTableSource subqueryTableSource = (SQLSubqueryTableSource) tableSource;
SQLSelect subSelect = subqueryTableSource.getSelect();
analyzeSelectItems(subSelect.getQueryBlock().getSelectList(), columnInfoList);
analyzeWhere(subSelect.getQueryBlock().getWhere(), columnInfoList);
analyzeJoin(subSelect.getQueryBlock().getFrom(), columnInfoList);
} else if (tableSource instanceof SQLExprTableSource) {
SQLExprTableSource exprTableSource = (SQLExprTableSource) tableSource;
String tableName = null;
if (exprTableSource.getAlias() != null) {
tableName = exprTableSource.getAlias();
} else if (exprTableSource.getExpr() instanceof SQLIdentifierExpr) {
tableName = ((SQLIdentifierExpr) exprTableSource.getExpr()).getName();
}
for (ColumnInfo columnInfo : columnInfoList) {
if (columnInfo.getTableName() == null) {
columnInfo.setTableName(tableName);
}
}
}
}
}
class ColumnInfo {
private String columnName;
private String tableName;
private boolean isSelectItem;
public ColumnInfo(String columnName, String tableName, boolean isSelectItem) {
this.columnName = columnName;
this.tableName = tableName;
this.isSelectItem = isSelectItem;
}
public String getColumnName() {
return columnName;
}
public void setColumnName(String columnName) {
this.columnName = columnName;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public boolean isSelectItem() {
return isSelectItem;
}
public void setSelectItem(boolean selectItem) {
isSelectItem = selectItem;
}
@Override
public String toString() {
return "ColumnInfo{" +
"columnName='" + columnName + '\'' +
", tableName='" + tableName + '\'' +
", isSelectItem=" + isSelectItem +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ColumnInfo that = (ColumnInfo) o;
return Objects.equals(columnName, that.columnName) &&
Objects.equals(tableName, that.tableName);
}
@Override
public int hashCode() {
return Objects.hash(columnName, tableName);
}
}
```
这个代码实现了对SELECT、WHERE和JOIN三个部分的分析,并且能够正确地识别出每个字段所属的表和是否是SELECT中的选择项。你可以根据自己的需求对这个代码进行扩展和修改。
阅读全文
相关推荐








