Mybatis入门:
目录
2.9 应用启动层StudentmybatisspringApplication
一、spring boot 应用mybatis 核心
spring boot 应用mybatis 核心,就是把mybatis 的mapper 通过spring注入实例。
二、举例:
2.1 背景
实现一个查询的接口,返回结果为多个实例的集合。
select * from MyStudent where name = #{name}
2.2 项目结构:
相对于单纯的mybatis 使用,spring boot 应用mybatis 缺少了mybatis 的总配置文件。
2.3 依赖包 pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>studentmybatisspring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>studentmybatisspring</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.4 项目配置文件application.yml
application.yml
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://124.70.87.136:3306/chen?useUnicode=true&characterEncoding=UTF-8
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
logging:
level:
com.example.studentmybatisspring.mapper: debug
里面包含项目端口信息
数据库连接信息
以及开启查看执行sql的日志。
这里设置查看日志,我们一般只设置查看对应包的日志。这里是想要查看的包名。
这个mapper就是我们mabits里面的mapper。
当我们启动项目,触发相应的sql时,就会显示对应的sql日志。
2.5 实例层entity
存放的是我们需要的实例。
ResultMsg,我们用来封装接口的返回结果。
package com.example.studentmybatisspring.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResultMsg<T> {
public static final String SUCCESS_NO = "200";
public static final String SUCCESS_SUCCESS = "success";
private String no;
private String msg;
private T data;
public ResultMsg(String no, String msg) {
this.no = no;
this.msg = msg;
}
public static <T> ResultMsg<T> success(T data){
return new ResultMsg(SUCCESS_NO,SUCCESS_SUCCESS,data);
}
public static <T> ResultMsg<T> error(String no,String msg){
return new ResultMsg(no,msg);
}
}
MyStudent,是数据库里MyStudent表对应的实例。
数据库的表:
对应表的实例MyStudent
package com.example.studentmybatisspring.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MyStudent {
private long id;
private String name;
private int age;
public MyStudent(String name, int age) {
this.name = name;
this.age = age;
}
public MyStudent(String name) {
this.name = name;
}
}
2.6 mybatis的mapper层
接口:MyStudentMapper
package com.example.studentmybatisspring.mapper;
import com.example.studentmybatisspring.entity.MyStudent;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface MyStudentMapper {
@Select("select * from MyStudent where name = #{name}")
List<MyStudent> findByName(String name);
}
接口:MyStudentMapper中,包含sql语句,以及sql语句对应的方法。
2.7 spring boot service 层
service接口
package com.example.studentmybatisspring.service;
import com.example.studentmybatisspring.entity.MyStudent;
import java.util.List;
public interface MyStudentService {
List<MyStudent> findByName(String name);
}
service实现类
package com.example.studentmybatisspring.service.impl;
import com.example.studentmybatisspring.entity.MyStudent;
import com.example.studentmybatisspring.mapper.MyStudentMapper;
import com.example.studentmybatisspring.service.MyStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class MyStudentServiceImpl implements MyStudentService {
@Autowired
private MyStudentMapper myStudentMapper;
@Override
public List<MyStudent> findByName(String name) {
return myStudentMapper.findByName(name);
}
}
我们在spring boot service 实现类中,注入了mybatis 的 mapper接口。
2.8 controller层
MyStudentController
package com.example.studentmybatisspring.controller;
import com.example.studentmybatisspring.entity.MyStudent;
import com.example.studentmybatisspring.entity.ResultMsg;
import com.example.studentmybatisspring.service.MyStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/chen")
public class MyStudentController {
@Autowired
private MyStudentService myStudentService;
@RequestMapping("/findByName")
public ResultMsg<List<MyStudent>> findByName(String name){
List<MyStudent> byName = myStudentService.findByName(name);
return ResultMsg.success(byName);
}
}
2.9 应用启动层StudentmybatisspringApplication
我们需要在项目启动层,增加mapper的扫描。
相对于单纯的mybatis 使用,spring boot 应用mybatis 缺少了mybatis 的总配置文件。
这里填写包名。@MapperScan(basePackages =
package com.example.studentmybatisspring;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "com.example.studentmybatisspring.mapper")
public class StudentmybatisspringApplication {
public static void main(String[] args) {
SpringApplication.run(StudentmybatisspringApplication.class, args);
}
}
在浏览器里访问:
在控制台显示sql日志
三、数据库字段名称与java实体类字段名称不一致
同一个表,数据库的字段名称,与java实体类字段名称不一致。
如数据库中,字段名称为user_id
但是在java项目中,实体类的名称为驼峰格式,userId。
我们需要在application.yml,开启驼峰命名的映射。