Lowest_Clown 2023-11-04 15:44 采纳率: 0%
浏览 3
已结题

SSM+VUE前后端分离项目,后端mapper.xml里的SQL语句在执行时遇到的问题

后端现在有两个Mapper.xml文件,一个UserMapper,一个TokenMapper.在UserServiceImpl.java文件里执行

User user = userMapper.selectByExample(userExample);


```的时候,能正确查询到数据库里的记录然后赋值到user。但是在TokenServiceImpl.java里执行
```java
List<Token> theToken = tokenMapper.selectTokenByExample(tokenExample);


```的时候,查询到记录后直接就不往下继续跑了,后面的代码根本不执行。以下是一些相关代码:

```java
@Service
public class TokenServiceImpl implements TokenService {
    @Autowired
    private TokenMapper tokenMapper;

    @Override
    public Token findToken(String token) {
        TokenExample tokenExample = new TokenExample();
        TokenExample.Criteria criteria = tokenExample.createCriteria();
        criteria.andTokenEqualTo(token);
        List<Token> theToken = tokenMapper.selectTokenByExample(tokenExample);
        System.out.println("tokenList:"+theToken);
        return theToken.get(0);
    }
    
    @Override
    public void addToken(Token token) {
        tokenMapper.insert(token);
    }
    
    @Override
    public void updateToken(Token token) {
        tokenMapper.updateByPrimaryKeySelective(token);
    }
}
<resultMap id="BaseResultMap" type="com.ssm.entity.Token">
    <id column="id" property="id" />
    <result column="user_id" property="userId"  />
    <result column="token" property="token" />
    <result column="applyTime" property="applyTime" />
    <result column="expireTime" property="expireTime" />
    <result column="countAuth" property="countAuth" />
    <result column="maxCountAuth" property="maxCountAuth" />
    <result column="ussAge" property="ussAge" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
<sql id="Base_Column_List">
    id, userId, token, applyTime, expireTime, countAuth, maxCountAuth, ussAge
  </sql>
  <select id="selectTokenByExample" parameterType="com.ssm.entity.TokenExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    'true' as QUERYID,
    <include refid="Base_Column_List" />
    from tokens
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>

UserMapper.xml里的代码和TokenMapper.xml差不多,就是resultMap和Base_Column_List是各自实体类还有数据表的内容。
然后下面是执行Mapper.xml里的SQL的时候输出的日志:
UserMapper里的SQL执行日志

Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@367b1aaa] was not registered for synchronization because synchronization is not active
JDBC Connection [jdbc:mysql://localhost:3306/webvideo?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai, UserName=root@localhost, MySQL Connector/J] will not be managed by Spring
==>  Preparing: select 'true' as QUERYID, id, username, password, identity from users WHERE ( username = ? and password = ? )
==> Parameters: first(String), 9608a82e5a0fb219ccb2(String)
<==    Columns: QUERYID, id, username, password, identity
<==        Row: true, 2, first, 9608a82e5a0fb219ccb2, 0
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@367b1aaa]

TokenMapper里的SQL执行日志

Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6ee175ad] was not registered for synchronization because synchronization is not active
JDBC Connection [jdbc:mysql://localhost:3306/webvideo?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai, UserName=root@localhost, MySQL Connector/J] will not be managed by Spring
==>  Preparing: select 'true' as QUERYID, id, userId, token, applyTime, expireTime, countAuth, maxCountAuth, ussAge from tokens WHERE ( token = ? )
==> Parameters: 1699082hYIbC1166E99l41E6266106(String)
<==    Columns: QUERYID, id, userId, token, applyTime, expireTime, countAuth, maxCountAuth, ussAge
<==        Row: true, 48, 2, 1699082hYIbC1166E99l41E6266106, 2023-11-04 15:17:46, 2023-12-04 15:17:46, 0, 1000, default
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6ee175ad]

输出完这个日志后,代码就不继续往下跑了。
顺便说一下,执行TokenMapper里的插入操作的语句执行起来是正常的,就是查询操作的语句有问题。
求解答,搞一天了还没解决这问题

  • 写回答

1条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2023-11-04 15:45
    关注

    引自强大而免费的微信小程序:皆我百晓生

    根据提供的代码和日志,报错提示是因为没有激活同步功能而导致的。解决方案如下:

    1. 在Spring配置文件中,确保已激活事务管理器。可以使用以下代码片段:

      <tx:annotation-driven/>
      
    2. 在TokenServiceImpl类上添加@Transactional注解,以确保在该类中的所有方法都处于同一个事务中。例如:

      @Service
      @Transactional
      public class TokenServiceImpl implements TokenService {
        //...
      }
      
    3. 确保在方法上使用@Transactional注解,以确保方法在事务中执行。例如:

      @Override
      @Transactional
      public void addToken(Token token) {
        tokenMapper.insert(token);
      }
      

    请注意,以上解决方案假设您在项目中使用了Spring事务管理器来管理数据库事务。如果您没有使用事务管理器或使用了其他的事务管理方式,请根据实际情况调整解决方案。

    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 11月4日
  • 创建了问题 11月4日

悬赏问题

  • ¥200 基于同花顺supermind的量化策略脚本编辑
  • ¥20 Html备忘录页面制作
  • ¥15 黄永刚的晶体塑性子程序中输入的材料参数里的晶体取向参数是什么形式的?
  • ¥20 数学建模来解决我这个问题
  • ¥15 计算机网络ip分片偏移量计算头部是-20还是-40呀
  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?