springboot 梳理1--简单整合mybatis

1. pom.xml添加

        <!--mybatis起步依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>
        <!--mysql连接驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>    

 

 

2. application.yml

#db Configuration:
spring:
  #默认datasource hikaricp
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/ssm?characterEncoding=utf8&&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 123456

mybatis:
  mapper-locations: mapper/*.xml
  type-aliases-package: com.xinzhi.studyspringboot.entity

 

3. entity层User

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private int id;
    private String username;
    private String password;
}

 

 

3. dao层UserMapper

@Mapper
public interface UserMapper {
    /**
     * 查询用户
     * @return
     */
    public List<User> queryUserList();
}

 

 

4. resources/mapper/UserMapper.xml

<?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.xinzhi.studyspringboot.dao.UserMapper">
    <select id="queryUserList" resultType="User">
        select * from user
    </select>
</mapper>

 

 

5. service层

 

 

(1)IUserService = UserMapper

(2)UserServiceImpl 实现IUserService,私有变量UserMapper,方法实现

@Service
public class UserServiceImpl implements IUserService {
    @Resource
    private UserMapper userMapper;

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

 

 

6. controller层UserController实现,私有变量IUserService

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userService;

    @GetMapping
    @ResponseBody
    public List<User> getUsers(){
        return userService.queryUserList();
    }
}

 

posted @ 2021-01-29 13:34  Master_Sun  阅读(58)  评论(0编辑  收藏  举报