1、SpringBoot开启Mybatis支持
添加Mybatis起步依赖以及mysqljdbc驱动、连接池druid驱动
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
2、修改SpringBoot配置文件application.yml
#配置数据源
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
username: root
password: root
jpa:
database: MySQL
show-sql: true
generate-ddl: true
3、创建实体bean User
package com.example.demo.Pojo;
public class User {
private Long id;
private String name;
private Integer age;
public User() {
}
public User(Long id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
4.创建mapper接口
package com.example.demo.mapper;
import com.example.demo.Pojo.User;
import java.util.List;
public interface UserMapper {
public void save(User user);
public void update(User user);
public void delete(Long id);
public List<User> getAll();
public User findOne(Long id);
}
5、创建Mybatis Mapper映射文件
<?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.demo.mapper.UserMapper">
<select id="getAll" resultType="com.example.demo.Pojo.User">
select * from user
</select>
</mapper>
6、创建Service接口
package com.example.demo.service;
import com.example.demo.Pojo.User;
import java.util.List;
public interface UserService {
public List<User> getUserList();
public void createUser(User user);
public User getUser(Long id);
public void updateUser(Long id,User user);
public void deleteUser(Long id);
}
7、创建Service实现类
package com.example.demo.service;
import com.example.demo.Pojo.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> getUserList() {
return userMapper.getAll();
}
@Override
public void createUser(User user) {
userMapper.save(user);
}
@Override
public User getUser(Long id) {
return userMapper.findOne(id);
}
@Override
public void updateUser(Long id, User user) {
user.setId(id);
userMapper.update(user);
}
@Override
public void deleteUser(Long id) {
userMapper.delete(id);
}
}
8、创建Controller
package com.example.demo.controller;
import com.example.demo.Pojo.User;
import com.example.demo.service.UserService;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController1 {
@Resource
private UserService userService;
@GetMapping("/")
public List<User> getUserList(){
return userService.getUserList();
}
}
9、修改SpringBoot程序主启动类,增加扫描mapper接口
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}