SSM+VUE前后端分离项目,后端mapper.xml里的SQL语句在执行时遇到的问题 ¥15 xml sql spring 后端现在有两个Mapper.xml文件,一个UserMapper,一个To

SSM+VUE前后端分离项目,后端mapper.xml里的SQL语句在执行时遇到的问题
¥15
xml
sql
spring
后端现在有两个Mapper.xml文件,一个UserMapper,一个TokenMapper.在UserServiceImpl.java文件里执行

User user = userMapper.selectByExample(userExample);

```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里的插入操作的语句执行起来是正常的,就是查询操作的语句有问题。


根据你提供的信息,问题可能出现在TokenMapper.xml中的SQL语句或者返回结果的处理上。以下是几个可能导致问题的原因和解决方法:
1. SQL语句错误:请确保TokenMapper.xml中的SQL语句正确无误。你可以尝试在数据库客户端执行相同的SQL语句,看是否能够正常返回结果。
2. 参数问题:确认tokenExample对象中的值是正确设置的,以确保查询条件正确。你可以尝试在日志输出前打印tokenExample的参数值,以确认是否匹配到预期的记录。
3. 返回结果问题:检查TokenMapper.xml中的resultMap是否正确映射了数据库返回的字段到实体类的属性。确保实体类的属性类型与数据库字段类型相匹配。
4. 空指针异常:如果代码在查询到记录后立即停止,可能是由于返回的结果集(theToken)为空导致的空指针异常。在调用theToken.get(0)之前,可以添加对结果集是否为空的判断,例如:
```java
if (theToken != null && theToken.size() > 0) {
    return theToken.get(0);
} else {
    return null; // 或者抛出异常或其他处理方式
}

通过以上几个方面的排查,你应该能够找到问题所在并进行修复。如果问题仍然存在,建议提供更多的错误信息和代码细节,以便更好地帮助你。

猜你喜欢

转载自blog.csdn.net/zezeaichirou/article/details/134627101
今日推荐