SpringBoot整合mybatis快速入門(轉(zhuǎn))

一、創(chuàng)建一個SpringBoot項目

image
image
image
image

二、引入相關(guān)依賴

        <!--web核心依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--mysql數(shù)據(jù)庫驅(qū)動-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

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

三、創(chuàng)建如下結(jié)構(gòu)文件

image
  • 編寫實體類com.zhg.demo.mybatis.entity.User
public class User implements Serializable {
    private Long id;//編號
    private String username;//用戶名
    private String password;//密碼
    //省略get set方法
}

  • 編寫接口com.zhg.demo.mybatis.mapper.UserMapper
    注意:需要使用@Mapper注解,不然SpringBoot無法掃描
@Mapper//指定這是一個操作數(shù)據(jù)庫的mapper
public interface UserMapper {
    List<User> findAll();
}

  • 編寫在resources文件中創(chuàng)建 mapper/UserMapper.xml文件
    注意
    1.namespace中需要與使用@Mapper的接口對應(yīng)
    2.UserMapper.xml文件名稱必須與使用@Mapper的接口一致
    3.標(biāo)簽中的id必須與@Mapper的接口中的方法名一致,且參數(shù)一致
<?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.zhg.demo.mybatis.mapper.UserMapper">
    <select id="findAll" resultType="User">
        SELECT * FROM tb_user
    </select>
</mapper>

  • 編寫接口com.zhg.demo.mybatis.service
public interface UserService {
    List<User> findAll();
}

  • 編寫實現(xiàn)類com.zhg.demo.mybatis.service.impl.UserServiceimpl
    注意:需要在接口實現(xiàn)類中使用@Service注解,才能被SpringBoot掃描,在Controller中使用@Authwired注入
@Service("userService")
public class UserServiceimpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }
}

  • 編寫api接口com.zhg.demo.mybatis.controller.UserController
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/findAll")
    public List<User> findAll(){
        return userService.findAll();
    }

}

  • 在啟動類中添加對@MapperScan的掃描
@SpringBootApplication
@MapperScan("com.zhg.demo.mybatis.mapper")//使用MapperScan批量掃描所有的Mapper接口;
public class MybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybatisApplication.class, args);
    }

}

四、配置文件

注意
1.mybatis中的mapper-locations是mapper的xml文件位置
2.mybatis中的type-aliases-package是為了配置xml文件中resultType返回值的包位置,如果未配置請使用全包名如下:

<select id="findAll" resultType="com.zhg.demo.mybatis.entity.User">
        SELECT * FROM tb_user
</select>

  • 在resources中創(chuàng)建application.yml文件,并編寫配置
server:
  port: 8081
spring:
  #數(shù)據(jù)庫連接配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://47.107.105.158:3306/test?characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456

#mybatis的相關(guān)配置
mybatis:
  #mapper配置文件
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.zhg.demo.mybatis.entity
  #開啟駝峰命名
  configuration:
    map-underscore-to-camel-case: true

五、創(chuàng)建數(shù)據(jù)庫和數(shù)據(jù)表

-- ----------------------------
-- Table structure for tb_user
-- ----------------------------
DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user` (
  `id` int(11) NOT NULL,
  `username` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of tb_user
-- ----------------------------
INSERT INTO `tb_user` VALUES ('1', 'laowang', '112233');
INSERT INTO `tb_user` VALUES ('2', 'laoli', '123456');

六、啟動并測試

轉(zhuǎn):http://www.itdecent.cn/p/541874714907

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容