首页 > 数据库 >spring boot——spring boot的基本配置——spring boot整合mybatis——本地实例运行——MyBatis动态SQL——MyBatis if标签——示例001

spring boot——spring boot的基本配置——spring boot整合mybatis——本地实例运行——MyBatis动态SQL——MyBatis if标签——示例001

时间:2023-01-05 23:24:03浏览次数:55  
标签:name int spring boot id user MyUser MyBatis public

 

 

 

 

 

 

 

 

pojo类:

package org.example.entity;

public class MyUser
{
    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;
    }

    @Override
    public String toString() {
        return "MyUser{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

mapper文件:

<?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="org.example.mapper.UserMapper">
    <select id="findAll" resultType="org.example.entity.MyUser">
        select * from users
    </select>

    <insert id="saveUser" parameterType="org.example.entity.MyUser">
        insert into users(id,name,age) values(#{id},#{name},#{age})
    </insert>

    <insert id="saveUser2" parameterType="org.example.entity.MyUser">
        insert into users(id,name,age) values(#{id},#{name},#{age})
    </insert>

    <insert id="saveInsertUser" parameterType="org.example.entity.MyUser">
        <selectKey keyProperty="id" keyColumn="id" order="AFTER" resultType="int">
            SELECT LAST_INSERT_ID();
        </selectKey>
        insert into users(id,name,age) values(#{id},#{name},#{age})
    </insert>

    <update id="updateUser" parameterType="org.example.entity.MyUser">
        update users set name = #{name},age = #{age} where id = #{id}
    </update>

    <delete id="deleteUser" parameterType="java.lang.Integer">
        delete from users where id = #{id}
    </delete>

    <delete id="deleteUserByName" parameterType="java.lang.String">
        delete from users where name = #{name}
    </delete>

    <select id="findById" parameterType="java.lang.Integer" resultType="org.example.entity.MyUser">
        select * from USERs where id = #{id}
    </select>

    <select id="findByName" parameterType="java.lang.String" resultType="org.example.entity.MyUser">
        select * from USERs where name = #{name}
    </select>

    <select id="findByNameMf" parameterType="java.lang.String" resultType="org.example.entity.MyUser">
        select * from USERs where name like #{name};
    </select>

    <select id="findByNameMf2" parameterType="java.lang.String" resultType="org.example.entity.MyUser">
        select * from USERs where name like '%${value}%';
    </select>

    <select id="findTotal" resultType="int">
        select count(id) from USERs;
    </select>

    <!--根据条件查询-->
    <select id="findUserByCondition" parameterType="org.example.entity.MyUser" resultType="org.example.entity.MyUser">
        select * from USERs where 1=1
        <if test="name != null">
            and name = #{name}
        </if>
        <if test="id != null">
            or id = #{id}
        </if>
        <if test="age != null">
            or age = #{age}
        </if>
    </select>

</mapper>

 

 

 

 

 

 

 

 

 

 

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<MyUser> findAll();

    void saveUser(MyUser user);

    int saveUser2(MyUser user);

    int saveInsertUser(MyUser user);

    int updateUser(MyUser user);

    int deleteUser(Integer id);

    int deleteUserByName(String name);

    MyUser findById(Integer id);

    List<MyUser> findByName(String name);

    List<MyUser> findByNameMf(String name);

    List<MyUser> findByNameMf2(String name);

    int findTotal();

    /**
     *查询条件可能是id、用户名,年龄;也可能都有,也可能都没有
     */
    List<MyUser> findUserByCondition(MyUser user);

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

service接口:

package org.example.service;

import org.example.entity.MyUser;
import java.util.List;

public interface UserService
{
    List<MyUser> findAll();

    void saveUser(MyUser user);

    int saveUser2(MyUser user);

    int saveInsertUser(MyUser user);

    int updateUser(MyUser user);

    int deleteUser(Integer id);

    int deleteUserByName(String name);

    MyUser findById(Integer id);

    List<MyUser> findByName(String name);

    List<MyUser> findByNameMf(String name);

    List<MyUser> findByNameMf2(String name);

    int findTotal();

    List<MyUser> findUserByCondition(MyUser user);

}

 

 

 

 

 

 

 

实现service接口:

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
{
    @Autowired
    UserMapper userMapper;

    @Override
    public List<MyUser> findAll()
    {
        return userMapper.findAll();
    }

    @Override
    public void saveUser(MyUser user)
    {
        userMapper.saveUser(user);
    }

    public int saveUser2(MyUser user)
    {
        return userMapper.saveUser2(user);
    }

    public int saveInsertUser(MyUser user)
    {

        return userMapper.saveInsertUser(user);
    }

    public int updateUser(MyUser user)
    {
        return userMapper.updateUser(user);
    }


    public int deleteUser(Integer id)
    {
        return userMapper.deleteUser(id);
    }

    public int deleteUserByName(String name)
    {
        return userMapper.deleteUserByName(name);
    }

    public MyUser findById(Integer id)
    {
        return userMapper.findById(id);
    }

    public List<MyUser> findByName(String name)
    {
        return userMapper.findByName(name);
    }

    public List<MyUser> findByNameMf(String name)
    {
        return userMapper.findByNameMf(name);
    }

    public List<MyUser> findByNameMf2(String name)
    {
        return userMapper.findByNameMf2(name);
    }

    public int findTotal()
    {
        return userMapper.findTotal();
    }

    public List<MyUser> findUserByCondition(MyUser user)
    {

        return userMapper.findUserByCondition(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
{
    @Autowired
    private UserService userService;

    @GetMapping(value = "/1")
    public List<MyUser> home1()
    {
        // 查询所有网站
        List<MyUser> 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;

    }

    @GetMapping(value = "/4")
    public int home4()
    {
        MyUser user2 = new MyUser();
        user2.setName("ccc");
        user2.setAge(321123);

        userService.saveInsertUser(user2);

        int a = user2.getId();

        System.out.println(a);

        System.out.println(user2);

        return a;

    }

    @GetMapping(value = "/5")
    public int updateUser()
    {
        MyUser user2 = new MyUser();
        user2.setId(1013);
        user2.setName("sprint-boot-1013");
        user2.setAge(199999);

        int a = userService.updateUser(user2);

        System.out.println(a);

        return a;

    }


    @GetMapping(value = "/6")
    public int deleteUser()
    {
        int a = userService.deleteUser(1012);

        System.out.println(a);

        return a;

    }

    @GetMapping(value = "/7")
    public int deleteUserByName()
    {
        int a = userService.deleteUserByName("sprint-boot-1013");

        System.out.println(a);

        return a;

    }

    @GetMapping(value = "/8")
    public void findById()
    {
        MyUser user001 = userService.findById(1011);

        System.out.println(user001);

    }

    @GetMapping(value = "/9")
    public List<MyUser> findByName()
    {
        List<MyUser> users= userService.findByName("aaa");

        for (MyUser user : users)
        {
            System.out.println(user.getId()+"  "+user.getName()+"  "+user.getAge());
        }

        return users;

    }

    @GetMapping(value = "/10")
    public List<MyUser> findByNameMf()
    {
        List<MyUser> users= userService.findByNameMf("%ae%");

        for (MyUser user : users)
        {
            System.out.println(user.getId()+"  "+user.getName()+"  "+user.getAge());
        }

        return users;

    }


    @GetMapping(value = "/11")
    public List<MyUser> findByNameMf2()
    {
        List<MyUser> users= userService.findByNameMf2("ae");

        for (MyUser user : users)
        {
            System.out.println(user.getId()+"  "+user.getName()+"  "+user.getAge());
        }

        return users;

    }

    @GetMapping(value = "/12")
    public int findTotal()
    {
        int a = userService.findTotal();

        return a;

    }


    @GetMapping(value = "/13")
    public List<MyUser> findUserByCondition()
    {
        MyUser user2 = new MyUser();
        user2.setName("aaa");
        user2.setAge(111821);


        List<MyUser> users= userService.findUserByCondition(user2);

        for (MyUser user : users)
        {
            System.out.println(user.getId()+"  "+user.getName()+"  "+user.getAge());
        }


        return users;

    }

}

 

 

 

 

 

 

 

 

 

执行前:

 

 

 

 

 

 

 

 

 

 

 

执行:

 

 

 

 

 

 

标签:name,int,spring,boot,id,user,MyUser,MyBatis,public
From: https://www.cnblogs.com/xiaobaibailongma/p/17029104.html

相关文章

  • 尚硅谷SpringCloud(H版&alibaba)
    0.SpringCloud升级,部分组件停用:Eureka停用,可以使用zk作为服务注册中心服务调用,Ribbon准备停更,代替为LoadBalanceFeign改为OpenFeignHystrix停更,改为resilence4j或者......
  • Java开发学习(五十)----MyBatisPlus快速开发之代码生成器解析
    1、代码生成器原理分析造句:我们可以往空白内容进行填词造句,比如:在比如:观察我们之前写的代码,会发现其中也会有很多重复内容,比如:那我们就想,如果我想做一个Book模块......
  • Spring Security – RequestRejectedException
    概述SpringSecurity提供了RequestRejectedHandler来处理当请求被拒绝时候如何处理,在没有进行配置的情况下,默认是使用DefaultRequestRejectedHandler直接将异常进行......
  • mybatis使用postgresql中的jsonb数据类型
    最近新开发的一个功能使用到postgresql中的jsonb数据类型。架构师可能考虑到这种数据格式更加便于存储json格式的数据,因此考虑使用这种数据类型。自己以前未曾使用过这......
  • 同事说不会在SpringBoot中集成日志记录操作我向他推荐了这篇文章
    SpringBoot中记录操作日志记一次SpringBoot中记录管理员的操作日志,记录管理员对用户、管理员、分类、视频等模块的操作,如:删除管理员、用户、视频、修改密码、信息等等,都......
  • 实现简单的csv文件上传和bootstrap表格的下载
    一、写一个简单的页面并发送文件引入bootstrap.js,jQuery.js等,具体的网页就不细写了,很简单。加入input框,button控件,进度条。如下:<liclass="list-group-item"......
  • 在SpringCloud中使用REST服务时的报错
    在SpringCloud中使用REST服务时使用前需要先在配置类中注入RestTemplate的Bean然后再使用自动装配即可@AutowiredprivateRestTemplaterestTemplate;问题......
  • SpringMVC实现浏览器端大文件分片上传
    ​ 最近遇见一个需要上传超大大文件的需求,调研了七牛和腾讯云的切片分段上传功能,因此在此整理前端大文件上传相关功能的实现。在某些业务中,大文件上传是一个比较重要的......
  • springboot 资源分离 打包
    <build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</a......
  • spring中Bean生命周期
    1.普通java对象创建过程举例:BusinessServicebusinessService=newBusinessService();编译:将.java文件编译成class文件加载:等到类需要被初始化时(比如new)class文件被虚......