集成Mybatis

2022/4/19 23:19:01

本文主要是介绍集成Mybatis,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

(1)引入start(JDBC,MySQl,MyBatis)

<dependency>   <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.0</version>
</dependency>

(2)配置数据库

spring.datasource.username=root
spring.datasource.password=159263487qwe
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

#整合MyBatis
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
mybatis.type-aliases-package=com.my.pojo

(3)实体类、mapper

@Mapper
@Repository
public interface UserMapper {
    List<User> queryAll ();
    User queryById(int id);
    int add (User user);
    int delete (int id);
    int update (User user);
}

(4)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.my.mapper.UserMapper">
    <select id="queryAll" resultType="User">
        select * from user
    </select>
    <select id="queryById" resultType="User">
        select * from user where id = #{id}
    </select>
</mapper>

(5)Controller
调用Servce层方法



这篇关于集成Mybatis的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程