文章目录
SpringBoot配置Mybatis:详细易懂
前期准备工作
Mybatis相应配置
编写相应代码
文件结构和结果
增删查改
Mybatis 动态SQL

参考文章

SpringBoot配置Mybatis:详细易懂
Mybatis作为后端持久层框架,在互联网大厂中应用广泛,所以掌握Mybatis,可谓是必备的。最近准备系统得复习一下Mybatis框架,所以博客会更几期关于Mybatis得文章,如果觉得看完有收获,希望收藏、点赞。如果觉得写得不好,欢迎评论区指正。
前期准备工作
Mybatis相应配置
修改pom.xml,获取Mybatis、MySQL相关依赖
mysql
mysql-connector-java
runtime
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.2
compile
/code>
code class='prism'>mybatis.type-aliases-package=com.test.demo
#mysql驱动
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#远程数据库链接 serverTimezone不可少
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
#MySQL数据库用户名、密码
spring.datasource.username=root
spring.datasource.password=xxxxx
#xml格式的mapper文件位置
mybatis.mapper-locations=classpath:/mapper/*.xml
/code>
code class='prism'>package com.test.demo;
public class Student {
public int sNo;
public String sName;
public int sAge;
public Student(int sNo, String sName, int sAge) {
this.sNo = sNo;
this.sName = sName;
this.sAge = sAge;
}
//Getter、Setter方法,我省略了,你在代码中别省略
}
/code>
code class='prism'>package com.test.demo.mapper;
import com.test.demo.Student;
public interface StudentMapper {
void insert(Student student);
}
/code>
br />
code class='prism'>
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
INSERT INTO student (Sno,Sname,Sage) VALUES (#{
sNo},#{
sName},#{
sAge})
/pre>
p>最后的controller代码文件
/p>
pre class='language-javascript'>
package com.test.demo.controller;
import org.springframework.web.bind.annotation.RestController;
import com.test.demo.Student;
import com.test.demo.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class HomeController {
@Autowired
private StudentMapper studentMapper;
@RequestMapping("/")
public String index() throws Exception {
studentMapper.insert(new Student(1, "天宇", 24));
return "OK";
}
}/pre>
h4>文件结构和结果
/h4>
h4>增删查改
/h4>
p>mapper接口
/p>
pre class='language-javascript'>
package com.test.demo.mapper;
import com.test.demo.Student;
public interface StudentMapper {
void insert(Student student);
void delete(String sNo);
Student selectByNumber(Integer sNo);
void updateName(Student student);
}/pre>
p>对应xml文件
/p>
p>参数的类型一定是完整的包名,参数名写ben类的属性名
/p>
pre class='language-javascript'>
INSERT INTO student (Sno,Sname,Sage) VALUES (#{sNo},#{sName},#{sAge})
DELETE FROM student where Sno = #{sNo};
UPDATE student SET Sname = #{sName} where Sno = #{sNo}
Mybatis 动态SQL
在实际应用开发过程中,我们往往需要写复杂的 SQL 语句,需要拼接,而拼接SQL语句又稍微不注意,由于引号,空格等缺失可能都会导致错误。Mybatis提供了动态SQL,也就是可以根据用户提供的参数,动态决定查询语句依赖的查询条件或SQL语句的内容。
if、foreach关键字用法
mapper接口
public interface StudentMapper {
List findByCondition(String sName, Integer sAge);
List selectByNumberList(List list);
} 对应的xml格式mapper文件
参考文章
简单,Spring boot 配置mybatis
Mybatis入门看这一篇就够了
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/121080.html