鄂尔多斯预应力钢绞线价格 SpringBoot中使用MyBatis入门笔记

新闻资讯 2025-12-26 12:00:20 115
钢绞线

第一步:准备数据库以及数据略过安装数据库操作以及创建数据库操作创建tb_user表初始化数据第二步:准备Maven工程1、使用 idea 创建一个 maven 工程鄂尔多斯预应力钢绞线价格

工程名:我使用的是springandmybatis

选择maven

选择JDK17或者以上的版本

groupid:也就是包名,我写的是com.test

2、添加依赖,在 pom.xml 文件中添加依赖(注意几个添加注释的地方),添加后记得同步 4.0.0org.springframework.bootspring-boot-starter-parent3.5.4com.testspringandmybatis1.0-SNAPSHOT1717UTF-8org.springframework.bootspring-boot-starter-weborg.mybatis.spring.bootmybatis-spring-boot-starter3.0.4mysqlmysql-connector-java8.0.33 #后端3.添加配置文件(在 resources 目录下,添加 application.yaml 文件)

spring:

datasource:

url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useSSL=false&autoReconnect=true

username: root

password: root

driver-class-name: com.mysql.cj.jdbc.Driver

# 这种方式比较简单

mybatis: mapper-locations: classpath:mapper/*.xml

第三步:编写代码编写UserModel类,预应力钢绞线其作用是对应数据库表 tb_userpackage com.test.model;

public class UserModel { private Integer id; private String username; private String password; private String gender; private String addr;

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; }

public String getPassword { return password; }

public void setPassword(String password) { this.password = password; }

public String getGender { return gender; }

public void setGender(String gender) { this.gender = gender; }

public String getAddr { return addr; }

public void setAddr(String addr) { this.addr = addr; } }

编写UserMapper接口(注意:在类上面添加了 @Repository 注解)package com.test.mapper;

import com.test.model.UserModel; import org.springframework.stereotype.Repository;

import java.util.List;

@Repository public interface UserMapper { /** * 查询所有用户 * @return */ List selectAll;

/** * 插入用户 * @param userModel * @return */ Integer insertUser(UserModel userModel); }

1.

在 resources 目录中创建 mapper 目录,用来专门存放 mapper 的 xml 文件,这里暂时先创建 userMapper.xml INSERT INTO tb_user(username, password, gender, addr) VALUES(#{username}, #{password}, #{gender}, #{addr}) SELECT * FROM tb_user 编写UserService类(注意:在类上面添加了 @Service 注解)package com.test.service;

手机号码:13302071130

import com.test.mapper.UserMapper; import com.test.model.UserModel; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service public class UserService {

private final UserMapper userMapper;

@Autowired // 构造器注入推荐 public UserService(UserMapper userMapper) { this.userMapper = userMapper; }

public List selectAll { return userMapper.selectAll; }

public int insertUser(UserModel user) { return userMapper.insertUser(user); } }

编写UserController类(注意:在类上面添加了 @RestController 注解)package com.test.controller;

import com.test.model.UserModel; import com.test.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController;

@RestController @RequestMapping("/user") public class UserController {

@Autowired private UserService userService;

@GetMapping(path="/all") public @ResponseBody Iterable getAllUsers { return userService.selectAll; }

 

@GetMapping(path="/add") public @ResponseBody Iterable addUser { String username = "张三"; String password = "123456"; String gender="1"; String addr = "上海"; UserModel userModel = new UserModel; userModel.setUsername(username); userModel.setPassword(password); userModel.setGender(gender); userModel.setAddr(addr); userService.insertUser(userModel); return userService.selectAll; } }

编写程序入口类(注意:在类名上添加了 @SpringBootApplication 和 @MapperScan("com.test.mapper") 两个注解)package com.test;

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

@SpringBootApplication @MapperScan("com.test.mapper") public class Main { public static void main(String[] args) { SpringApplication.run(Main.class, args); } }

第四步:测试运行入口类,如果没有错误,就可以使用浏览器请求 UserController 中定义的两个接口

查询所有插入数据鄂尔多斯预应力钢绞线价格