steps

  • Importing Maven dependencies
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> The < version > 2.1.4 < / version > < / dependency >Copy the code
  • Write mapper interface, annotate @Mapper@Repository
@Mapper
@Repository
public interface PeopleMapper {
    List<People> getPeopleList(a);
    People getPeopleById(int id);
    int addPeople(People people);
    int updatePeople(People people);
    int deletePeople(int id);
}
Copy the code
  • Write SQL, write mapper.xml

      
<! DOCTYPEmapper
        PUBLIC "- / / mybatis.org//DTD Mapper / 3.0 / EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.boot.mapper.PeopleMapper">
    <insert id="addPeople">
        insert into  mybatis.people (id,name,age,address) values (#{id},#{name},#{age},#{address})
    </insert>
    <update id="updatePeople">
        update mybatis.people set name=#{name},age=#{age},address=#{address} where id=#{id}
    </update>
    <delete id="deletePeople" parameterType="int">
        delete from mybatis.people where id=#{id}
    </delete>
    <select id="getPeopleList" resultType="com.boot.pojo.People">
         select * from mybatis.people
    </select>
    <select id="getPeopleById" resultType="com.boot.pojo.People" parameterType="int">
         select * from mybatis.people where id=#{id}
    </select>
</mapper>
Copy the code
  • The configuration application. Yaml
spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/mybatis? useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
  type-aliases-package: com.boot.pojo Set the package alias
  mapper-locations: classpath:mapper/*.xml # locate mapper XML
Copy the code