SpringBoot整合mybatis

20 篇文章 0 订阅
9 篇文章 0 订阅

1)、导入spring-mybatis的依赖

<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>

2)、在properties文件获取配置中写有关数据库的依赖‘

spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/maven?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#整合mybatis  起别名
mybatis.type-aliases-package=com.hu.springboot.domain
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

3)、测试一下是否成功, springboot中有一个测试方法

@SpringBootTest
class SpringBootMybatis02ApplicationTests {

    @Autowired
    DataSource dataSource;
    @Test
    void contextLoads() throws SQLException {
        System.out.println(dataSource.getClass());
        System.out.println(dataSource.getConnection());

    }

}

在这里插入图片描述

这里测试成功
然后写一个实体类

public class User {
    private int id;
    private String username;
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int 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;
    }
}

创建一个UserMapper 进行CRUD操作

@Mapper
//这个注解表示了这是一个mybatis的mapper类,注意这个很重要
@Repository
public interface UserMapper {
    List<User> queryUserList();
    User queryUserById(Integer id);
    int addUser(User user);
    int updateUser(User user);
    int deleteUser(Integer id);
}

service层与UserMapper一样的所以就不列出来

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.hu.springboot.mapper.UserMapper">
    <select id="queryUserList" resultType="User">
            select * from users;
    </select>

    <select id="queryUserById" resultType="User">
        select * from users where id = #{id}
    </select>

    <insert id="addUser" parameterType="User">
        insert into users (id,username,password) values (#{id}, #{username},#{password});
    </insert>
    <update id="updateUser" parameterType="User">
        update users set username = #{username},password=#{password} where id = #{id}
    </update>
    <delete id="deleteUser" parameterType="int">
        delete from users where id = #{id}
    </delete>
</mapper>

接下来就是控制层

@RestController
public class UserController {
    @Autowired
    private UserServiceImpl userService;
    @GetMapping("/queryList")
    public List<User> queryList(){
        List<User> users = userService.queryUserList();

        return users;
    }
    //根据id选择用户
    @GetMapping("/selectUserById")
    public String selectUserById(){
        User user = userService.queryUserById(5);
        System.out.println(user);
        return "ok";
    }
    //添加一个用户
    @GetMapping("/addUser")
    public String addUser(){
        User user = new User();
        user.setId(33);
        user.setUsername("zhangsan");
        user.setPassword("123456");
        userService.addUser(user);
        return "ok";
    }
    //修改一个用户
    @GetMapping("/updateUser")
    public String updateUser(){
        User user = new User();
        user.setId(50);
        user.setUsername("zhangsan");
        user.setPassword("654321");
        userService.updateUser(user);
        return "ok";
    }
    //根据id删除用户
    @GetMapping("/deleteUser")
    public String deleteUser(){
        userService.deleteUser(50);
        return "ok";
    }

}

运行结果:
在这里插入图片描述
CRUD都是可以执行的 ,图就略了

springboot的三层架构比ssm的要简单很多,可以省略大多数配置文件

注意的是springboot整合mybatis的时候映射要配置好,就是namespace和每个sql语句的id要与前面UserMapper要一一对应

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值