赞
踩
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
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
SQL Server中建表
创建entity包,在包中创建MyUser实体类
package com.example.demo.entity;
import lombok.Data;
@Data
public class MyUser {
//成员变量名与数据库中表的字段要对应上
private Integer uid;
private String uname;
private String usex;
}
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注解的方式操作数据库
//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(); } }
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"; } }
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); } }
源码下载
https://download.csdn.net/download/Oceansssss/76550135
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。