Springboot整合mybatis的步骤
程序员文章站
2022-07-10 15:58:56
前期工作1.导入mybatis整合依赖
2.连接数据库
前期工作
1.导入mybatis整合依赖
<!-- mybatis整合 --> <dependency> <groupid>org.mybatis.spring.boot</groupid> <artifactid>mybatis-spring-boot-starter</artifactid> <version>2.1.4</version> </dependency>
2.连接数据库
3.连接完数据库就去applicaton.yml配置一下数据库
spring: datasource: username: root password: 123456 url: jdbc:mysql://localhost:3306/mybatis?servertimezone=utc&useunicode=true&characterencoding=utf-8 driver-class-name: com.mysql.cj.jdbc.driver
开始整合
1.编写与数据库对应的实体类
package com.example.pojo; import lombok.allargsconstructor; import lombok.data; import lombok.noargsconstructor; @data @allargsconstructor @noargsconstructor public class user { private integer id; private string name; private string pwd; }
为了偷懒,导入了lombok
<!-- lombok --> <dependency> <groupid>org.projectlombok</groupid> <artifactid>lombok</artifactid> </dependency>
2.编写mapper
package com.example.mapper; import com.example.pojo.user; import org.apache.ibatis.annotations.mapper; import org.springframework.stereotype.repository; import java.util.list; @mapper @repository public interface usermapper { //查询用户的全部信息 list<user> getuserlist(); //select 找出id=1的用户 user getuserbyid(int id); //insert 增加一个用户 int insertuser(user user); //delete 删除id=4的用户 int deleteuser(int id); //update 将id=2的用户名字改为小龙 int updateuser(user user); }
3.编写mapper.xml文件
<?xml version="1.0" encoding="utf-8" ?> <!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.mapper.usermapper"> <select id="getuserlist" resulttype="user"> select * from mybatis.user </select> <select id="getuserbyid" resulttype="user" parametertype="int"> select * from mybatis.user where id = #{id}; </select> <insert id="insertuser" parametertype="user"> insert into mybatis.user (id, name, pwd) values (#{id}, #{name}, #{pwd}); </insert> <delete id="deleteuser" parametertype="int"> delete from mybatis.user where id = #{id}; </delete> <update id="updateuser" parametertype="user"> update mybatis.user set name = #{name}, pwd = #{pwd} where id = #{id}; </update> </mapper>
这里我们用了别名而且我们把这个mapper.xml文件放在了resources目录下,所以我们要去application.yml配置一下
mybatis: type-aliases-package: com.example.pojo mapper-locations: classpath:mybatis/mapper/*.xml
mapper.xml文件编写的位置:
4.编写controller
package com.example.controller; import com.example.mapper.usermapper; import com.example.pojo.user; import org.apache.ibatis.annotations.param; import org.springframework.beans.factory.annotation.autowired; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.pathvariable; import org.springframework.web.bind.annotation.restcontroller; import java.util.list; @restcontroller public class usercontroller { @autowired private usermapper usermapper; @getmapping("/getuserlist") public list<user> getuserlist(){ return usermapper.getuserlist(); } @getmapping("/getuserbyid/{id}") public user getuserbyid(@pathvariable("id") int id){ return usermapper.getuserbyid(id); } @getmapping("/insertuser") public string insertuser(){ usermapper.insertuser(new user(5,"xiaoming","111")); return "ok"; } @getmapping("/deleteuser") public string deleteuser(){ usermapper.deleteuser(5); return "ok"; } @getmapping("/updateuser") public string updateuser(){ usermapper.updateuser(new user(5,"xx","111")); return "ok"; } }
5.进行测试
以上就是springboot整合mybatis的步骤的详细内容,更多关于springboot整合mybatis的资料请关注其它相关文章!
上一篇: 宇文赟做过哪些“大事”?每件事都离经叛道
下一篇: Python简洁优雅的推导式示例详解
推荐阅读
-
SpringBoot整合MongoDB的步骤详解
-
带着新人学springboot的应用01(springboot+mybatis+缓存 上)
-
SpringBoot无废话入门04:MyBatis整合
-
JAVA WEB快速入门之从编写一个基于SpringBoot+Mybatis快速创建的REST API项目了解SpringBoot、SpringMVC REST API、Mybatis等相关知识
-
Springboot整合redis步骤
-
SpringBoot整合JDBC的实现
-
springboot+springmvc+mybatis项目整合
-
SpringBoot整合Elasticsearch详细步骤以及代码示例(附源码)
-
SpringBoot 整合jdbc和mybatis
-
SpringBoot整合mybatis访问时报错Invalid bound statement (not found)