原生查询+1

This commit is contained in:
TianJun
2021-04-30 11:21:24 +08:00
parent 343bd3f137
commit 82fb012850
6 changed files with 94 additions and 23 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ import (
type LingYeDO struct {
// 自增ID
ID uint64 `gorm:"column:id;type:bigint(20);not null;primary_key;AUTO_INCREMENT;comment:'自增ID'"`
ID uint64 `gorm:"primary_key"`
// 创建时间
CreatedAt time.Time `gorm:"comment:'创建时间'"`
// 更新时间
+11 -4
View File
@@ -1,17 +1,16 @@
package main
import (
"fmt"
"lingye-gin/src/config"
"lingye-gin/src/middleware"
v1 "lingye-gin/src/test/v1"
"lingye-gin/src/modules/system/entity"
"lingye-gin/src/util"
)
func main() {
// 初始化yaml配置
new(config.ApplicationProperties).Init()
v1.DescribeStudents(nil)
// 初始化redis
new(middleware.RedisPool).Init()
// 初始化gorm, 注册表
@@ -23,3 +22,11 @@ func main() {
// 初始化gin
new(middleware.GinEngine).Start()
}
func Test() {
// 通过ID查询
var role entity.Role
util.SelectOne(role, "select * from sys_role where id = ?", 1)
fmt.Printf("SelectOne=%v", role)
// 查询列表
}
+49
View File
@@ -0,0 +1,49 @@
package dao
import (
"lingye-gin/src/config"
"lingye-gin/src/modules/system/entity"
"lingye-gin/src/modules/system/service/query"
"lingye-gin/src/util"
)
type RoleDAO struct{}
// 新增
func (dao RoleDAO) Insert(resource *entity.Role) {
config.SqlExcutor.Create(resource)
}
// 删除
func (RoleDAO) DeleteById(id uint64) {
role := entity.Role{}
role.ID = id
config.SqlExcutor.Delete(&role)
}
// 更新
func (RoleDAO) UpdateById(role *entity.Role) {
model := &entity.Role{}
model.ID = role.ID
config.SqlExcutor.Model(model).Updates(map[string]interface{}{
"role_key": role.RoleKey,
"role_name": role.RoleName,
"role_sort": role.RoleSort,
})
}
// 条件分页查询
func (RoleDAO) SelectPage(condition query.UserQuery) ([]entity.Role, int) {
var users []entity.Role
var total int
page, pageSize := util.FixPage(condition.Page, condition.PageSize)
// 分页条件
pageConfig := config.SqlExcutor.Where("username LIKE ?", "%"+condition.Username+"%")
// 分页参数
pageConfig = pageConfig.Limit((page - 1) * pageSize).Offset(pageSize)
pageConfig = pageConfig.Find(&users)
// 统计数量
pageConfig.Count(&total)
return users, total
}
+2 -2
View File
@@ -7,8 +7,8 @@ import (
// 字典类型
type DictType struct {
base.LingYeDO
DictName string `gorm:"column:dict_name;type:varchar(255);comment:'字典名称'"`
DictType string `gorm:"column:dict_type;type:varchar(255);unique_index;comment:'字典类型'"`
DictName string `gorm:"column:dict_name;type:varchar(255);index:idx_name_type;comment:'字典名称'"`
DictType string `gorm:"column:dict_type;type:varchar(255);index:idx_name_type;comment:'字典类型'"`
Status string `gorm:"column:status;type:char(1);default:'0';comment:'状态(0正常 1停用)'"`
Remark string `gorm:"column:remark;comment:'备注'"`
}
+14 -16
View File
@@ -13,17 +13,15 @@ import (
)
// 高级加密标准(Advanced Encryption Standard, AES)
type AESUtils struct{}
// 16,24,32位字符串的话, 分别对应AES-128, AES-192, AES-256 加密方法
// key不能泄露
func (AESUtils) GetRandomPwdKey() string {
func GetRandomPwdKey() string {
u2 := uuid.NewV4()
return strings.Replace(u2.String(), "-", "", -1)
}
// PKCS7 填充模式
func (AESUtils) PKCS7Padding(ciphertext []byte, blockSize int) []byte {
func PKCS7Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
// Repeat()函数的功能是把切片[]byte{byte(padding)}复制padding个, 然后合并成新的字节切片返回
padText := bytes.Repeat([]byte{byte(padding)}, padding)
@@ -31,7 +29,7 @@ func (AESUtils) PKCS7Padding(ciphertext []byte, blockSize int) []byte {
}
// 填充的反向操作, 删除填充字符串
func (AESUtils) PKCS7UnPadding(origData []byte) ([]byte, error) {
func PKCS7UnPadding(origData []byte) ([]byte, error) {
// 获取数据长度
length := len(origData)
if length == 0 {
@@ -45,7 +43,7 @@ func (AESUtils) PKCS7UnPadding(origData []byte) ([]byte, error) {
}
// 实现加密
func (util AESUtils) AesEncrypt(origData []byte, key []byte) ([]byte, error) {
func AesEncrypt(origData []byte, key []byte) ([]byte, error) {
// 创建加密算法实例
block, err := aes.NewCipher(key)
if err != nil {
@@ -54,7 +52,7 @@ func (util AESUtils) AesEncrypt(origData []byte, key []byte) ([]byte, error) {
// 获取块的大小
blockSize := block.BlockSize()
// 对数据进行填充,让数据长度满足需求
origData = util.PKCS7Padding(origData, blockSize)
origData = PKCS7Padding(origData, blockSize)
// 采用AES加密方法中CBC加密模式
blocMode := cipher.NewCBCEncrypter(block, key[:blockSize])
result := make([]byte, len(origData))
@@ -64,7 +62,7 @@ func (util AESUtils) AesEncrypt(origData []byte, key []byte) ([]byte, error) {
}
// 实现解密
func (util AESUtils) AesDeCrypt(cypted []byte, key []byte) ([]byte, error) {
func AesDeCrypt(cypted []byte, key []byte) ([]byte, error) {
// 创建加密算法实例
block, err := aes.NewCipher(key)
if err != nil {
@@ -78,7 +76,7 @@ func (util AESUtils) AesDeCrypt(cypted []byte, key []byte) ([]byte, error) {
// 这个函数也可以用来解密
blockMode.CryptBlocks(origData, cypted)
// 去除填充字符串
origData, err = util.PKCS7UnPadding(origData)
origData, err = PKCS7UnPadding(origData)
if err != nil {
return nil, err
}
@@ -86,8 +84,8 @@ func (util AESUtils) AesDeCrypt(cypted []byte, key []byte) ([]byte, error) {
}
// 加密base64
func (util AESUtils) EnPwdCode(pwd []byte) (string, error) {
result, err := util.AesEncrypt(pwd, []byte(config.AppProps.Jwt.Secret))
func EnPwdCode(pwd []byte) (string, error) {
result, err := AesEncrypt(pwd, []byte(config.AppProps.Jwt.Secret))
if err != nil {
return "", err
}
@@ -95,19 +93,19 @@ func (util AESUtils) EnPwdCode(pwd []byte) (string, error) {
}
// 解密
func (util AESUtils) DePwdCode(pwd string) ([]byte, error) {
func DePwdCode(pwd string) ([]byte, error) {
// 解密base64字符串
pwdByte, err := base64.StdEncoding.DecodeString(pwd)
if err != nil {
return nil, err
}
// 执行AES解密
return util.AesDeCrypt(pwdByte, []byte(config.AppProps.Jwt.Secret))
return AesDeCrypt(pwdByte, []byte(config.AppProps.Jwt.Secret))
}
func (util AESUtils) Test() {
func AesTest() {
str := []byte("加密测试")
pwd, _ := util.EnPwdCode(str)
bt, _ := util.DePwdCode(pwd)
pwd, _ := EnPwdCode(str)
bt, _ := DePwdCode(pwd)
fmt.Println(string(bt))
}
+17
View File
@@ -0,0 +1,17 @@
package util
import (
"lingye-gin/src/config"
)
func SelectOne(target interface{}, sql string, values ...interface{}) {
config.SqlExcutor.Raw(sql, values).Scan(target)
}
func SelectList(target interface{}, sql string, values ...interface{}) {
config.SqlExcutor.Raw(sql, values).Scan(target)
}
func Create() {
}