mysql、mybatis、QueryWrapper的排序与mybatis的xml编写

排序

根据数据表sys_series中HOT(int类型)进行升序排列:

  • 原来的数据库中存储:
    初始
  • 排序
# 结果是HOT字段为null的所有数据都排在最前面,不为null的数据按升序排列
SELECT * FROM sys_series ORDER BY HOT;

直接sort字段升序

# 结果是HOT字段为null的所有数据都排在最后面,不为null的数据按数据库中原本的先后顺序排列
SELECT * FROM sys_series ORDER BY isnull(HOT);

isnull

# 结果是HOT字段为null的所有数据都排在最后面,不为null的数据按升序排列
SELECT * FROM sys_series ORDER BY isnull(HOT), HOT;

null排在最后面升序

# 结果是HOT字段为null的所有数据都排在最前面,不为null的数据按升序排列
SELECT * FROM sys_series ORDER BY HOT, isnull(HOT);

先排字段
最后一种相当于第一种。因为对于ORDER BY排序的优先级是从前往后,先按第一个order by的字段进行排序,如果遇到了两个相同的,则按照第二个排列,eg:

# 先按NEED的值升序排列,若NEED值相等,则根据HOT的值升序
SELECT * FROM sys_series ORDER BY NEED, HOT;

两个字段排序

# 先按NEED的值升序排列,若NEED值相等,则根据HOT的降序
SELECT * FROM sys_series ORDER BY NEED, HOT DESC;

两字段排序

Mybatis排序

List<SysSeries> getLike();
<select id="getLike" resultType="java.util.List">
    SELECT *    
    FROM sys_series    
    ORDER BY ISNULL(HOT), HOT
</select>
报错

The error occurred while handling results### SQL: SELECT * FROM sys_series ORDER BY ISNULL(HOT), HOT### Cause: java.lang.UnsupportedOperationException

原因

resultType配置错误,返回的结果为List,所以resultType应该配置为实体类的路径:resultType=“com.harmony.supreme.modular.series.entity.SysSeries”,eg:

List<String> getAllIds();
<select id="getAllIds" resultType="java.lang.String"></select>
SysSeries selectById(String id);
<select id="selectById" resultType="com.harmony.supreme.modular.series.entity.SysSeries"></select>
List<Map<String, Object>> countByIdList(@Param("idList") List<String> idList);
<select id="countByIdList" resultType="java.util.Map"></select>

问答

根据 int 类型的字段 SORT 进行升序排列,如果SORT值为null空,则按创建时间升序排列:

select * from table_name ORDER BY ISNULL(SORT), SORT,CREATE_TIME
select * from table_name where (create_user = 'xxx') ORDER BY ISNULL(SORT), SORT,CREATE_TIME
List<TableName> get(String createUser);
  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Spring Boot 中同时集成 Neo4j、MySQLMyBatis,你可以按照以下步骤进行操作: 1. 在 `pom.xml` 文件中添加以下依赖: ```xml <!-- Neo4j --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-neo4j</artifactId> </dependency> <!-- MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> ``` 2. 在 `application.properties` 文件中分别配置 Neo4j 和 MySQL 数据库的连接信息: ```properties # Neo4j spring.data.neo4j.uri=bolt://localhost:7687 spring.data.neo4j.username=neo4j spring.data.neo4j.password=neo4j # MySQL spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 3. 在 `@SpringBootApplication` 注解标注的启动类中添加 `@MapperScan` 注解,指定 MyBatis 的 Mapper 所在的包路径: ```java @SpringBootApplication @MapperScan("com.example.demo.mapper") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 4. 在 MyBatis 的 Mapper 接口中编写 SQL 语句和对应的方法,可以使用 `@Mapper` 注解或者在启动类中添加 `@MapperScan` 注解指定扫描路径。 5. 在需要使用 Neo4j 的地方注入 `Neo4jTemplate` 或者 `Session`,使用它们来进行 Neo4j 数据库的操作。 6. 在需要使用 MySQL 的地方注入 `JdbcTemplate` 或者 `DataSource`,使用它们来进行 MySQL 数据库的操作。 注意,同时使用多个数据库时,需要注意事务的处理,可以使用 `@Transactional` 注解或者 Spring 的编程式事务管理来实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值