精通 Spring Boot 系列文(11)
程序IT圈
共 5009字,需浏览 11分钟
· 2020-07-30
阅读全文,约 14 分钟
Spring Boot 整合 MyBatis
MyBatis 是目前优秀的 ORM 框架,支持普通的数据库操作,几乎消除了常规的 JDBC 操作,极大简化我们的开发操作。
1)编辑 pom.xml 文件
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.nxgroupId>
<artifactId>springbootdataartifactId>
<version>1.0-SNAPSHOTversion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.6.RELEASEversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<scope>testscope>
dependency>
dependencies>
project>
2)编辑 application.properties 文件
####################
### 数据源信息配置 ###
####################
# 数据库地址
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdata?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
# 用户名
spring.datasource.username=root
# 密码
spring.datasource.password=1234
# 数据库驱动
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
# 指定连接池中最大的活跃连接数.
spring.datasource.max-active=20
# 指定连接池最大的空闲连接数量.
spring.datasource.max-idle=8
# 指定必须保持连接的最小值
spring.datasource.min-idle=8
# 指定启动连接池时,初始建立的连接数量
spring.datasource.initial-size=10
3)创建 User 持久化类
public class User implements Serializable{
private static final long serialVersionUID = 1L;
private int id ;
private String loginName ;
private String username ;
private String password;
// setXxx 和 getXxx 方法
}
4)创建 UserRepository 数据访问接口
public interface UserRepository {
@Insert("insert into tb_user(login_name ,username ,password) "
+ "values (#{loginName},#{username},#{password})")
public int insertUser(User user);
// 插入数据获取主键
@Insert("insert into tb_user(login_name ,username ,password) "
+ "values (#{loginName},#{username},#{password})")
@Options(useGeneratedKeys=true,keyProperty="id",keyColumn="id")
public void insertGetKey(User user);
@Select("select * from tb_user where username = #{username}")
// 引用id="userResult"的@Results
@ResultMap("userResult")
public User selectByUsername(@Param("username")String username);
@Select("select * from tb_user")
// @Results用于映射对象属性和数据库列,常用于对象属性和数据库列不同名情况
@Results(id="userResult",value={
@Result(id=true,column="id",property="id"),
@Result(column="login_name",property="loginName"),
@Result(column="password",property="password"),
@Result(column="username",property="username")
})
public List findAll() ;
@Delete("delete from tb_user where id=#{id}")
public void delete(final Integer id);
@Select("select * from tb_user where id=#{id}")
// 引用id="userResult"的@Results
@ResultMap("userResult")
public User findUserById(int id);
@Update("update tb_user set username=#{username}, login_name=#{loginName} where id=#{id}")
public void update(final User user);
}
5)创建 UserService 业务层类
@Service
public class UserService {
// 注入UserRepository
@Resource
private UserRepository userRepository;
public int insertUser(User user){
return userRepository.insertUser(user);
}
public User selectByUsername(String username){
return userRepository.selectByUsername(username);
}
public List findAll() {
return userRepository.findAll();
}
public void insertGetKey(User user) {
userRepository.insertGetKey(user);
}
public void update(User user) {
userRepository.update(user);
}
public void delete(Integer id) {
userRepository.delete(id);
}
}
6)创建 UserController 控制器类
@RestController
@RequestMapping("/user")
public class UserController {
// 注入UserService
@Resource
private UserService userService;
@RequestMapping("/insertUser")
public String insertUser(User user){
return "插入数据["+userService.insertUser(user)+"]条";
}
@RequestMapping("/insertGetKey")
public User insertGetKey(User user) {
userService.insertGetKey(user);
return user ;
}
@RequestMapping("/selectByUsername")
public User selectByUsername(String username){
return userService.selectByUsername(username);
}
@RequestMapping("/findAll")
public List findAll() {
return userService.findAll();
}
@RequestMapping("/update")
public void update(User user) {
userService.update(user);
}
@RequestMapping("/delete")
public void delete(Integer id) {
userService.delete(id);
}
}
7)测试
http://localhost:8080/user/insertUser?loginName=shuaishuai&username=小猿&password=123123
Java后端编程
更多Java推文,关注公众号
评论
文心一言 vs GPT-4 —— 全面横向比较
向AI转型的程序员都关注了这个号👇👇👇文章目录PK方法PK过程Round 1: 语义理解简单语义文言文理解孤立语理解上下文理解Round 2: 内容创作撰写邮件撰写影评撰写软文Round 3: 逻辑推理简单推理逻辑陷阱逻辑干扰多链条推理Round 4: 编码能力常见算法高级算法找bug代码理解Rou
机器学习AI算法工程
0
学一学 Spring Boot 3.x
在 Java 后端开发领域,功能强大的 Spring 开源框架不仅是首选,也是事实上的标准。但由于 Spring 存在配置烦琐、部署不易、依赖管理困难等问题,因此基于 Spring 的快速开发框架 Spring Boot 应运而生,它能大大简化 Spring 应用程序的配置和部署过程。2018 年,
小哈学Java
0
Go 1.22 的新增功能系列之二:reflect.TypeFor
Go 1.22 的第一个候选版本已经发布,这意味着最终版本即将发布,现在是我在博客中介绍我在这个周期中所做工作的时候了。像往常一样,我的贡献很小,但它们是我的,所以我将从幕后的角度来谈谈它们。首先是reflect.TypeFor。这是整个函数:// TypeFor returns the [Type
GoCN
0
有意思!一个关于 Spring 历史的在线小游戏
发现 Spring One 的官网上有个好玩的彩蛋,分享给大家!进到Spring One的官网,可以看到右下角有个类似马里奥游戏中的金币图标。点击该金币之后,会打开一个新的页面,进入下面这样一个名为:The History Of Spring 的在线小游戏你可以使用上下左右的方向键来控制Spring
公众号程序猿DD
1
Go 1.22 的新增功能系列之一:cmp.Or
截至撰写本文时,Go 1.22 已经发布几个月了。早就该结束我为 1.22 所做的工作的系列了。抱歉耽搁了这么久,我最近忙于生活事务。如果您错过了我关于reflect.TypeFor(https://blog.carlana.net/post/2024/golang-reflect-type-for
GoCN
1
全新 SOTA backbone | 2024年了,再见ViT系列Backbone,实数难得,不知道效果如何?
点击上方“小白学视觉”,选择加"星标"或“置顶”重磅干货,第一时间送达在构建用于精确匹配的深度固定长度表示时,确定指纹上的密集特征点,特别是在像素 Level 上,具有重大意义。为了探索指纹匹配的可解释性,作者提出了一种多阶段可解释的指纹匹配网络,名为通过视觉 Transformer 进行指纹匹配的
小白学视觉
10
Spring Boot + flowable 快速实现工作流
关注我们,设为星标,每天7:40不见不散,架构路上与您共享回复架构师获取资源大家好,我是你们的朋友架构君,一个会写代码吟诗的架构师。来源:blog.csdn.net/zhan107876/article/details/120815560总览一、flowable-ui部署运行二、绘制流程图绘图细节:
Java架构师社区
0
雷军回应被称爽文第一男主;Meta推出开源大模型Llama 3;特步就北京半程马拉松赛发布致歉声明
雷军:没模仿马斯克、乔布斯,卡里也没有冰冷的40亿,SU7正式版本开始交付4月18日,小米集团CEO雷军开播,与网友畅谈SU7开售这20天并带领大家参观小米交付中心。在直播中雷军称:”很多人都催我们快一点交付啊,雷总你能不能去工厂打螺丝啊。今天我们这个工厂是现代化工厂啊,主要的工艺都是全自动化生产的
亿欧网
0