- 导入Mybatis响应jar版本包(jar、springboot-mybatis-start.jar)
- 数据源信息(driverurlusernamepassword),放全局配置文件
JDK版本:1.8
第一步:配置pom.xml,导入mybatis (springboot+mybatis的jar)、mysql-driver驱动
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.2.0
mysql
mysql-connector-java
runtime
第二步:配置application.yml
(1)配置数据源信息(数据库连接信息)
server:
port: 8080
servlet:
context-path: /myproject #配置项目访问名称
spring:
jackson:
timeZone: Asia/Shanghai
dateFormat: yyyy-MM-dd'T'HH:mm:ss.SSSZ
encoding:
force: true
enabled: true # Enable http encoding support.
charset: UTF-8
datasource: #mybatis配置信息
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
name: demo #数据库名称
url: jdbc:mysql://localhost:3306/${spring.datasource.name}?useUnicode=yes&characterEncoding=UTF8&serverTimezone=${spring.jackson.timeZone}&useLegacyDatetimeCode=false&useAffectedRows=true&allowMultiQueries=true
username: root
password: root
hikari:
connection-timeout: 30000 # 等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQ
minimum-idle: 5 # 最小连接数
maximum-pool-size: 20 # 最大连接数
auto-commit: true # 自动提交
idle-timeout: 600000 # 连接超时的最大时长(毫秒),超时则被释放(retired),默认:10分钟
pool-name: DateSourceHikariCP # 连接池名字
max-lifetime: 1800000 # 连接的生命时长(毫秒),超时而且没被使用则被释放(retired),默认:30分钟
connection-test-query: SELECt 1
(2)配置mybatis的映射信息
mybatis:
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper
@Data
public class User {
private static final long serialVersionUID = 4763112663967580532L;
private Integer id;
private String username;
private String password;
private Integer age;
private String phone;
private String email;
}
(1)配置数据源信息(数据库连接信息)
server:
port: 8080
servlet:
context-path: /myproject #配置项目访问名称
spring:
jackson:
timeZone: Asia/Shanghai
dateFormat: yyyy-MM-dd'T'HH:mm:ss.SSSZ
encoding:
force: true
enabled: true # Enable http encoding support.
charset: UTF-8
datasource: #mybatis配置信息
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
name: demo #数据库名称
url: jdbc:mysql://localhost:3306/${spring.datasource.name}?useUnicode=yes&characterEncoding=UTF8&serverTimezone=${spring.jackson.timeZone}&useLegacyDatetimeCode=false&useAffectedRows=true&allowMultiQueries=true
username: root
password: root
hikari:
connection-timeout: 30000 # 等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQ
minimum-idle: 5 # 最小连接数
maximum-pool-size: 20 # 最大连接数
auto-commit: true # 自动提交
idle-timeout: 600000 # 连接超时的最大时长(毫秒),超时则被释放(retired),默认:10分钟
pool-name: DateSourceHikariCP # 连接池名字
max-lifetime: 1800000 # 连接的生命时长(毫秒),超时而且没被使用则被释放(retired),默认:30分钟
connection-test-query: SELECt 1
(2)配置mybatis的映射信息
mybatis:
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper
@Data
public class User {
private static final long serialVersionUID = 4763112663967580532L;
private Integer id;
private String username;
private String password;
private Integer age;
private String phone;
private String email;
}
mybatis:
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper
@Data
public class User {
private static final long serialVersionUID = 4763112663967580532L;
private Integer id;
private String username;
private String password;
private Integer age;
private String phone;
private String email;
}
导入lombok依赖包
org.projectlombok lomboktrue
(3) 在src/main/java/com/example/demo/mapper包下创建接口UserMapper.java,与UserMapper.xml文件映射,用于实现数据库的增删改查)
package com.example.demo.mapper;
import com.example.demo.bean.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface UserMapper {
User findByUserId(@Param("id") Integer id);
}
(4) 在resources的mybatis/mapper/ 创建UserMapper.xml (SQL映射文件)
- 配置文件模板:入门_MyBatis中文网
namespace: 表示映射接口UserMapper.java的完整包结构名称 id:取值与namespace指向接口UserMapper.java中方法名称findByUserId保持一致 resultType:结果类型,将查询处理的数据存放到哪种类型的对象中,这边写实体类User.java的完整包结构名称
(5) 单元测试
创建src/test/java/com/example/demo/mapper/UserMapperTests.java
UserMapperTests.java的文件内容如下
package com.example.demo.mapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
import java.sql.SQLException;
@SpringBootTest
public class UserMapperTests {
@Autowired
private DataSource dataSource;
@Autowired
private UserMapper userMapper;
@Test
public void getConnection() throws SQLException {
System.out.println(dataSource.getConnection());
}
@Test
public void findUserByUsername(){
System.out.println(userMapper.findByUsername("qiongxia"));
}
}
3.业务层开发(server层)
在src/main/java/com/example/demo创建service包,然后在该包下创建各种业务逻辑的类
(1)业务层接口类
UserService.java
package com.example.demo.service;
import com.example.demo.bean.User;
import org.apache.ibatis.annotations.Param;
public interface UserService {
User findByUsername(@Param("username") String username);
}
(2)业务层接口实现类
UserServiceImpl.java
package com.example.demo.service.Impl;
import com.example.demo.bean.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
//service层依赖mapper层,需要创建mapper对象
@Autowired
private UserMapper userMapper;
@Override
public User findByUsername(String username) {
return userMapper.findByUsername(username);
}
}
(3)业务层单元测试
创建src/test/java/com/example/demo/service/UserServiceTests.java,内容如下
package com.example.demo.service;
import com.example.demo.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.sql.SQLException;
@SpringBootTest
public class UserServiceTests {
@Autowired
private UserService userService;
@Test
public void findByUsername(){
System.out.println(userService.findByUsername("qiongxia"));
}
}
4.控制层开发(controller层)
以JSON格式的字符传递给前端
@RestController注解控制器



