SSM框架初体验(查找功能)

博客介绍了在UserController文件中查找ID内容的实现过程。使用@Controller自动注入方法,@RequestMapping指向userController文件。新建user调用userService的findById方法,该方法进入UserServiceImpl执行userMapper的selectByPrimaryKey方法查找内容,最后将结果显示在index中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建UserController文件查找ID内容,在浏览器中查看。http://localhost:8080/blogssm/blogController/index

@Controller
@RequestMapping("userController")
public class UserController {
    @Autowired       //自动注入方法
    private UserService userService;
    @RequestMapping("index")
    public String index(Model model, HttpServletRequest request){
       User user= userService.findById(3);
       model.addAttribute("user",user);
        return "user/userList";
    }
}

@Controller为自动注入方法。
@RequestMapping为指针,指向userController文件中。

新建一个user调用userService类中的findById方法,

public interface UserService {
    User findById(Integer id);
}

然后findById进入UserServiceImpl 中执行userMapper类中的selectByPrimaryKey方法查找内容,并回复值给user。

@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User findById(Integer id) {
        User user = userMapper.selectByPrimaryKey((long) id);
        return user;
    }
}

selectByPrimaryKey方法的源码为:

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

执行完后回复值到user中,在model.addAttribute("user",user);的key\value回复到index中显示出来。在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值