springboot入门系列教程|第六篇:springboot结合mybatis实现增删改查

版权声明:转载请注明地址,谢谢配合 https://blog.csdn.net/pulong0748/article/details/82467164

前言

     上一篇介绍了关于thymeleaf的使用,那么接下来我们看看springboot结合mybatis的使用。
     准备工作:

jdk1.8
IDEA
maven 3.3.9

     初始化项目:







     看下我的文档基本结构:


     开始编写代码:

      配置文件

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/spring_boot_study
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource
mybatis:
  mapper-locations: classpath*:mapper/*.xml  
server:
  port: 8000

      controller

@RestController
public class MybatisController {

    @Autowired
    private UserService userService;

    @GetMapping("/user/get/{id}")
    public User user(@PathVariable int id) {
        return userService.findUserById(id);
    }

    @PostMapping("/user/add")
    public int addUser(@RequestBody User user){
        return userService.addUser(user);
    }

    @PostMapping("/user/update")
    public int updateUser(@RequestBody User user){
        return userService.updateUser(user);
    }

    @GetMapping("/user/delete/{id}")
    public int deleteUser(@PathVariable int id){
        return userService.deleteById(id);
    }
}

      entity

public class User {

    private int id;

    private String name;

    private int age;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

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

}

      mapper

public interface UserMapper {
    User findUserById(int id);

    int  addUser(User user);

    int  updateUser(User user);

    int  deleteById(int id);

}

     serviceImpl

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private  UserMapper userMapper;
    @Override
    public User findUserById(int id) {
        return userMapper.findUserById(id);
    }

    @Override
    public int addUser(User user) {
        return userMapper.addUser(user);
    }

    @Override
    public int updateUser(User user) {
        return  userMapper.updateUser(user);
    }

    @Override
    public int deleteById(int id) {
        return userMapper.deleteById(id);
    }
}

      SpringBootMybatisApplication

@SpringBootApplication
//这步很关键一定要扫描mapper接口所在的包
@MapperScan("com.coderv.springbootmybatis.mapper")
public class SpringBootMybatisApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootMybatisApplication.class, args);
    }
}

     druid连接池依赖


        <!-- alibaba的druid数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.0</version>
        </dependency>

     Mapper文件

<mapper namespace="com.coderv.springbootmybatis.mapper.UserMapper">

    <resultMap id="UserResultMap" type="com.coderv.springbootmybatis.entity.User">

        <id property="id" column="id"/>
        <result property="age" column="age"/>
        <result property="name" column="name" javaType="java.lang.String"/>
    </resultMap>

    <select id="findUserById" parameterType="int" resultMap="UserResultMap">
        SELECT id,name,age FROM user WHERE id = #{id}
    </select>


    <insert id="addUser" parameterType="com.coderv.springbootmybatis.entity.User" useGeneratedKeys="true" keyColumn="id">
        insert into  user
        (id,age,name)
        VALUES (
        #{id},
        #{age},
        #{name}
        )
    </insert>

    <update id="updateUser" parameterType="com.coderv.springbootmybatis.entity.User">
        update user  set
        name=#{name} WHERE id=#{id}
    </update>

    <delete id="deleteById" parameterType="int" >
        delete from user where  id=#{id}
    </delete>


</mapper>

     小建议:
     1:测试的时候都用postman,就别搞什么页面了,麻烦。
     2: 自己动手搞一遍,你会发现你会遇到异常,也许是粗心,也许是其他问题

     源码和数据库放在github上:spring-boot-mybatis

     r如果有小伙伴觉得我写的不错的话可以支持一下我的公众号哦:coldStone,主要会分享分布式系统相关的一系列技术,目前会推出springboot、springcloud和docker系列教程,后期会有关于中间件以及各个层面的性能优化的文章,同时还会分享一些赚钱理财的小套路哦,欢迎大家来支持,一起学习成长,程序员不仅仅是搬瓦工!


猜你喜欢

转载自blog.csdn.net/pulong0748/article/details/82467164