热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

springboot——springboot的基本配置——springboot整合mybatis——本地实例运行——保存操作——int类型...

mapper文件:

mapper文件:


PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
insert into users(id,name,age) values(#{id},#{name},#{age})insert into users(id,name,age) values(#{id},#{name},#{age})

mapper接口:

package org.example.mapper;import org.example.entity.MyUser;
import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapper
public interface UserMapper
{public List findAll();void saveUser(MyUser user);int saveUser2(MyUser user);}

dao接口:

package org.example.service;import org.example.entity.MyUser;import java.util.List;public interface UserService
{List findAll();void saveUser(MyUser user);int saveUser2(MyUser user);}

实现dao接口:

package org.example.service.Impl;import org.example.entity.MyUser;
import org.example.mapper.UserMapper;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UserServiceImpl implements UserService
{@AutowiredUserMapper userMapper;@Overridepublic List findAll(){return userMapper.findAll();}@Overridepublic void saveUser(MyUser user){userMapper.saveUser(user);}public int saveUser2(MyUser user){return userMapper.saveUser2(user);}
}

控制器:

package org.example.controller;import java.util.List;
import org.example.entity.MyUser;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class MyUserController
{@Autowiredprivate UserService userService;@GetMapping(value = "/1")public List home1(){// 查询所有网站List users = userService.findAll();for (MyUser user : users){System.out.println(user.getId()+" "+user.getName()+" "+user.getAge());}return users;}@GetMapping(value = "/2")public void home2(){MyUser user2 = new MyUser();user2.setName("TTT");user2.setAge(7878);userService.saveUser(user2);}@GetMapping(value = "/3")public int home3(){MyUser user2 = new MyUser();user2.setName("RRR");user2.setAge(6666);int a = userService.saveUser2(user2);System.out.println(a);return a;}}

之前数据如下:

运行后如下:

 


推荐阅读
author-avatar
rge4688618
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有