Spring Boot集成mybatis

之前在Spring Boot微服务框架搭建中已经讲了最简单的hello world例子的创建。接下来,将在那个例子的基础上,集成mybatis。

1. 添加依赖

首先,在pom.xml文件中添加对mybatis以及mysql驱动的依赖。MyBatis 1.3.1对应着Spring Boot 1.5.8 RELEASE的版本。不清楚的,可以在Spring Initializr时候,把mysql,mybatis选上,IDEA会自动选择相对应的版本。


  mysql
  mysql-connector-java
  runtime


  org.mybatis.spring.boot
  mybatis-spring-boot-starter
  1.3.1

2.配置文件

在application.yml中添加配置。mapper-locations为mapper映射文件存放的位置,classpath:为resources文件夹。

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&characterEncoding=utf8
mybatis:
  mapper-locations: classpath:mapper/*.xml

3.创建例子

1) 创建Entity

public class Student {
    private String id;
    private String pwd;
    private String name;
    private String sex;
    private String year;
    private String major;
    ......
    省略getter和setter
    ......
}

2) 创建StudentMapper接口

@Mapper注解作用为扫描的时候自动会扫描到这个文件。

@Mapper
public interface StudentMapper {
    List getList();
    Student getStudentByName(String name);
}

3) 创建StudentMapper.xml文件

namespace需要对应StudentMapper接口的位置,id对应接口中的方法,resultType为返回的数据类型。其中#{name}为getStudentByName(String name)方法传入的参数。





    

    


4) 创建StudentService

Service需要为其添加@Service注解,该注解同后面的@Controller注解一样,确保该类能添加到容器中,能被自动注入。

@Service
public class StudentServiceImpl implements StudentService{

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public List getList() {
        return studentMapper.getList();
    }

    @Override
    public Student getStudentByName(String name) {
        return studentMapper.getStudentByName(name);
    }
}

4) 创建StudentController

@RestController
@RequestMapping("/stu")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/list")
    public List getStudentList(){
        return studentService.getList();
    }

    @RequestMapping("/{name}")
    public Student getByName(@PathVariable String name){
        return studentService.getStudentByName(name);
    }

}

5) 运行入口类

输入localhost:8080/stu/list,可以看到已经从数据库中提取出了信息。

Spring Boot集成mybatis_第1张图片

测试另一方法

Spring Boot集成mybatis_第2张图片

4.mapper的注解写法

public interface UserMapper {

    @Select("SELECT * FROM users")
    @Results({
        @Result(property = "userSex",  column = "user_sex", javaType = UserSexEnum.class),
        @Result(property = "nickName", column = "nick_name")
    })
    List getAll();

    @Select("SELECT * FROM users WHERE id = #{id}")
    @Results({
        @Result(property = "userSex",  column = "user_sex", javaType = UserSexEnum.class),
        @Result(property = "nickName", column = "nick_name")
    })
    UserEntity getOne(Long id);

    @Insert("INSERT INTO users(userName,passWord,user_sex) VALUES(#{userName}, #{passWord}, #{userSex})")
    void insert(UserEntity user);

    @Update("UPDATE users SET userName=#{userName},nick_name=#{nickName} WHERE id =#{id}")
    void update(UserEntity user);

    @Delete("DELETE FROM users WHERE id =#{id}")
    void delete(Long id);

}

注解写mapper 参考spring boot(六):如何优雅的使用mybatis

你可能感兴趣的:(Spring Boot集成mybatis)