RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 805442
Accepted
Timur  Musharapov
Timur Musharapov
Asked:2020-03-27 21:33:49 +0000 UTC2020-03-27 21:33:49 +0000 UTC 2020-03-27 21:33:49 +0000 UTC

春天。无法实现服务(MyBatis)

  • 772

无法使用 .从表中正确获取所有记录MyBatis。

错误跟踪:

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /C:/Users/Tim/eclipse-workspace/springMVC-mybatis-postgresql-fbcda0e11b1addcac43be8813e16b8b703022591/src/main/java/com/core/newbie/controller/UserController.java:[49,52] incompatible types: com.core.newbie.model.User cannot be converted to java.util.List<com.core.newbie.model.User>
[ERROR] /C:/Users/Tim/eclipse-workspace/springMVC-mybatis-postgresql-fbcda0e11b1addcac43be8813e16b8b703022591/src/main/java/com/core/newbie/impl/UserServiceImpl.java:[29,8] com.core.newbie.impl.UserServiceImpl is not abstract and does not override abstract method getAll() in com.core.newbie.service.UserService
[ERROR] /C:/Users/Tim/eclipse-workspace/springMVC-mybatis-postgresql-fbcda0e11b1addcac43be8813e16b8b703022591/src/main/java/com/core/newbie/impl/UserServiceImpl.java:[59,23] getAll() in com.core.newbie.impl.UserServiceImpl cannot implement getAll() in com.core.newbie.service.UserService
  return type java.util.List<com.core.newbie.model.User> is not compatible with com.core.newbie.model.User
[ERROR] /C:/Users/Tim/eclipse-workspace/springMVC-mybatis-postgresql-fbcda0e11b1addcac43be8813e16b8b703022591/src/main/java/com/core/newbie/impl/UserServiceImpl.java:[57,5] method does not override or implement a method from a supertype
[ERROR] /C:/Users/Tim/eclipse-workspace/springMVC-mybatis-postgresql-fbcda0e11b1addcac43be8813e16b8b703022591/src/main/java/com/core/newbie/impl/UserServiceImpl.java:[60,26] cannot find symbol
  symbol:   method findAll()
  location: variable userMapper of type com.core.newbie.mapper.UserMapper
[INFO] 5 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE

用户映射器.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.core.newbie.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.core.newbie.model.User">
    <id column="id" property="id" jdbcType="INTEGER"/>
    <result column="user_name" property="userName" jdbcType="VARCHAR"/>
    <result column="password" property="password" jdbcType="VARCHAR"/>
    <result column="age" property="age" jdbcType="INTEGER"/>
</resultMap>

<sql id="Base_Column_List">
    id, user_name, password, age
</sql>

<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer">
    select
    <include refid="Base_Column_List"/>
    from n_user
    where id = #{id,jdbcType=INTEGER}
</select>

<select id="findAll" resultMap="BaseResultMap">
    select * from n_user
</select>

<!--<select id="selectAllWithPagination" resultMap="BaseResultMap">
    select * from n_user
</select>-->

<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from n_user
    where id = #{id,jdbcType=INTEGER}
</delete>

<insert id="insert" parameterType="com.core.newbie.model.User">
    insert into n_user (id, user_name, password,
    age)
    values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
    #{age,jdbcType=INTEGER})
</insert>

<insert id="insertSelective" parameterType="com.core.newbie.model.User">
    insert into n_user
    <trim prefix="(" suffix=")" suffixOverrides=",">
        <if test="id != null">
            id,
        </if>
        <if test="userName != null">
            user_name,
        </if>
        <if test="password != null">
            password,
        </if>
        <if test="age != null">
            age,
        </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
        <if test="id != null">
            #{id,jdbcType=INTEGER},
        </if>
        <if test="userName != null">
            #{userName,jdbcType=VARCHAR},
        </if>
        <if test="password != null">
            #{password,jdbcType=VARCHAR},
        </if>
        <if test="age != null">
            #{age,jdbcType=INTEGER},
        </if>
    </trim>
</insert>

<update id="updateByPrimaryKeySelective" parameterType="com.core.newbie.model.User">
    update n_user
    <set>
        <if test="userName != null">
            user_name = #{userName,jdbcType=VARCHAR},
        </if>
        <if test="password != null">
            password = #{password,jdbcType=VARCHAR},
        </if>
        <if test="age != null">
            age = #{age,jdbcType=INTEGER},
        </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
</update>

<update id="updateByPrimaryKey" parameterType="com.core.newbie.model.User">
    update n_user
    set user_name = #{userName,jdbcType=VARCHAR},
    password = #{password,jdbcType=VARCHAR},
    age = #{age,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
</update>
</mapper>

用户.java(模型):

package com.core.newbie.model;

import javax.persistence.*;
import java.util.Set;

public class User {

private Integer id;

private String userName;

private String password;

private Integer age;

@ManyToMany
@JoinTable(name = "user_roles", joinColumns = @JoinColumn(name = "user_id"),
        inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles;

public Integer getId() {
    return id;
}

public void setId(final Integer id) {
    this.id = id;
}

public String getUserName() {
    return userName;
}

public void setUserName(final String userName) {
    this.userName = userName == null ? null : userName.trim();
}

public String getPassword() {
    return password;
}

public void setPassword(final String password) {
    this.password = password == null ? null : password.trim();
}

public Integer getAge() {
    return age;
}

public void setAge(final Integer age) {
    this.age = age;
}

public Set<Role> getRoles() {
    return roles;
}

public void setRoles(Set<Role> roles) {
    this.roles = roles;
}
}

用户服务:

package com.core.newbie.service;

import com.core.newbie.model.User;

public interface UserService {
User getUserById(int userId);

//  User findByUsername(String username);

void insetUser(User user);

User getAll();
}

UserServiceImpl.java:

package com.core.newbie.impl;

import com.core.newbie.mapper.UserMapper;
import com.core.newbie.model.User;
import com.core.newbie.service.UserService;

import java.util.List;

import org.apache.ibatis.annotations.Select;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;

@Override
public User getUserById(final int userId) {
    return userMapper.selectByPrimaryKey(userId);
}

@Override
public void insetUser(final User user) {
    userMapper.insert(user);
}

@Override
@Select("select * from n_user")
public List<User> getAll() {
    return userMapper.findAll();
}
}

用户控制器.java:

package com.core.newbie.controller;

import com.alibaba.fastjson.JSON;
import com.core.newbie.model.User;
import com.core.newbie.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/user")
public class UserController {
Logger logger = Logger.getLogger(UserController.class);

@Autowired
private UserService userService;

@RequestMapping("/showUser")
public String showUser(final HttpServletRequest request, final Model model) 
{
    final int userId = Integer.parseInt(request.getParameter("id"));
    final User user = userService.getUserById(userId);
    model.addAttribute("user", user);
    logger.debug("running in UserController.java -> showUser()");
    logger.info(JSON.toJSON(request.getRequestURI()));
    logger.info(JSON.toJSON(user));
    return "/user/showUser.jsp";
}

@RequestMapping("/users")
public String users() {
    final List<User> users = userService.getAll();
    logger.debug("running in UserController.java -> users()");
    logger.info(JSON.toJSON(users));
    return "/user/users.jsp";
}
}
java
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Komdosh
    2020-03-27T23:17:22Z2020-03-27T23:17:22Z

    您在接口中User错误地指定了返回类型User getAll();,但它被UserServiceImpl假定为List<User> getAll();

    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5