您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章给大家分享的是有关SpringBoot项目中怎么整合MyBatis的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
<!-- mybatis整合 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
spring: datasource: username: root password: 123456 url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.cj.jdbc.Driver
package com.example.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String name;
private String pwd;
}
为了偷懒,导入了lombok
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
package com.example.mapper;
import com.example.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface UserMapper {
//查询用户的全部信息
List<User> getUserList();
//select 找出id=1的用户
User getUserById(int id);
//insert 增加一个用户
int insertUser(User user);
//delete 删除id=4的用户
int deleteUser(int id);
//update 将id=2的用户名字改为小龙
int updateUser(User user);
}
<?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.example.mapper.UserMapper">
<select id="getUserList" resultType="user">
select *
from mybatis.user
</select>
<select id="getUserById" resultType="user" parameterType="int">
select *
from mybatis.user
where id = #{id};
</select>
<insert id="insertUser" parameterType="user">
insert into mybatis.user (id, name, pwd)
values (#{id}, #{name}, #{pwd});
</insert>
<delete id="deleteUser" parameterType="int">
delete
from mybatis.user
where id = #{id};
</delete>
<update id="updateUser" parameterType="user">
update mybatis.user
set name = #{name},
pwd = #{pwd}
where id = #{id};
</update>
</mapper>
这里我们用了别名而且我们把这个mapper.xml文件放在了resources目录下,所以我们要去application.yml配置一下
mybatis:
type-aliases-package: com.example.pojo
mapper-locations: classpath:mybatis/mapper/*.xml
mapper.xml文件编写的位置:
package com.example.controller;
import com.example.mapper.UserMapper;
import com.example.pojo.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/getUserList")
public List<User> getUserList(){
return userMapper.getUserList();
}
@GetMapping("/getUserById/{id}")
public User getUserById(@PathVariable("id") int id){
return userMapper.getUserById(id);
}
@GetMapping("/insertUser")
public String insertUser(){
userMapper.insertUser(new User(5,"xiaoming","111"));
return "ok";
}
@GetMapping("/deleteUser")
public String deleteUser(){
userMapper.deleteUser(5);
return "ok";
}
@GetMapping("/updateUser")
public String updateUser(){
userMapper.updateUser(new User(5,"xx","111"));
return "ok";
}
}
感谢各位的阅读!关于“SpringBoot项目中怎么整合MyBatis”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。