首页 > 数据库 >spring boot——spring boot的基本配置——spring boot整合mybatis——本地实例运行——MyBatis动态SQL——MyBatis choose、when和other

spring boot——spring boot的基本配置——spring boot整合mybatis——本地实例运行——MyBatis动态SQL——MyBatis choose、when和other

时间:2023-01-06 23:35:13浏览次数:48  
标签:name int spring boot user MyUser MyBatis List 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>

    <!--插入数据后返回自增id-->
    <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>

    <!--根据id删除数据-->
    <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>

    <!--根据id查询数据-->
    <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>

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


    <!--choose-when-otherwise 标签实现-->
    <select id="findUserByWhere" parameterType="org.example.entity.MyUser" resultType="org.example.entity.MyUser">
        select * from USERs where 1=1
        <choose>
            <when test="name != null and name !=''">
                AND name LIKE CONCAT('%',#{name},'%')
            </when>
            <when test="age != null and age !=''">
                AND age > #{age}
            </when>
            <otherwise>
                AND id is not null
            </otherwise>
        </choose>
    </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);

    List<MyUser> findUserByBetweenAge(MyUser user);

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

    List<MyUser> findUserByBetweenAge(MyUser user);

    List<MyUser> findUserByWhere(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);
    }

    public List<MyUser> findUserByBetweenAge(MyUser user)
    {
        return userMapper.findUserByBetweenAge(user);
    }

    public List<MyUser> findUserByWhere(MyUser user)
    {
        return userMapper.findUserByWhere(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;

    }


    @GetMapping(value = "/14")
    public List<MyUser> findUserByBetweenAge()
    {
        MyUser user2 = new MyUser();
        user2.setAge(500000);


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

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


        return users;

    }


    @GetMapping(value = "/15")
    public List<MyUser> findUserByWhere()
    {
        MyUser user2 = new MyUser();

//        user2.setName("ac");

        user2.setAge(800);


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

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


        return users;

    }

}

 

 

 

 

 

 

 

 

执行:

 

 

 

 

 

 

 

=====================================================================

 

解释:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

相关文章

  • SpringBoot @Cacheable 缓存不生效的问题
    背景Springboot+CaffeineCache+使用@Cacheable注解请求查询一个方法,因为数据变化频率低,查询频率高,于是使用缓存,并使用注解。但发现用了@Cacheable这个注解,发现并......
  • springboot注解
    ConfigurationProperties常用于yml配置文件binding,支持松散绑定prefix参数可以指定配置文件中某一个节点,对其子节点属性自动绑定松散绑定支持中划线、驼峰、大写、下......
  • Caused by: org.yaml.snakeyaml.error.YAMLException|springboot yml配置问题
    参考https://blog.csdn.net/loler15/article/details/114987020原因:将propriate转yml配置时要删除重建......
  • EL1008E: Property or field 'config' cannot be found on object of type 'org.sprin
    EL1008E:Propertyorfield'config'cannotbefoundonobjectoftype'org.springframework.beans.factory.config.BeanExpressionContext' 1、报错信息将Sprin......
  • 基于SpringBoot 使用 Flink 收发Kafka消息
    前言这周学习下Flink相关的知识,学习到一个读写Kafka消息的示例,自己动手实践了一下,别人示例使用的是普通的JavaMain方法,没有用到springboot.我们在实际工作中会使用spr......
  • mybatis中in的坑
    在mybatis的in查询分为三种传入参数1、string字符串传入,以逗号分割:优点:简单方便,高效,缺点:不能防止sql注入,注意,传入的参数为字符串,如('A123','B123','C123')需要提前把数......
  • Spring Boot笔记--Spring Boot相关介绍+快速入门
    相关介绍简化了Spring开发,避免了Spring开发的繁琐过程提供了自动配置、起步依赖、辅助功能快速入门结果呈现:相关过程:helloController.javapackageorg.example......
  • SpringBoot笔记--Failed to read candidate component class报错的解决
    问题解决形成该错误的原因就是Spring依赖和我们所使用的JDK版本不匹配更详细一点的话,要解决这个问题,我们就需要升高Spring版本或者降低JDK版本即可解决否则不能够在网页......
  • SpringCloud-Stream整合RabbitMQ
    我们知道,当微服务越来越来多的时候,仅仅是feign的http调用方式已经满足不了我们的使用场景了。这个时候系统就需要接入消息中间件了。相比较于传统的Spring项目、SpringBoot......
  • bootstrap下载进度条
    原来的web应用中文件下载都是打开一个新的窗口,文件自动下载后需手动关闭新打开的窗口,且没有下载进度显示。<script>window.open(downloadUrl,'__blank');</script>......