Spring Boot整合MyBatis连接 SQL Server(1)通过SQL注解的方式操作数据库

通过SQL注解的方式操作数据库

  1. 在pom.xml文件中添加依赖
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.1.4version>
        dependency>

        <dependency>
            <groupId>com.microsoft.sqlservergroupId>
            <artifactId>mssql-jdbcartifactId>
            <scope>runtimescope>
        dependency>
  1. application.properties 文件中添加以下内容
spring.datasource.url=jdbc:sqlserver://127.0.0.1:1433;DatabaseName=xxx //这里填自己的数据库名

spring.datasource.username=xxx
//这里填自己的登录名
spring.datasource.password=xxx
//这里填自己的密码
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver

logging.level.com.example.demo.repository=debug
//这里填自己的包路径
spring.jackson.serialization.indent-output=true
  1. SQL Server中建表
    Spring Boot整合MyBatis连接 SQL Server(1)通过SQL注解的方式操作数据库_第1张图片

  2. 创建entity包,在包中创建MyUser实体类

package com.example.demo.entity;

import lombok.Data;

@Data
public class MyUser {
	//成员变量名与数据库中表的字段要对应上
    private Integer uid;
    private String uname;
    private String usex;

}

  1. 创建repository包,包中创建MyUserRepository接口
package com.example.demo.repository;

import com.example.demo.entity.MyUser;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MyUserRepository {
	//插入
    @Insert("insert into XXX (uid,uname,usex) values (#{uid},#{uname},#{usex})")
    public void insertData(@Param("uid")Integer uid,@Param("uname")String uname,@Param("usex")String usex);
	//查询
    @Select("select * from XXX")
    public List<MyUser> findAll();
	//删除
    @Delete("truncate table XXX")
    public void deleteData();
}

这里用了SQL注解的方式操作数据库

  1. 创建service包,在包中创建MyUserService接口和MyUserServiceImpl实现类
//MyUserService
package com.example.demo.service;

import com.example.demo.entity.MyUser;

import java.util.List;

public interface MyUserService {
    public List<MyUser> findAll();
    public void insertData(Integer uid, String uname, String usex);
    public void deleteData();
}

//MyUserServiceImpl
package com.example.demo.service;

import com.example.demo.entity.MyUser;
import com.example.demo.repository.MyUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class MyUserServiceImpl implements MyUserService{
    @Autowired
    private MyUserRepository myUserRepository;
    @Override
    public List<MyUser> findAll() {
        return myUserRepository.findAll();
    }
    @Override
    public void insertData(Integer uid,String uname,String usex){
        myUserRepository.insertData(uid,uname,usex);
    }
    @Override
    public void deleteData(){
        myUserRepository.deleteData();
    }
}
  1. 创建controller包,在包中创建控制器类MyUserController
package com.example.demo.controller;

import com.example.demo.entity.MyUser;
import com.example.demo.service.MyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@RestController
public class MyUserController {
    @Autowired
    private MyUserService myUserService;
    @RequestMapping("/findAll")
    public List<MyUser> findAll(){
        return myUserService.findAll();
    }
    @RequestMapping("/insert")
    public String insertData(Integer uid, String uname, String usex){
        myUserService.insertData(20,"test","女");
        return "insert successfully";
    }
    @RequestMapping("/delete")
    public String deleteData(){
        myUserService.deleteData();
        return "delete successfully";
    }

}

  1. 入口类
package com.example.demo;

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

@SpringBootApplication
@MapperScan(basePackages={"com.example.demo.repository"})
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

  1. 运行结果
    Spring Boot整合MyBatis连接 SQL Server(1)通过SQL注解的方式操作数据库_第2张图片
    Spring Boot整合MyBatis连接 SQL Server(1)通过SQL注解的方式操作数据库_第3张图片
    Spring Boot整合MyBatis连接 SQL Server(1)通过SQL注解的方式操作数据库_第4张图片
    下一篇将介绍使用基于XML的SQL映射文件操作数据库
    https://blog.csdn.net/Oceansssss/article/details/122668633

源码下载
https://download.csdn.net/download/Oceansssss/76550135

你可能感兴趣的:(Spring,Boot,spring,boot,sql,sqlserver,mybatis)