在pom.xml中添加mybatis依赖和mysql驱动
-
- <dependency>
- <groupId>org.mybatis.spring.bootgroupId>
- <artifactId>mybatis-spring-boot-starterartifactId>
- <version>2.2.2version>
- dependency>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <scope>runtimescope>
- dependency>
在application.properties中添加以下信息:
- spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
- spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
- spring.datasource.username=root
- spring.datasource.password=123456
注意:spring.datasource.url=jdbc:mysql://localhost:3306/springboot中springboot是数据库名称
1. 在pom.xml中引入src/main/resouces目录下所有xml文件:
mapper接口的映射文件通常存放在src/main/resouces目录下
- <build>
-
- <resources>
- <resource>
- <directory>src/main/resourcesdirectory>
- <includes>
- <include>**/*.*include>
- includes>
- resource>
- resources>
2. 在application.properties中添加以下信息来指定映射文件存放在resources目录下的那个包内
通常自定义创建一个包,用于存放映射文件。(这里在resources目录下创建了一个mapper包)
mybatis.mapper-locations=classpath:mapper/*.xml
3. 添加日志
在application.properties中添加以下信息:
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
创建数据库表所对应的实体类对象
- package com.xdu.studyspringboot.pojo;
-
- public class User {
- private Integer id;
- private String name;
- private Integer age;
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public Integer getAge() {
- return age;
- }
-
- public void setAge(Integer age) {
- this.age = age;
- }
-
- @Override
- public String toString() {
- return "User{" +
- "id=" + id +
- ", name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
- }
- package com.xdu.studyspringboot.mapper;
-
- import com.xdu.studyspringboot.pojo.User;
- import org.apache.ibatis.annotations.Mapper;
- import org.apache.ibatis.annotations.Param;
-
- @Mapper
- public interface UserMapper {
- User selectById(@Param("id") Integer id);
- }
在src/main/resources/mapper目录下创建与Mapper接口同名的xml文件,即UserMapper.xml
- "1.0" encoding="UTF-8" ?>
- mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.xdu.studyspringboot.mapper.UserMapper">
-
- <select id="selectById" resultType="com.xdu.studyspringboot.pojo.User">
- select * from user where id=#{id}
- select>
-
- mapper>
- package com.xdu.studyspringboot.service;
-
- import com.xdu.studyspringboot.pojo.User;
-
- public interface UserService {
- User queryUserById(Integer id);
- }
- package com.xdu.studyspringboot.service.impl;
-
- import com.xdu.studyspringboot.mapper.UserMapper;
- import com.xdu.studyspringboot.pojo.User;
- import com.xdu.studyspringboot.service.UserService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- @Service
- public class UserServiceImpl implements UserService {
- @Autowired
- private UserMapper userMapper;
-
- @Override
- public User queryUserById(Integer id) {
- return userMapper.selectById(id);
- }
- }
- package com.xdu.studyspringboot.controller;
-
- import com.xdu.studyspringboot.pojo.User;
- import com.xdu.studyspringboot.service.UserService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
-
- @Controller
- public class UserController {
- @Autowired
- private UserService userService;
-
- @RequestMapping("/user")
- @ResponseBody
- public String queryUser(){
- Integer id = 1;
- User user = userService.queryUserById(id);
- return user.toString();
- }
- }