IDEA2020+springboot+mybatis实现helloworld和简单的crud

博主分享了一篇关于使用SpringBoot搭建简单CRUD应用的教程。文中通过一步步指导,从创建项目到配置数据库,再到实现Controller、Service、Mapper及实体类,展示了如何进行数据库操作。提醒读者注意博客质量,避免误导初学者。
摘要由CSDN通过智能技术生成

碎碎念

       这几天甲方把服务器关了,所以暂时手头没有什么任务。想起来springboot好久没用了就想去网上随便找篇博客看下,自己整个demo玩一玩。我也不知道为啥好几篇博客都是啥玩意,有好几篇博客是抄的一样的找了一篇好评比较多的,发现他竟然用控制层直接去调用持久层。关键是下面还一堆人谢谢他整理的,算了我还是凭着自己的记忆自己搭建把。希望大家整理博客是为了学东西不是为了所谓的点击量啥的,这样乱整理也会误导初学者,大家按照我这篇博客用IDEA2020去搭建springboot实现简单的CRUD反正可以跑通,不会耽误时间。

好了正文开始:
我直接上图如果你还看不懂做不会,那可能你确实不适合当程序员
在这里插入图片描述
在这里插入图片描述
我一般用springboot脚手架创建boot项目,点击next选择好包名和你的java版本
在这里插入图片描述
而后选择好你需要引入依赖和boot版本
这里我选的是2.3.9,不过好像20版本idea给的是最新版2.4的
在这里插入图片描述在这里插入图片描述
最后选择一下目录,和项目名
首先给大家看下我的项目结构
在这里插入图片描述
然后再给各位看一下pom.xml的完整代码:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zlt</groupId>
    <artifactId>springbootdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springbootdemo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

而后我就把各位当成springboot初学者
首先弄一个helloworld出来
HelloWorld.java的代码如下

package com.zlt.springbootdemo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorld {

    @RequestMapping("/hello")
    public String helloworld(){
        return "Hello Springboot";
    }

}

访问结果如下:springboot的端口号是可以改的直接在application.properties或者application.yml中改,我的properties配置如下:

server.port=8085
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driver-class-name = com.mysql.jdbc.Driver

mybatis.type-aliases-package = org.spring.springboot.damain
mybatis.mapper-locations = classpath:mapper/*.xml

在这里插入图片描述
而后是UserController:

package com.zlt.springbootdemo.controller;

import com.zlt.springbootdemo.entity.User;
import com.zlt.springbootdemo.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping(value="/select")
    public String getUserinfo(@Param("id") int id){
        String userinfo = userService.selectByPrimaryKey(id);
        return userinfo;
    }

    @ResponseBody
    @RequestMapping(value="/delete")
    public int deleteUserinfo(@Param("id") int id){
        int userinfo = userService.deleteUserById(id);
        return userinfo;
    }

    @ResponseBody
    @RequestMapping(value="/add")
    public int insertUserinfo(User user){
        int userinfo = userService.insert(user);
        return userinfo;
    }

    @ResponseBody
    @RequestMapping(value="/update")
    public int updateUserinfo(User user){
        int userinfo = userService.updatePrimaryKey(user);
        return userinfo;
    }


}

而后是UserMapper:

package com.zlt.springbootdemo.dao;


import com.zlt.springbootdemo.entity.User;

public interface UserMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
}

而后是User实体类:

package com.zlt.springbootdemo.entity;

public class User {
    private Integer id;

    private String username;

    private Integer userage;

    private String useraddress;

    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }

    public Integer getUserage() {
        return userage;
    }

    public void setUserage(Integer userage) {
        this.userage = userage;
    }

    public String getUseraddress() {
        return useraddress;
    }

    public void setUseraddress(String useraddress) {
        this.useraddress = useraddress == null ? null : useraddress.trim();
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", userage=" + userage +
                ", useraddress='" + useraddress + '\'' +
                '}';
    }
}

UserService实现类:

package com.zlt.springbootdemo.service;

import com.zlt.springbootdemo.entity.User;

public interface UserService {

    public String selectByPrimaryKey(Integer id);

    public int deleteUserById(Integer id);

    public int insert(User user);

    public int updatePrimaryKey(User user);
}

而后是UserServiceImpl实现类:

package com.zlt.springbootdemo.service.impl;

import com.zlt.springbootdemo.dao.UserMapper;
import com.zlt.springbootdemo.entity.User;
import com.zlt.springbootdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public String selectByPrimaryKey(Integer id) {
        User user = userMapper.selectByPrimaryKey(id);
        return user.toString();
    }

    @Override
    public int deleteUserById(Integer id) {
        int isdelete = userMapper.deleteByPrimaryKey(id);
        return isdelete;
    }

    @Override
    public int insert(User user) {
        int isinsert = userMapper.insertSelective(user);
        return isinsert;
    }

    @Override
    public int updatePrimaryKey(User user) {
        int updateinfo = userMapper.updateByPrimaryKey(user);
        return updateinfo;
    }
}

而后是启动类:

package com.zlt.springbootdemo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.zlt.springbootdemo.dao")
public class SpringbootdemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootdemoApplication.class, args);
    }

}

而后是UserMapper.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.zlt.springbootdemo.dao.UserMapper">
  <resultMap id="BaseResultMap" type="com.zlt.springbootdemo.entity.User">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="userAge" jdbcType="INTEGER" property="userage" />
    <result column="userAddress" jdbcType="VARCHAR" property="useraddress" />
  </resultMap>
  <sql id="Base_Column_List">
    id, username, userAge, userAddress
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from user
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from user
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.zlt.springbootdemo.entity.User">
    insert into user (id, username, userAge, 
      userAddress)
    values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{userage,jdbcType=INTEGER}, 
      #{useraddress,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.zlt.springbootdemo.entity.User">
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="username != null">
        username,
      </if>
      <if test="userage != null">
        userAge,
      </if>
      <if test="useraddress != null">
        userAddress,
      </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="userage != null">
        #{userage,jdbcType=INTEGER},
      </if>
      <if test="useraddress != null">
        #{useraddress,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.zlt.springbootdemo.entity.User">
    update user
    <set>
      <if test="username != null">
        username = #{username,jdbcType=VARCHAR},
      </if>
      <if test="userage != null">
        userAge = #{userage,jdbcType=INTEGER},
      </if>
      <if test="useraddress != null">
        userAddress = #{useraddress,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.zlt.springbootdemo.entity.User">
    update user
    set username = #{username,jdbcType=VARCHAR},
      userAge = #{userage,jdbcType=INTEGER},
      userAddress = #{useraddress,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

而后是我的数据库结构
在这里插入图片描述
我这篇springboot不是什么全网最详细怎么怎么样的我只能告诉你是可以跑通的,希望大家不忘初心好好学习,不要和有些博客一样搞些花里胡哨

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值