栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【Java从零到架构师第③季】【43】SpringBoot-MyBatis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Java从零到架构师第③季】【43】SpringBoot-MyBatis


持续学习&持续更新中…

守破离


【Java从零到架构师第③季】【43】SpringBoot-MyBatis
    • 引入依赖
    • 数据源配置
    • MyBatis配置
    • 扫描DAO
    • MyBatis主配置—XML
    • MyBatis主配置—注解
    • MyBatis主配置—application.yml
    • starter的命名规范
    • useGeneratedKeys
    • 实例
        • 项目结构
        • Application
        • MyBatisConfig
        • application.yml 与 MyBatis映射文件
        • mybatis-config.xml
        • index.html
        • controller
        • service
        • dao
    • 参考

引入依赖

http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

    
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.3
        
        
            mysql
            mysql-connector-java
        
        
            com.alibaba
            druid-spring-boot-starter
            1.2.1
        

		
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
    
数据源配置

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/test_mybatis?serverTimezone=UTC
    druid:
      initial-size: 5
      max-active: 10
MyBatis配置

扫描DAO

MyBatis主配置—XML

mybatis:
  config-location: classpath:mybatis-config.xml



    
        
        
    

MyBatis主配置—注解

@Configuration
public class MyBatisConfig {
    @Bean
    public ConfigurationCustomizer customizer() {
        return (configuration) -> {
            configuration.setMapUnderscoreToCamelCase(true);
        };
    }
}
MyBatis主配置—application.yml

mybatis:
  configuration:
    map-underscore-to-camel-case: true
starter的命名规范

https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-starter

useGeneratedKeys

useGeneratedKeys直接写在映射文件中:




    
        INSERT INTO skill(name, level) VALUES (#{name}, #{level})
    

实例 项目结构

Application
@SpringBootApplication
@MapperScan("programmer.lp.dao")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
MyBatisConfig
@Configuration
public class MyBatisConfig {
    @Bean
    public ConfigurationCustomizer customizer() {
        return (configuration) -> {
            configuration.setMapUnderscoreToCamelCase(true);
            configuration.setUseGeneratedKeys(true);
        };
    }
}
application.yml 与 MyBatis映射文件

server:
  servlet:
    context-path: /mybatis
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/test_mybatis?serverTimezone=UTC
    druid:
      initial-size: 5
      max-active: 10
mybatis:
  type-aliases-package: programmer.lp.domain
  #  mapper-locations: classpath:mappers*.properties
                    ***.java -->
                    
                
            
        
    

mybatis-config.xml



    
    
        
        
        
        
    

index.html



    
    Skill







保存
删除
controller
@RestController
@RequestMapping("/skills")
public class SkillController {
    @Autowired
    private SkillService service;

    @GetMapping("/list")
    public List list() {
        return service.list();
    }

    @GetMapping("/get")
    public Skill get(Integer id) {
        return service.get(id);
    }

    @PostMapping("/save")
    public String save(Skill skill) {
        String[] msgs;
        Integer id = skill.getId();
        if (id != null && id > 0) {
            msgs = new String[] {"更新成功", "更新失败"};
        } else {
            msgs = new String[] {"添加成功", "添加失败"};
        }
        return service.save(skill) ? msgs[0] + "_" + skill.getId() : msgs[1];
    }

    @PostMapping("/remove")
    public String remove(Integer id) {
        return service.remove(id) ? "删除成功" : "删除失败";
    }
}
service
@Service
@Transactional
public class SkillServiceImpl implements SkillService {
    @Autowired
    private SkillDao dao;

    @Override
    public boolean save(Skill skill) {
        System.out.println(skill.getId());
        Integer id = skill.getId();
        if (id == null || id < 1) {
            return dao.save(skill);
        }
        return dao.update(skill);
    }

    @Override
    @Transactional(readOnly = true)
    public List list() {
        return dao.list();
    }

    @Override
    @Transactional(readOnly = true)
    public Skill get(Integer id) {
        return dao.get(id);
    }

    @Override
    public boolean remove(Integer id) {
        return dao.remove(id);
    }
}
public interface SkillService {
    boolean save(Skill skill);
    List list();
    Skill get(Integer id);
    boolean remove(Integer id);
}
dao
public interface SkillDao {
    boolean save(Skill skill);
    boolean update(Skill skill);
    boolean remove(Integer id);
    @Select("SELECT * FROM skill")
    List list();
    @Select("SELECT * FROM skill WHERe id = #{id}")
    Skill get(Integer id);
}



    
        INSERT INTO skill(name, level) VALUES (#{name}, #{level})
    

    
        UPDATE skill SET name = #{name}, level = #{level} WHERe id = #{id}
    

    
        DELETE FROM skill WHERe id = #{id}
    

参考

小码哥-李明杰: Java从0到架构师③进阶互联网架构师.


本文完,感谢您的关注支持!


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/837347.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号