spring boot整合mybatis实现对MySQL数据库内容的增删改查

1.spring boot

SpringBoot基于Spring4.0设计,不仅继承了Spring框架原有的优秀特性,而且还通过简化配置来进一步简化了Spring应用的整个搭建和开发过程,提供了更好的体验。另外SpringBoot通过集成大量的框架使得依赖包的版本冲突,以及引用的不稳定性等问题得到了很好的解决。

Spring具有简单,可测试和松耦合等特点、不仅可以用于服务器端的开发,也可以应用F任何Java应用的开发中,Spring框架的主要优点如下:方便解耦,简化开发;方便集成各种优秀框架如Struts2、Hibernate、 MyBatis) 的直接支持;Spring对Java EE开发中非常难用的些API都提供了封装,使这些API应用的难度大大降低;方便进行测试。

那我们为什么还要使用spring boot呢

(1)简化配置,不需要编写太多的xml配置文件;

(2)基于Spring构建,使开发者快速入门,门槛很低;

(3)SpringBoot可以创建独立运行的应用而不需要依赖于容器;

(4)内置tomcat服务器,不需要打包成war包,可以直接放到tomcat中运行;

(5)提供maven极简配置,以及可视化的相关监控功能,比如性能监控,应用的健康程度等;

(6)为微服务SpringCloud奠定了基础,使得微服务的构建变得简单;

(7)Spring可以整合很多各式各样的框架,并能很好的集成;

(8)活跃的社区与论坛,以及丰富的开发文档;

2.简单sql语句的实现

新建项目选择依赖

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的示例,演示如何使用Spring整合MyBatis数据库进行增删操作: 1. 添加依赖 在 pom.xml 文件中添加以下依赖: ```xml <dependencies> <!-- Spring整合MyBatis依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <!-- MySQL数据库驱动依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> </dependencies> ``` 2. 配置数据源 在 application.properties 文件中添加以下配置: ```properties # 数据库连接配置 spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # MyBatis配置 mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.example.demo.entity ``` 3. 创建实体类 创建一个实体类,例如: ```java public class User { private Integer id; private String name; private Integer age; // 省略getter和setter方法 } ``` 4. 创建Mapper接口 创建一个Mapper接口,例如: ```java public interface UserMapper { // 新增用户 int insert(User user); // 删除用户 int deleteById(Integer id); // 更新用户 int update(User user); // 根据ID询用户 User selectById(Integer id); // 询所有用户 List<User> selectAll(); } ``` 5. 创建Mapper XML文件 在 resources/mapper 目录下创建 UserMapper.xml 文件,例如: ```xml <mapper namespace="com.example.demo.mapper.UserMapper"> <!-- 新增用户 --> <insert id="insert" parameterType="User"> insert into user(name, age) values(#{name}, #{age}) </insert> <!-- 删除用户 --> <delete id="deleteById" parameterType="java.lang.Integer"> delete from user where id = #{id} </delete> <!-- 更新用户 --> <update id="update" parameterType="User"> update user set name = #{name}, age = #{age} where id = #{id} </update> <!-- 根据ID询用户 --> <select id="selectById" resultType="User" parameterType="java.lang.Integer"> select id, name, age from user where id = #{id} </select> <!-- 询所有用户 --> <select id="selectAll" resultType="User"> select id, name, age from user </select> </mapper> ``` 6. 创建Service接口及实现类 创建一个Service接口及实现类,例如: ```java public interface UserService { // 新增用户 int addUser(User user); // 删除用户 int deleteUserById(Integer id); // 更新用户 int updateUser(User user); // 根据ID询用户 User getUserById(Integer id); // 询所有用户 List<User> getAllUsers(); } @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public int addUser(User user) { return userMapper.insert(user); } @Override public int deleteUserById(Integer id) { return userMapper.deleteById(id); } @Override public int updateUser(User user) { return userMapper.update(user); } @Override public User getUserById(Integer id) { return userMapper.selectById(id); } @Override public List<User> getAllUsers() { return userMapper.selectAll(); } } ``` 7. 编写Controller 在Controller中注入UserService,并实现对应的增删接口即可。例如: ```java @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @PostMapping("/add") public int addUser(@RequestBody User user) { return userService.addUser(user); } @DeleteMapping("/{id}") public int deleteUserById(@PathVariable Integer id) { return userService.deleteUserById(id); } @PutMapping("/update") public int updateUser(@RequestBody User user) { return userService.updateUser(user); } @GetMapping("/{id}") public User getUserById(@PathVariable Integer id) { return userService.getUserById(id); } @GetMapping("/all") public List<User> getAllUsers() { return userService.getAllUsers(); } } ``` 这样,我们就完成了Spring整合MyBatis数据库实现增删的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值