1.新建项目
file==》new==》project
弹出界面,点击next就行
然后一般修改项目名称和地址
目前添加这几个基本的依赖
修改maven仓库
可以用
或者
配置application.yml
#配置启动服务
server:
port: 8080 #访问端口,自定义,写啥都行
servlet:
context-path: /mybatis1 #访问路径,可以省略不写
#配置数据库
spring:
datasource:
url: jdbc:mysql://localhost:3306/education?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
#education是数据库名字,根据自己的去改变
username: root
password: root
#账号密码也是一样的
driver-class-name: com.mysql.cj.jdbc.Driver
#配置mybatis映射文件
mybatis:
mapper-locations: classpath:/mappers/*.xml
创建数据库
CREATE TABLE
user
(
id
INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
username
VARCHAR(64) DEFAULT ‘’ COMMENT ‘账号’,
password
VARCHAR(64) DEFAULT ‘’ COMMENT ‘密码’,
PRIMARY KEY (id
)
) ENGINE=INNODB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
INSERT INTO USER(username,PASSWORD) VALUES (‘admin’,‘123456’);
项目基本目录文件
实体类User
package com.example.mybatis1.entity;
public class User {
private Integer id;
private String username;
private Integer password;
public Integer getId() {
return id;
}
public void setUserId(Integer id) {
this.id = id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getPassword() {
return password;
}
public void setPassword(Integer password) {
this.password = password;
}
}
映射文件UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.mybatis1.mapper.UserMapper">
<select id="getUserById" parameterType="Integer" resultType="com.example.mybatis1.entity.User">
SELECT * FROM user WHERE id = #{id}
</select>
</mapper>
接收的,接口文件UserMapper
package com.example.mybatis1.mapper;
import com.example.mybatis1.entity.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper {
User getUserById(Integer id);
}
service接口层UserService
package com.example.mybatis1.service;
import com.example.mybatis1.entity.User;
public interface UserService {
public User getUserById(Integer id);
}
impl实现UserServiceImpl
package com.example.mybatis1.service.impl;
import com.example.mybatis1.entity.User;
import com.example.mybatis1.mapper.UserMapper;
import com.example.mybatis1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User getUserById(Integer id) {
return userMapper.getUserById(id);
}
}
controller层UserController
package com.example.mybatis1.controller;
import com.example.mybatis1.entity.User;
import com.example.mybatis1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/findId/{id}")
public User getUser(@PathVariable("id") Integer id){
return userService.getUserById(id);
}
}
启动类
一定要加上@MapperScan(“com.example.mybatis1.mapper”)注解
package com.example.mybatis1;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.mybatis1.mapper")//扫描mapper下面的包。mapper文件就不需要加注解了
public class Mybatis1Application {
public static void main(String[] args) {
SpringApplication.run(Mybatis1Application.class, args);
}
}
然后启动项目