zoukankan      html  css  js  c++  java
  • SpringBoot入门二:与Mybatis整合

    一、编程步骤

    1、引入依赖

     springboot相关依赖(略)、mybatis-spring-boot-starter、mysql、druid、lombook

    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.4</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.2.5</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    

     2、整合mybatis

    1)建表

    2)实体类:使用lombook

    @Data
    @Accessors(chain = true)
    @NoArgsConstructor
    @AllArgsConstructor
    public class User {
        private Integer id;
        private String name;
        private Integer age;
        private String email;
    }
    

    3)DAO接口:接口抽象:BaseDao

    //BaseDao
    public interface BaseDao<T> {
        List<T> getAll();
        void insert(T t);
    }
    
    //UserDao
    @Repository
    public interface UserDao extends BaseDao<User> {
    }
    

    4)Mapper配置文件

    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    <mapper namespace="com.icucoder.learn.dao.UserDao">
        <select id="getAll" resultType="user">
            select id, name, age, email
            from user;
        </select>
        <insert id="insert" parameterType="user">
            insert into user
            values (#{id}, #{name}, #{age}, #{email});
        </insert>
    
    </mapper>
    

    5)Service接口

    public interface UserService {
        List<User> getAll();
        void insert(User t);
    }
    

    6)Service接口实现类

    @Service
    @Transactional
    public class UserServiceImpl implements UserService {
        @Autowired
        UserDao userDao;
    
        @Override
        public List<User> getAll() {
            return userDao.getAll();
        }
    
        @Override
        public void insert(User user) {
            System.out.println("insert success");
            userDao.insert(user);
            int i =1/0;
        }
    }
    

     7)配置相关属性

    (1)配置dao扫描位置

    入口类上加上注解:@MapperScan("com.icucoder.learn.dao")//指定dao接口扫描位置

    @SpringBootApplication
    @MapperScan("com.icucoder.learn.dao")//指定dao接口扫描位置
    public class LearnApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(LearnApplication.class, args);
        }
    
    }
    

    (2)在application.properties中配置数据源和Mapper文件位置

    #数据源
    spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot?characterEncoding=UTF-8&serverTimezone=UTC
    spring.datasource.username=root
    spring.datasource.password=xroot
    #Mapper文件位置
    mybatis.mapper-locations=classpath:mapper/*.xml
    mybatis.type-aliases-package=com.icucoder.learn.entity
    

    8)编写Controller测试

    @RestController
    @RequestMapping("/user")
    public class UserController {
        @Autowired
        UserService userService;
    
        @RequestMapping("/all")
        public List<User> all() {
            return userService.getAll();
        }
    
        @RequestMapping("/insert")
        public String insertOne() {
            User user = new User(7, "mayun", 29, "admin@qq.com");
            userService.insert(user);
            return "insert success";
        }
    }
    

     9)服务器附属并访问测试

    http://127.0.0.1:8080/user/all


    如果你真心觉得文章写得不错,而且对你有所帮助,那就不妨小小打赏一下吧,如果囊中羞涩,不妨帮忙“推荐"一下,您的“推荐”和”打赏“将是我最大的写作动力!

    本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.
    qq群 微信
  • 相关阅读:
    【REACT HOOKS】useState是如何保存并更新数据的?
    CSS Modules 使用
    【TS】一些常用的工具类型
    【TS】type和interface的区别
    【TS】unknown类型
    判断虚拟导航栏(NavigationBar)是否显示
    本地的FTP服务器
    Cocos2d-x3.3RC0的Android编译Activity启动流程分析
    Eclipse/MyEclipse 最最常用的快捷键
    anroidstudio log
  • 原文地址:https://www.cnblogs.com/hoaprox/p/14585977.html
Copyright © 2011-2022 走看看