springboot-6.整合Mybatis

springboot入门篇集合

  • springboot-1.简约版创建第一个应用
  • springboot-2.利用接口返回数据(json)
  • springboot-3.资源文件属性配置
  • springboot-4.模板引擎freemarker&thymeleaf
  • springboot-5.全局异常捕获
  • springboot-6.整合Mybatis
  • springboot-7.快速上手redis
     
     
     

前言

这里是我目前为止走的坑最多的地方吧,在这里整理一下经验⑧
 

一 .解析Java框架中entity层,mapper层,service层各层作用

(1)entity层
别名: model层 ,domain层。
用途: 实体层,用于存放我们的实体类,与数据库中的属性值基本保持一致,实现set和get的方法。一般数据库一张表对应一个实体类,类属性同表字段一一对应
简单例子:user表的实体User

public class User {
    private int id;//id
    private String username;//用户名
    //……省略set,get方法
}

 
(2)mapper层
别名: dao层
用途: 对数据库进行数据持久化操作,针对数据库操作的,主要实现增删改查操作
简单例子:

@Mapper
public interface UserMapper {
    public User selectUserById(int id);
}

 
(3) service层
业务逻辑层,调用dao层接口,接收dao层返回的数据,完成项目的基本功能设计。
封装Service层的业务逻辑有利于业务逻辑的独立性和重复利用性。
作用:为controller层的类提供接口进行调用。
简单例子:

public interface UserService extends Service {

}

二.快速使用

(1)添加依赖项

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

        <!--通用mapper-->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>1.1.5</version>
        </dependency>

        <!--druid数据源-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10 </version>
        </dependency>


        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10 </version>
        </dependency>

 
 
(2)配置application.yml

spring:
  #数据源配置
  datasource:
    url: jdbc:mysql://localhost:3306/customer_db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: ******
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    connectionProperties: druid.stat.mergeSql=true

#为mybatis配置,生产环境可删除
restart:
  include:
    mapper: /mapper-[\\w-\\.]+jar
    pagehelper: /pagehelper-[\\w-\\.]+jar

mapper:
  mappers: cn.ericam.nndemo.utils.MyMapper
  not-empty: false
  identity: MYSQL

 
 
(3)编写User.java【entity层】

public class User {
    private String id;
    private String username;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}

 
 
(4)编写UserMapper.java(dao层)

@Mapper
public interface UserMapper {
    @Select("select * from user where id = #{id}")
    public User selectUserById(int id);

    @Select("select * from user where username = #{username}")
    public List<User> selectUserByName(String userName);

    @Insert("insert into user(id,username) values (#{id},#{username})")
    public void addUser(User user);

    @Update("update user set username=#{username} where id=#{id}")
    public void updateUser(User user);

    @Delete("delete from user where id=#{id}")
    public void deleteUser(int id);
}

 
 
(5)编写userController.java

@RestController
@RequestMapping("/user")
public class userController {

    @Autowired
    UserMapper userMapper;

    @RequestMapping(value={"/selectUserById"}, method=RequestMethod.GET)
    public User selectUserById(String id){
        User user = userMapper.selectUserById(Integer.parseInt(id));
        return user;
    }

    @RequestMapping(value={"/selectUserByName"}, method=RequestMethod.GET)
    public List<User> selectUserByName(String userName){
        return userMapper.selectUserByName(userName);
    }

    @RequestMapping(value={"/addUser"}, method=RequestMethod.POST)
    public void addUser(User user){
        userMapper.addUser(user);
    }

    @RequestMapping(value={"/updateUser"}, method=RequestMethod.POST)
    public void updateUser(User user){
        userMapper.updateUser(user);
    }

    @RequestMapping(value={"/deleteUser"}, method=RequestMethod.POST)
    public void deleteUser(String id){
        userMapper.deleteUser(Integer.parseInt(id));
    }
}

此时便已经全部配置结束
为了方便测试,忽略了service层,直接调用了mapper层
 
 
(6)测试
1.
springboot-6.整合Mybatis_第1张图片
 
 
2.
springboot-6.整合Mybatis_第2张图片

你可能感兴趣的:(springboot)