mybatis

1.项目结构

2.application.yml

点击查看代码
spring:
  application:
    name: springboot-mybatis
  #数据库连接信息
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/web01
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
#mybatis相关配置
mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

3.SpringbootMybatisApplicationTests

点击查看代码
package com.itheima;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class SpringbootMybatisApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void testFindAll(){
    List<User> userList = userMapper.findAll();
    userList.forEach(System.out::println);
}



    @Test
    public void testDeleteById(){
    userMapper.deleteById(4);
    }


    @Test
    public void testInsert(){
        User user = new User(null, "lisi", "123", "李四", 2); // 确保参数数量和类型正确
        userMapper.insert(user);
    }

    @Test
    public void testUpdate(){
        User user = new User(6, "wangwu", "123", "王五", 2);
        userMapper.update(user);
    }

    @Test
    public void testFindByUsernameAndPassword(){
        User user = userMapper.findByUsernameAndPassword("diaochan", "123456");
        System.out.println(user);
    }
}

4.UserMapper

点击查看代码
package com.itheima.mapper;

import com.itheima.pojo.User;
import org.apache.ibatis.annotations.*;


import java.util.List;

@Mapper
public interface UserMapper {

//查询所有
    //@Select("select id, username, password, name, age from user")
    public List<User> findAll();
//根据ID删除数据
    @Delete("delete from user where id = #{id}")
    public void deleteById(Integer id);
    //添加一个用户
    @Insert("insert into user (username,password,name,age)values(#{username},#{password},#{name},#{age}) ")
    public void insert(User user);
    //根据ID更新用户信息
    @Update("update user set username = #{username},password = #{password},name = #{name},age = #{age} where id = #{id}")
    public void update(User user);
    //根据用户名和密码查询用户信息
    @Select("select * from user where username = #{username} and password = #{password}")
    public User findByUsernameAndPassword(String username, String password);
}

5.User

点击查看代码
package com.itheima.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data

@NoArgsConstructor
public class User {
    private Integer id;
    private String username;
    private String password;
    private String name;
    private Integer age;

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

    public User(Integer id, String username, String password, String name, Integer age) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.name = name;
        this.age = age;
    }
}

6.UserMapper.xml

点击查看代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.UserMapper">

    <select id="findAll" resultType="com.itheima.pojo.User">
        select id, username, password, name, age from user
    </select>

</mapper>
posted @   是好正义呀  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示