zoukankan      html  css  js  c++  java
  • 整合MyBatis

    配置数据源相关属性(见前一章节 DataSource配置

    引入依赖

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

    application.properties

    mybatis.mapper-locations=classpath:public/mybatis/mapper/*.xml 
    mybatis.config-location=classpath:public/mybatis/mybatis-config.xml

    mybatis-config.xml

    <?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>
    
        <settings>
            <setting name="mapUnderscoreToCamelCase" value="true"/>
        </settings>
    </configuration>

    EmployeeMapper.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.atguigu.springBoot01.mapper.EmployeeMapper">
        <!-- public Employee getEmpById(Integer id); public void insertEmp(Employee 
            employee); -->
    
        <resultMap type="com.atguigu.springBoot01.entity.Employee" id="empMap">
            <id column="id" property="id" />
            <result column="lastName" property="lastName" />
            <result column="email" property="email" />
            <result column="gender" property="gender" />
            <result column="department_id" property="department.id" />
        </resultMap>
    
        <select id="getEmpById" resultMap="empMap">
            SELECT * FROM t_employee WHERE id=#{id}
        </select>
    
        <insert id="insertEmp" parameterType="com.atguigu.springBoot01.entity.Employee">
            INSERT INTO t_employee(last_name,email,gender,department_id) VALUES
            (#{lastName},#{email},#{gender},#{department.id})
        </insert>
    </mapper>

    EmployeeMapper接口

    package com.atguigu.springBoot01.mapper;
    
    import com.atguigu.springBoot01.entity.Employee;
    
    public interface EmployeeMapper {
        
        public Employee getEmpById(Integer id);
        
        public void insertEmp(Employee emp);
    
    }

    使用MapperScan批量扫描所有的Mapper接口

    @SpringBootApplication
    @MapperScan(value = "com.atguigu.springBoot01.mapper")
    public class Application {
    public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
  • 相关阅读:
    浅谈C++ STL中的优先队列(priority_queue)
    哈夫曼树与哈夫曼编码
    Binary Search 的递归与迭代实现及STL中的搜索相关内容
    Prime 算法的简述
    估算网站需要多少宽带方法
    微服务服务拆分步骤
    第一次有人把科创板讲得这么简单明了
    一个人优秀到可怕的三个迹象!一旦具备,必为人中龙凤,大富大贵
    美元加息怎么“剪羊毛”
    英国脱欧的影响
  • 原文地址:https://www.cnblogs.com/xiaoliangup/p/10409214.html
Copyright © 2011-2022 走看看