1.创建项目时勾选以下模块

2.注解版

注解版的Mapper

package com.moti.mapper;

import com.moti.bean.Department;
import org.apache.ibatis.annotations.*;

//指定这是一个操作数据库的mapper
@Mapper
public interface DepartmentMapper {
    @Select("select * from department where id = #{id}")
    public Department getDeptById(int id);
    @Delete("delete from department where id = #{id}")
    public int deleteDeptById(int id);

    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into department(departmentName) values (#{departmentName})")
    public int addDept(Department department);
    @Update("update department set departmentName = #{departmentName} where id = #{id}")
    public int updateDeptById(Department department);
}

注解版的全局配置文件

package com.moti.config;

import org.apache.ibatis.session.Configuration;
import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
import org.springframework.context.annotation.Bean;

@org.springframework.context.annotation.Configuration
public class MyBatisConfig {

    @Bean
    public ConfigurationCustomizer configurationCustomizer(){
        return new ConfigurationCustomizer(){

            @Override
            public void customize(Configuration configuration) {
                //开启驼峰命名
                configuration.setMapUnderscoreToCamelCase(true);
            }
        };
    }
}

3.配置文件版

全局配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <!--关于MyBatis的一些常用配置-->
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING" />
        <setting name="cacheEnabled" value="true"/>
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

</configuration>

Mapper映射文件

<?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.moti.mapper.EmployeeMapper">
    <select id="getEmpById" resultType="com.moti.bean.Employee">
        select * from employee where id = #{id}
    </select>
    <insert id="addEmp">
        insert into employee(lastName,email,gender,d_id) values (#{lastName},#{email},#{gender},#{dId})
    </insert>
</mapper>

这里需要在SpringBoot的配置文件中配置MyBatis配置文件的位置,如下

mybatis:
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
最后修改日期:2020-07-12

作者

留言

Nice read, I just passed this onto a friend who was doing a little research on that. And he actually bought me lunch since I found it for him smile Therefore let me rephrase that: Thank you for lunch! Glennie Tomas Nichola

I really like and appreciate your blog post. Keep writing. Kirstin Steffen Fortunato

Good article! We are linking to this particularly great post on our site. Keep up the great writing. Dorine Ingram Moody

Hi there, yes this post is in fact good and I have learned lot of things from it concerning blogging. thanks.| Adi Germaine Hepzi

Every as soon as in a whilst we choose blogs that we study. Listed below would be the most up-to-date web pages that we select Sheela Maxie Gotthelf

You ought to be a part of a contest for one of the finest websites on the net. I will highly recommend this blog! Leena Nobie Adham

Wow, amazing blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your website is fantastic, as well as the content! Maisey Birch Sayce

If some one needs to be updated with most up-to-date technologies afterward he must be pay a visit this web page and be up to date every day. Becka Gregg Yelich

Very neat article post. Thanks Again. Keep writing. Leontyne Torey Chaney

If some one wishes expert view on the topic of running a blog then i propose him/her to pay a visit this webpage, Keep up the fastidious job. Salomi Ban Vizza Eilis Myles Marguerita

A person essentially help to make severely articles I might state. This is the first time I frequented your web page and thus far? I surprised with the analysis you made to make this particular publish incredible. Fantastic task! Gilda Valentijn Crain

Thank you for this blog Charlotte! I agree that the separation anxiety was always on my part. Thanks for blessing us with your wisdom! Angela Billy Hurley

Amazing! Its in fact awesome piece of writing, I have got much clear idea on the topic of from this piece of writing. Cassandry Ciro Vlad

Yay google is my world beater helped me to find this great site! Andie Stewart Birdella

撰写回覆或留言

发布留言必须填写的电子邮件地址不会公开。