大网站建设规范,会员制营销,四川住房和城乡建设厅网站题库,网站建设 长春目录 实现步骤
1. 在项目的 pom.xml 配置文件中引入如下依赖
2. 在项目的 application.properties 配置文件中添加如下依赖
3. 新建 UserMapper.class 接口类#xff0c;添加如下 3 个方法
4. 在 /resources/mybatis/mapper 路径(需要手动创建文件夹)下创建 UserMapper.xm…目录 实现步骤
1. 在项目的 pom.xml 配置文件中引入如下依赖
2. 在项目的 application.properties 配置文件中添加如下依赖
3. 新建 UserMapper.class 接口类添加如下 3 个方法
4. 在 /resources/mybatis/mapper 路径(需要手动创建文件夹)下创建 UserMapper.xml 文件添加如下操作数据库配置
5. 省略 UserService.class/UserServiceImpl.class 类代码创建 UserController.class 并添加如下代码测试
补充 实现步骤
1. 在项目的 pom.xml 配置文件中引入如下依赖
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-jdbc/artifactId
/dependency
dependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion2.1.4/version
/dependency
dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactId
/dependency
注意这里我们没有引入其他的数据源所以引入 spring-boot-starter-data-jdbc 依赖使用 Hikari 数据源如果要使用其他的数据源则需要引入对应依赖即可
2. 在项目的 application.properties 配置文件中添加如下依赖
#################### 数据库连接池配置 ####################
# 数据库连接地址
spring.datasource.url jdbc:mysql://localhost:3306/spring_study?characterEncodingutf8serverTimezoneUTC
# 数据库驱动类
spring.datasource.driver-class-name com.mysql.cj.jdbc.Driver
# 数据库用户名
spring.datasource.username root
# 数据库密码
spring.datasource.password dufu9137*
# 最小空闲连接
spring.datasource.hikari.minimum-idle 2
# 最大连接数
spring.datasource.hikari.maximum-pool-size 3
# 连接最大存活时间(应大于等于 30000)
spring.datasource.hikari.max-lifetime 30000
# 空闲连接超时时间(应小于 max-lifetime 的值)
spring.datasource.hikari.idle-timeout 20000
# 用于测试连接是否可用的查询语句
spring.datasource.hikari.connection-test-query SELECT 1#################### MyBatis 配置 ####################
# 指定 Mapper.xml 文件的位置
mybatis.mapper-locations classpath:mybatis/mapper/*.xml
# 开启驼峰命名映射规则
mybatis.configuration.map-underscore-to-camel-case true
# 打开日志输出功能
mybatis.configuration.log-impl org.apache.ibatis.logging.stdout.StdOutImpl
# 设置实体类映射别名所在包路径
mybatis.type-aliases-package com.study.springboot.entities
# 设置操作超时时间
mybatis.configuration.default-statement-timeout 3
# 开启缓存
mybatis.configuration.cache-enabled true
3. 新建 UserMapper.class 接口类添加如下 3 个方法
Mapper
public interface UserMapper {User getUserById(Integer id);ListUser getUserList();Integer addUser(User user);
}
4. 在 /resources/mybatis/mapper 路径(需要手动创建文件夹)下创建 UserMapper.xml 文件添加如下操作数据库配置
?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.study.springboot.mapper.UserMapperselect idgetUserById parameterTypeint resultTypeUserselect * from user where id #{id}/selectselect idgetUserList resultTypeUserselect * from user order by id asc/selectinsert idaddUser parameterTypeUser useGeneratedKeystrue keyColumnid keyPropertyidinsert into user(name) values(#{name})/insert
/mapper
5. 省略 UserService.class/UserServiceImpl.class 类代码创建 UserController.class 并添加如下代码测试
RestController
public class UserController {Resourceprivate UserService userService;GetMapping(/byId)public User getUserById(RequestParam Integer id) {return userService.getUserById(id);}GetMapping(/list)public ListUser getUserList() {return userService.getUserList();}PostMapping(/add)public User addUser(RequestBody User user) {Integer count userService.addUser(user);return count 1 ? user : null;}
}
补充
1. xxxMapper.class 上添加 Mapper 注解表明这是一个 Mapper 类也可以在项目启动类或添加了 Configuration 注解的配置类上添加 MapperScan(com.study.springboot.mapper) 注解表明这个包路径下的所有接口类都是 Mapper 类但建议使用 Mapper 注解实现
2. 我们这里使用的 xxxMapper.xml 配置文件的方式添加 SQL 语句操作数据库也可以在 xxxMapper.class 类上直接使用 Select、Update、Insert、Delete 注解直接添加 SQL 语句操作注意注解式添加 SQL方法中的参数可能需要 Param 注解标记
3. 对于自增的记录有时候我们需要插入操作后直接返回插入的结果但是得不到自增的主键如果是使用 xxxMapper.xml 文件方式可以通过添加如下属性解决这个问题
insert idaddUser parameterTypeUser useGeneratedKeystrue keyColumnid keyPropertyidinsert into user(name) values(#{name})
/insert
如果是使用注解的方式那么可通过添加 Options 注解添加属性解决这个问题
Insert(insert into user(name) values(#{name}))
Options(useGeneratedKeys true, keyColumn id, keyProperty id)
Integer addUser(User user);
useGeneratedKeys true表示使用自增主键
keyColumn id数据库中主键列名
keyProperty id映射实体类主键字段
4. MyBatis 操作数据库时传递的参数为对象的话在 SQL 语句中直接使用对象的属性占位即可例如
Insert(insert into user(name) values(#{name}))
Options(useGeneratedKeys true, keyColumn id, keyProperty id)
Integer addUser(User user);
如果参数使用了 Param 注解的话则需要使用 Param 注解的值.对象属性占位例如
Insert(insert into user(name) values(#{user.name}))
Options(useGeneratedKeys true, keyColumn id, keyProperty id)
Integer addUser(Param(user) User user);