一:使用IDEA創建springboot項目


只點擊web的web項目

完成

二:在項目目錄下pom.xml中添加額外mybatis的依賴
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
三:配置數據庫信息,springboot支持properties格式和yml格式,這里使用yml做示例。把resource下的application.properties改成application.yml

spring.datasource.url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
spring.datasource.username: root
spring.datasource.password: 123456
spring.datasource.driver-class-name: com.mysql.jdbc.Driver
四:編寫測試代碼,結構如下

①:首先創建entity(與數據庫字段映射)在entity下創建Persion類實體
package com.example.springmybatisdemo.entity;
public class Person {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return
"id=" + id +
", name='" + name + '\'' +
", age=" + age
;
}
}
②:創建mapper(數據庫操作語句)在mapper下創建UserMapper
package com.example.springmybatisdemo.mapper;
import com.example.springmybatisdemo.entity.Person;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper {
@Select("SELECT * FROM Person WHERE id = #{id}")
Person selectUser(int id);
}
③:創建服務,在service下創建UserService
package com.example.springmybatisdemo.service;
import com.example.springmybatisdemo.entity.Person;
import com.example.springmybatisdemo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
UserMapper userMapper;
public Person selectUser(int id) {
return userMapper.selectUser(id);
}
}
④:創建controller,在controller包下創建UserController
package com.example.springmybatisdemo.controller;
import com.example.springmybatisdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/helloUser/{id}")
public String selectUser (@PathVariable int id){
return userService.selectUser(id).toString();
}
}
⑤:配置Mapper掃描,在啟動類中添加MapperScan("")
package com.example.springmybatisdemo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.example.springmybatisdemo.mapper") //掃描的mapper
@SpringBootApplication
public class SpringmybatisdemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringmybatisdemoApplication.class, args);
}
}
五:數據庫表結構:(與entity的Persion對應)

測試數據:

六:訪問請求

