一.通过idea建立工程
1.png
2.png
3.png
4.png
二.配置文件
1.在resources文件夹下建立config文件夹,并创建mybatis-config.xml,代码如下:
/p>
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
2.在resources文件夹下建立mapper文件夹,并创建UserMapper.xml,代码如下:
/p>
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
select * from user where id = #{id}
select * from user
3.application.properties配置文件代码如下:
spring.datasource.url=jdbc:mysql://localhost:3306/webtest?characterEncoding=utf-8&useSSL=true&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver
mybatis.config-location=classpath:config/mybatis-config.xml
mybatis.mapper-locations= classpath:mapper/*.xml
如果遇到下列错误,是由于MySQL 这个jar 包依赖类型默认是runtime ,
也就是说只有运行时生效,所以虽然这里报错,但是不影响你代码运行。
error.png
在pom.xml文件内将依赖改成compile就行
image.png
三.pojo类
在com.wyh.springbootmybatisdemo下建立pojo包,并在该包下建立User类
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private String password;
public User() {
}
public User(Long id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
四.mapper
在com.wyh.springbootmybatisdemo下建立mapper包,并在该包下建立UserMapper接口
import com.wyh.springbootmybatisdemo.pojo.User;
import java.util.List;
public interface UserMapper {
User findById(Long id);
List findAll();
}
五.controller控制器
在com.wyh.springbootmybatisdemo下建立controller包,并在该包下建立UserController类
import com.wyh.springbootmybatisdemo.mapper.UserMapper;
import com.wyh.springbootmybatisdemo.pojo.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private UserMapper userMapper;
@GetMapping
public User findUser(@RequestParam(value = "id") Long id){
return userMapper.findById(id);
}
}
六.启动类
在启动类上面加入扫描的注解
@MapperScan("com.wyh.springbootmybatisdemo.mapper")
如下所示:
@SpringBootApplication
@MapperScan("com.wyh.springbootmybatisdemo.mapper")
public class SpringbootmybatisdemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootmybatisdemoApplication.class, args);
}
}
七.数据库
建立webtest数据库,user表结构如下:
mysql_user表
八.项目结构目录
image.png
九. 启动springboot项目并测试
在浏览器url输入http://localhost:8080/user?id=1
test.png
测试成功!!!