zoukankan      html  css  js  c++  java
  • springBoot mybatis配置

    1.pom 配置

     <!--mybatis配置 start-->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.2.0</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.39</version>
            </dependency>
            <!--mybatis配置 end-->

    2.resources资源中application.yml 配置

    spring:
      datasource:
        url: jdbc:mysql://192.168.0.1:3306/数据库?useUnicode=true&characterEncoding=utf8
        username: root
        password: 123456
        driver-class-name: com.mysql.jdbc.Driver
    mybatis:
    mapper-locations: classpath:mybatis-mappers/*.xml

    3.如果没有resources资源文件夹,可先建立resources 文件夹

    file-》project structure-》Modules-》sources-》 找到 resources文件夹

    点击红框中resources  点击 Apply   OK 就可以了

    4.建立实体包 entity

    创建user实体类

    @Setter
    @Getter
    public class user {
        private String id;
        private String name;
        private int sex;
    }//end

    5.建立mapper包

    创建 userMapper 接口

    @Repository
    public interface userMapper {
        @Select("select * from user where id=#{id}")
        user selectUser(String id);
    }//end

    另一种方式用xml配置

    @Repository
    public interface userMapper{boolean insertList(List<user> list);
    }//end

    xml 需要配置到 resources 资源文件夹下 mybatis-mappers 中

    <?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="userMapper包的全路径">
        <insert id="insertList" >
            insert into user
            (id,name,sex)
            values
            <foreach collection="list" item="item" index="index" separator=",">
                (
                #{item.id},
                #{item.name},
                #{item.sex}
                )
            </foreach>
        </insert>
    </mapper>

    6.建立service包

    创建 UserService 接口

    public interface UserService {
        user selectUser(String id);
    }//end

    创建实现类 UserServiceImpl

    @Service
    public class UserServiceImpl implements UserService {
        @Autowired
        userMapper _userMapper;
    
        @Override
        public user selectUser(String id) {
            return _userMapper.selectUser(id);
        }
    }//end

    7. 创建testController 类

    @RestController
    @RequestMapping(value = "/test")
    public class testController {
        @Autowired
        private UserService _UserService;
    
        @RequestMapping(value = "/select/{id}")
        public String selectUser(@PathVariable String id) {
            return _UserService.selectUser(id).getName();
        }
    }//end

    8.主程序入口,加入Mapper 扫包范围

    @MapperScan("mapper包路径")
    @SpringBootApplication
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
    }
    欢迎指正:haizi2014@qq.com
  • 相关阅读:
    Java结束线程的三种方法(爱奇艺面试)
    Threadlocal 传递参数(百度二面)
    数据一致性 kafka 是保存副本 leader读写,follower 只备份 而 zookeeper是 leader 读写,follower负责读
    Mysql 间隙锁原理,以及Repeatable Read隔离级别下可以防止幻读原理(百度)
    SOA,SOAP,RPC,以及 RPC协议与 REST 协议之间的关系(搜狗)
    select、poll、epoll之间的区别(搜狗面试)
    windows日志查看-非法关机判断方法
    望帝春心托杜鹃 中望帝的由来~
    深入解读Quartz的原理
    Hibernate性能优化之EHCache缓存
  • 原文地址:https://www.cnblogs.com/hcfan/p/9888325.html
Copyright © 2011-2022 走看看