介绍MyBatis
MyBatis 通过XML描述符或注解将对象与存储过程或SQL语句进行映射,并提供了普通SQL查询、存储过程和高级映射等操作方式,使得操作数据库变得非常方便。
springboot3 安装MyBatis
pom.xml文件中添加如下依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.1</version>
</dependency>
配置
application.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/lab
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*.xml #mapper文件位置
configuration:
map-underscore-to-camel-case: true #使用驼峰命名法转换字段
在resources下创建文件夹mapper(对应配置mapper-locations)下的xml文件
创建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.huisheng.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.huisheng.entity.UserEntity">
<result column="id" jdbcType="INTEGER" property="id" />
<result column="user_name" jdbcType="VARCHAR" property="userName" />
<result column="passWord" jdbcType="VARCHAR" property="passWord" />
</resultMap>
<select id="Sel" resultType="com.huisheng.entity.UserEntity">
select * from lab_user where id = #{id}
</select>
<!-- 插入操作 -->
<insert id="save" parameterType="UserEntity">
INSERT INTO lab_user(username, password)
VALUES (#{username}, #{password})
</insert>
<!-- 更新操作 -->
<update id="update" parameterType="UserEntity">
UPDATE lab_user SET
username = #{username},
password = #{password}
WHERE id = #{id}
</update>
<!-- 删除操作 -->
<delete id="deleteById" parameterType="Long">
DELETE FROM user
WHERE id = #{id}
</delete>
</mapper>
注意对应 namespace=“com.huisheng.mapper.UserMapper”
需要再建一个文件夹mapper 存放
在文件夹下创建UserMapper接口,
package com.huisheng.mapper;
import com.huisheng.entity.UserEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserMapper {
@Select("SELECT * FROM lab_user")
List<UserEntity> getUsers();
UserEntity Sel(int id);
}
注意:@Mapper 如果不加的话需要 在入口文件
@MapperScan(“com.huisheng.mapper”),如果在入口文件加了就不需要加@Mapper注解
@Mapper注解标识该接口为Mapper接口。MyBatis会自动扫描这些接口并创建对应的实现类。
package com.huisheng;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.huisheng.mapper")
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
还需要创建实体类,这里使用了lombok,这个可以自己的习惯是否使用lombok
实体类
package com.huisheng.entity;
import lombok.Data;
@Data
public class UserEntity extends BaseEntity{
private Integer id;
private String userName;
private String passWord;
}
service层
package com.huisheng.service;
import com.huisheng.entity.UserEntity;
import com.huisheng.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
UserMapper userMapper;
public List<UserEntity> getUsers(){
return userMapper.getUsers();
}
public UserEntity Sel(int id){
return userMapper.Sel(id);
}
}
controller
package com.huisheng.controller;
import com.huisheng.entity.UserEntity;
import com.huisheng.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Slf4j
@RestController
public class index {
@Autowired
private UserService userService;
@RequestMapping("/hello")
public String hello(@RequestParam(required = true) String name,Integer id) {
log.info("名称:"+name);
log.error("错误日志");
return "Hello 1000";
}
@GetMapping("/user_list")
public List<UserEntity> getUserList()
{
return userService.getUsers();
}
@PostMapping("/user")
public String user(UserEntity userEntity){
return userEntity.toString();
}
@GetMapping("/user/{user_id}")
public String getUserById(@PathVariable(name="user_id") Integer userId){
UserEntity userEntity=userService.Sel(userId);
return userEntity.toString();
}
}