spring-boot-route(八)整合mybatis操作数据库

spring-boot-route(八)整合mybatis操作数据库

是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Java ,普通的 Java对象)映射成数据库中的记录。

通过注解完成数据操作

第一步:引入mysql依赖和依赖


    mysql
    mysql-connector-java


    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    LATEST

第二步:新建学生表及对应的实体类

CREATE TABLE `student` (
   `student_id` int(30) NOT NULL AUTO_INCREMENT,
   `age` int(1) DEFAULT NULL COMMENT '年龄',
   `name` varchar(45) DEFAULT NULL COMMENT '姓名',
   `sex` int(1) DEFAULT NULL COMMENT '性别:1:男,2:女,0:未知',
   `create_time` datetime DEFAULT NULL COMMENT '创建时间',
   `status` int(1) DEFAULT NULL COMMENT '状态:1:正常,-1:删除',
   PRIMARY KEY (`student_id`)
 ) ENGINE=InnoDB AUTO_INCREMENT=617354 DEFAULT CHARSET=utf8mb4 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC COMMENT='学生表'

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student implements Serializable {
    private static final long serialVersionUID = 6712540741269055064L;
    private Integer studentId;
    private Integer age;
    private String name;
    private Integer sex;
    private Date createTime;
    private Integer status;
}

第三步:配置数据库连接信息

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/simple_fast
    username: root
    password: root

增删改查

@Mapper
public interface StudentMapper {
    @Select("select * from student where student_id = #{studentId}")
    Student findById(@Param("studentId") Integer studentId);
    @Insert("insert into student(age,name) values(#{age},#{name})")
    int addStudent(@Param("name") String name,@Param("age") Integer age);
    @Update("update student set name = #{name} where student_id = #{studentId}")
    int updateStudent(@Param("studentId") Integer studentId,@Param("name") String name);
    @Delete("delete from student where student_id = #{studentId}")
    int deleteStudent(@Param("studentId") Integer studentId);
}

bf57435b2b3c61632126d9172a19aeb2

上面演示的传参方式是通过单个参数传递的sql xml path,如果想通过Map或实体类传参数spring-boot-route(八)整合mybatis操作数据库spring-boot-route(八)整合mybatis操作数据库,就不需要使用@Param来绑定参数了,将map中的key或者实体类中的属性与sql中的参数值对应上就可以了。

通过XML配置完成数据操作

@和@

@加在数据层接口上,将其注册到ioc容器上sql xml path,@加在启动类上,需要指定扫描的数据层接口包。如下:

@Mapper
public interface StudentMapper {}

@SpringBootApplication
@MapperScan("com.javatrip.mybatis.mapper")
public class MybatisApplication {
    public static void main(String[] args) {
        SpringApplication.run(MybatisApplication.class, args);
    }
}

两个注解的作用一样sql xml path,在开发中为了方便,通常我们会使用@。

指定.xml的位置

mybatis:
  mapper-locations: classpath:mybatis/*.xml

开启数据实体映射驼峰命名

mybatis:
  configuration:
    map-underscore-to-camel-case: true

编写xml和与之对应的接口




    
        select * from student where student_id = #{studentId}
    
    
        insert into student(name,age) values(#{name},#{age})
    
    
    
        update student set name = #{name} where  student_id = #{studentId}
    
    
        delete from student where student_id = #{studentId}
    

@Mapper
public interface StudentXMapper {
    Student findById(@Param("studentId") Integer studentId);
    int addStudent(Student student);
    int updateStudent(@Param("studentId") Integer studentId,@Param("name") String name);
    int deleteStudent(@Param("studentId") Integer studentId);
}

编写测试类

@SpringBootTest
class MybatisApplicationTests {
    @Autowired
    StudentMapper mapper;
    @Autowired
    StudentXMapper xMapper;
    @Test
    void testMapper() {
        Student student = mapper.findById(10);
        mapper.addStudent("Java旅途",19);
        mapper.deleteStudent(31);
        mapper.updateStudent(10,"Java旅途");
    }
    @Test
    void contextLoads() {
        
        Student student = xMapper.findById(10);
        Student studentDo = new Student();
        studentDo.setAge(18);
        studentDo.setName("Java旅途呀");
        xMapper.addStudent(studentDo);
        xMapper.deleteStudent(32);
        xMapper.updateStudent(31,"Java旅途");
    }
}

这里有几个需要注意的点:标签中属性对应的是接口;标签的id对应接口中的方法名字;标签的对应查询的实体类,使用全路径。

公告:
1. 本站所有资源来源于用户上传和网络,如有侵权请联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系站长处理!
6. 本站不售卖代码,资源标价只是站长收集整理的辛苦费!如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。
7. 站长QQ号码 2205675299

资源库 - 资源分享下载网 » spring-boot-route(八)整合mybatis操作数据库

常见问题FAQ

关于资源售价和售后服务的说明?
本站所有资源的标价均为本站收集资源的辛苦费,不代表资源本身的价值。软件是高智慧高价值的商品,不可能是白菜价。本站资源标价只是赞助费用,收取的赞助费仅用来维持本站的日常运营!毕竟收集整理资料需要投入云计算资源和站长大量的精力。
代码有没有售后服务和技术支持?
由于代码的运行具有不可预见性,本站不保证代码完整可运行,不提供技术支持和售后服务。 本站原创代码都是站长自己开发的,可以有偿提供技术支持服务。 网站里标明【亲测】的代码都是站长亲测过的,其他的代码由于精力有限,没有一一测试,不能保证代码就一定能够使用,更没有技术支持服务,下载前请自行斟酌。
有没有搭建服务?
由于搭建服务比较费时费力,所以本站除了原创代码外均不提供搭建服务。本站分享代码纯属兴趣爱好,不以盈利为目的,请勿咨询有没有搭建服务,谢谢理解。
链接地址失效了怎么办?
请带上资源链接地址联系客服,工作时间内我们看到后将第一时间回复。
关于解压密码
本站资源一般都没有加密,如果发现需要解压密码的,那么就输入 hao.35dc.com 试试。

发表评论

资源库,由老程序员细心甄别、精心筛选,只为提供优质的源码资源

关于我们 联系我们