详解SpringBoot 快速整合MyBatis(去XML化)
程序员文章站
2024-02-26 10:14:10
序言:
此前,我们主要通过xml来书写sql和填补对象映射关系。在springboot中我们可以通过注解来快速编写sql并实现数据访问。(仅需配置:mybatis.c...
序言:
此前,我们主要通过xml来书写sql和填补对象映射关系。在springboot中我们可以通过注解来快速编写sql并实现数据访问。(仅需配置:mybatis.configuration.map-underscore-to-camel-case=true)。为了方便大家,本案例提供较完整的层次逻辑springboot+mybatis+annotation。
具体步骤
1. 引入依赖
在pom.xml 引入orm框架(mybaits-starter)和数据库驱动(mysql-conn)的依赖。
<?xml version="1.0" encoding="utf-8"?> <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"> <!--继承信息 --> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.0.0.m4</version> <relativepath/> </parent> <!--依赖管理 --> <dependencies> <dependency> <!--添加web依赖 --> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <!--添加mybatis依赖 --> <groupid>org.mybatis.spring.boot</groupid> <artifactid>mybatis-spring-boot-starter</artifactid> <version>1.3.1</version> </dependency> <dependency><!--添加mysql驱动依赖 --> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <scope>runtime</scope> </dependency> <dependency><!--添加test依赖 --> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </dependencies> </project>
2. 添加数据源
在application.yml 添加数据源,以及开启mybaits的驼峰映射功能。
spring: datasource: url: jdbc:mysql://localhost:3306/socks?usessl=false username: root password: root driver-class-name: com.mysql.jdbc.driver mybatis: configuration: map-underscore-to-camel-case: true #开启驼峰映射
3. 编写数据层代码
// pojo类如下: public class user { private string userid; private string username; private string password; // getters & setters .. } // 数据层代码如下: public interface usermapper { @select("select * from t_user where 1=1") list<user> list(); @select("select * from t_user where username like #{username}") list<user> findbyusername(string username); @select("select * from t_user where user_id like #{userid}") user getone(string userid); @delete("delete from t_user where user_id like #{userid}") int delete(string userid); }
4. 添加数据库记录
在navicat 连接本地数据库,随便打开查询窗口,复制下面这段脚本,点击执行即可。
drop database if exists `socks`; create database `socks`; use `socks`; drop table if exists `t_user`; create table `t_user` ( `user_id` varchar(50) , `username` varchar(50) , `password` varchar(50) ) ; insert into `t_user` values ('1', 'admin', 'admin'); insert into `t_user` values ('2', 'yizhiwazi', '123456');
5. 启动项目
@springbootapplication @mapperscan("com.hehe.mapper") //扫描mapper接口 public class mybatisapplication { public static void main(string[] args) { springapplication.run(mybatisapplication.class, args); } }
6. 单元测试
import static org.assertj.core.api.assertions.assertthat; @runwith(springrunner.class) @springboottest public class mybatisapplicationtest { @suppresswarnings("all") @autowired usermapper usermapper; @test public void test_db() { //开始进行测试 assertthat(usermapper.list().size()).isgreaterthan(1); assertthat(usermapper.getone("1")).isnotequalto(null); assertthat(usermapper.getone("xxx")).isequalto(null); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。