首先最重要的是思路:可以分为以下几步
1)添加Mybatis的起步依赖
2)添加数据库驱动坐标(这次使用SQL server)
3)添加数据库连接信息
4)创建数据库与表
5)创建实体Bean
6)编写Mapper接口
7)在application.yml中添加mybatis的信息
8)编写Controller
9)启动SpringBoot引导类
思路是最重要的,思路是最重要的,思路是最重要的,重要的话说三遍!!!!!
1)添加Mybatis的起步依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
2)添加数据库驱动坐标(这次使用SQL server)
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
</dependency>
3)添加数据库连接信息
spring:
datasource:
data-password: itcqms
data-username: sa
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
url: jdbc:sqlserver://ITC-QMS:1433;databaseName=TEST_HZ
4)创建数据库与表
此处省略几十行代码
5)创建实体Bean
package com.example.pojo;
public class Students {
private String studentSno;
private String studentSname;
private String studentSsex;
private String studentSage;
private String studentSaddress;
public Students() {
super();
// TODO Auto-generated constructor stub
}
public Students(String studentSno, String studentSname, String studentSsex, String studentSage,
String studentSaddress) {
super();
this.studentSno = studentSno;
this.studentSname = studentSname;
this.studentSsex = studentSsex;
this.studentSage = studentSage;
this.studentSaddress = studentSaddress;
}
public Students(String studentSname, String studentSsex, String studentSage, String studentSaddress) {
super();
this.studentSname = studentSname;
this.studentSsex = studentSsex;
this.studentSage = studentSage;
this.studentSaddress = studentSaddress;
}
public String getStudentSno() {
return studentSno;
}
public void setStudentSno(String studentSno) {
this.studentSno = studentSno;
}
public String getStudentSname() {
return studentSname;
}
public void setStudentSname(String studentSname) {
this.studentSname = studentSname;
}
public String getStudentSsex() {
return studentSsex;
}
public void setStudentSsex(String studentSsex) {
this.studentSsex = studentSsex;
}
public String getStudentSage() {
return studentSage;
}
public void setStudentSage(String studentSage) {
this.studentSage = studentSage;
}
public String getStudentSaddress() {
return studentSaddress;
}
public void setStudentSaddress(String studentSaddress) {
this.studentSaddress = studentSaddress;
}
@Override
public String toString() {
return "Students [studentSno=" + studentSno + ", studentSname=" + studentSname + ", studentSsex=" + studentSsex
+ ", studentSage=" + studentSage + ", studentSaddress=" + studentSaddress + "]";
}
}
6)编写Mapper接口
package com.example.dao;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import com.example.pojo.Students;
@Repository
public interface StudentsDao {
@Select("SELECT * FROM dbo.STUDENTS")
@Results({
@Result(property = "studentSno", column = "student_sno"),
@Result(property = "studentSname", column = "student_sname"),
@Result(property = "studentSsex", column = "student_ssex"),
@Result(property = "studentSage", column = "student_sage"),
@Result(property = "studentSaddress", column = "student_saddress")
})
List<Students> getAll();
@Select("SELECT * FROM dbo.STUDENTS where student_sno=#{id} ")
@Results({
@Result(property = "studentSno", column = "student_sno"),
@Result(property = "studentSname", column = "student_sname"),
@Result(property = "studentSsex", column = "student_ssex"),
@Result(property = "studentSage", column = "student_sage"),
@Result(property = "studentSaddress", column = "student_saddress")
})
Students findOneById(Integer id);
@Insert("insert into dbo.STUDENTS(student_sno,student_sname,student_ssex,student_sage,student_saddress) values(#{studentSno},#{studentSname},#{studentSsex},#{studentSage},#{studentSaddress})")
int addaddStudents(Students students);
@Delete("delete from dbo.STUDENTS where student_sno=#{id} ")
int deleteStudentsById(Integer id);
}
7)在application.yml中添加mybatis的信息
mybatis:
type-aliases-package: com.example.com.example.pojo
8)编写Controller
package com.example.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.pojo.Students;
import com.example.serverice.StudentsService;
@RestController
@RequestMapping("/students")
public class StudentsController {
@Autowired
private StudentsService studentsService;
@GetMapping
public List<Students> findAll() {
List<Students> students = studentsService.getAll();
return students;
}
@GetMapping("/{id}")
public Students findOneById(@PathVariable Integer id) {
System.out.println("id=========" + id);
return studentsService.findOneById(id);
}
/*
* @PostMapping public String addStudents(@RequestParam("studentSno")String
* studentSno,@RequestParam("studentSname")String
* studentSname,@RequestParam("studentSsex")String
* studentSsex,@RequestParam("studentSage")String
* studentSage,@RequestParam("studentSaddress")String studentSaddress ) {
* Students students = new
* Students(studentSno,studentSname,studentSsex,studentSage,studentSaddress);
* System.out.println("students====="+students); try { int i=
* studentsService.addStudents(students); if(i>0) { return "添加成功"; } else {
* return "添加失败"; }
*
* }catch(Exception e) { // TODO Auto-generated catch block e.printStackTrace();
* System.out.println("添加失败");
*
* } return "000000000"; }
*/
@PostMapping
public String addStudents2(@RequestBody Students students) {
System.out.println("students====="+students);
try {
int i= studentsService.addStudents(students);
if(i>0) {
return "添加成功";
}
else {
return "添加失败";
}
}catch(Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("添加失败");
}
return "000000000";
}
@DeleteMapping("{id}")
public String deleteStudentsById(@PathVariable Integer id) {
System.out.println("delete 的id="+id);
try {
int i=studentsService.deleteStudentsById(id);
if (i>0) {
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "删除失败";
}
}
9)启动SpringBoot引导类
package com.example;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.dao") //扫描mapper接口
public class SmsApplication {
public static void main(String[] args) {
SpringApplication.run(SmsApplication.class, args);
}
}
以上是全部步骤,xml版的看我另外一篇博客