编译工具:STS
代码下载链接:
一、项目的建立
选择依赖:Mybatis,Web,MySql,JDBC
SpringBoot版本:2.0.3
项目生成结构:
pom依赖:
1 24 4.0.0 5 6com.xm 7demo005_Mybatis 80.0.1-SNAPSHOT 9jar 10 11demo005_Mybatis 12Demo project for Spring Boot 13 1415 20 21org.springframework.boot 16spring-boot-starter-parent 172.0.2.RELEASE 1819 22 26 27UTF-8 23UTF-8 241.8 2528 53 5429 32org.springframework.boot 30spring-boot-starter-jdbc 3133 36org.springframework.boot 34spring-boot-starter-web 3537 41 42org.mybatis.spring.boot 38mybatis-spring-boot-starter 391.3.2 4043 47mysql 44mysql-connector-java 45runtime 4648 52org.springframework.boot 49spring-boot-starter-test 50test 5155 62 63 6456 6157 60org.springframework.boot 58spring-boot-maven-plugin 59
二、创建数据库
三、项目配置
1.添加yml配置文件:application.yml
1 #配置mybatis 2 mybatis: 3 #配置xml映射路径 4 mapper-locations: classpath:mapper/*.xml 5 #配置实体类的别名 6 type-aliases-package: com.xm.pojo 7 configuration: 8 #开启驼峰命名法 9 map-underscore-to-camel-case: true10 11 12 #配置mysql连接13 spring:14 datasource:15 url: jdbc:mysql://10.1.51.31:3306/xm16 username: root17 password: cube150118 driver-class-name: com.mysql.jdbc.Driver
2.在source文件夹下建立mapper文件夹
3.在Springboot启动类添加@MapperScan注解
1 package com.xm; 2 3 import org.mybatis.spring.annotation.MapperScan; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 @MapperScan(value="com.xm.mapper") 7 @SpringBootApplication 8 public class Demo005MybatisApplication { 9 10 public static void main(String[] args) {11 SpringApplication.run(Demo005MybatisApplication.class, args);12 }13 }
注意:添加@MapperScan注解后,每个mapper都会自动扫描成为Bean。否则,需要在每个mapper接口上添加@Mapper接口
四、代码实现
1.实体类Student
1 package com.xm.pojo; 2 3 /** 4 * name:学生实体 5 * @author xxm 6 * 7 */ 8 public class Student { 9 /**10 * content:主键id11 */12 private int id;13 /**14 * content:姓名15 */16 private String name;17 18 public int getId() {19 return id;20 }21 public void setId(int id) {22 this.id = id;23 }24 public String getName() {25 return name;26 }27 public void setName(String name) {28 this.name = name;29 }30 31 32 33 }
2.数据操作层StudentMapper
1 package com.xm.mapper; 2 3 import java.util.List; 4 5 import com.xm.pojo.Student; 6 7 public interface StudentMapper { 8 9 /**10 * 根据id查询11 * @param id12 * @return13 */14 public Student getById(Integer id);15 16 /**17 * 查询全部18 * @return19 */20 public Listlist();21 22 /**23 * 插入24 * @param student25 */26 public void insert(Student student);27 28 /**29 * 根据student的id修改30 * @param student31 */32 public void update(Student student);33 34 /**35 * 根据id删除36 * @param id37 */38 public void delete(Integer id);39 40 }
3.mapper映射Studentmapper
1 2 34 5 6 9 10 11 14 15 16 17 insert into student(name) values(#{name})18 19 20 2122 update student set name=#{name} where id=#{id}23 24 25 2627 delete from student where id=#{id}28 29
4.控制层StudentController
1 package com.xm.controller; 2 3 import java.util.List; 4 5 import javax.websocket.server.PathParam; 6 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.web.bind.annotation.DeleteMapping; 9 import org.springframework.web.bind.annotation.GetMapping;10 import org.springframework.web.bind.annotation.PathVariable;11 import org.springframework.web.bind.annotation.PostMapping;12 import org.springframework.web.bind.annotation.PutMapping;13 import org.springframework.web.bind.annotation.RestController;14 15 import com.xm.mapper.StudentMapper;16 import com.xm.pojo.Student;17 18 @RestController19 public class StudentController {20 @Autowired21 private StudentMapper studentMapper;22 23 /**24 * 根据id查询学生25 * @param id26 * @return27 */28 @GetMapping("/student/{id}")29 public Student getById(@PathVariable("id") Integer id) {30 31 Student student = studentMapper.getById(id);32 return student;33 34 }35 36 /**37 * 查询全部38 * @return39 */40 @GetMapping("/students")41 public Listlist(){42 List students = studentMapper.list();43 return students;44 }45 46 /**47 * 插入48 * @param student49 */50 @PostMapping("/student")51 public void insert( Student student) {52 studentMapper.insert(student);53 }54 55 /**56 * 修改57 * @param student58 */59 @PutMapping("/student/{id}")60 public void update(Student student,@PathVariable("id")Integer id) {61 studentMapper.update(student);62 }63 64 /**65 * 根据id删除66 * @param id67 */68 @DeleteMapping("/student/{id}")69 public void delete(@PathVariable("id") Integer id) {70 studentMapper.delete(id);71 }72 73 }
2018-06-1517:31:14