springboot配置注解版的mybatis和mybati-plus

引入maven

  <dependency>
     <groupId>com.alibaba</groupId>
      <artifactId>druid-spring-boot-starter</artifactId>
      <version>1.1.17</version>
  </dependency>
  <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.1.4</version>
  </dependency>

修改配置文件

spring:
datasource:
  url: jdbc:mysql://localhost:3306/shop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
  username: root
  password: 12345
  driver-class-name: com.mysql.cj.jdbc.Driver
  druid:
    aop-patterns: com.atguigu.admin.*  #监控SpringBean
    filters: stat,wall     # 底层开启功能,stat(sql监控),wall(防火墙)
    stat-view-servlet:   # 配置监控页功能
      enabled: true
      login-username: admin
      login-password: 123456
      resetEnable: false
    web-stat-filter:  # 监控web
      enabled: true
      urlPattern: /*
      exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
    filter:
      stat:    # 对上面filters里面的stat的详细配置
        slow-sql-millis: 1000
        logSlowSql: true
        enabled: true
      wall:
        enabled: true
        config:
          drop-table-allow: false

# 配置mybatis规则
mybatis:
#  config-location: classpath:mybatis/mybatis-config.xml  #全局配置文件位置
mapper-locations: classpath:mybatis/mapper/*.xml  #sql映射文件位置
configuration:
  map-underscore-to-camel-case: true  #开启驼峰命名法

#可以不写全局;配置文件,所有全局配置文件的配置都放在configuration配置项中即可

编写Mapper接口

@Mapper
public interface UserMapper {
	//插入完成后返回自增的主键id赋值给User里的id属性
    @Insert("insert into tb_user(name,age) values(#{name},#{age})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(User user);

    @Select("select * from tb_user where id=#{id}")
    User getUserById(Long id);
}

@Service
public class UserServiceIpml implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public void insert(User user) {
        userMapper.insert(user);
    }

    @Override
    public User getUserById(Long id) {
        return userMapper.getUserById(id);
    }
}
@Slf4j
@RestController
@RequestMapping("/hello")
public class HelloController {
	@Autowired
    private UserService userService;
	@GetMapping("/user/{id}")
    public User getUser(@PathVariable("id") Long id) {
        log.info("id" + id);
        return userService.getUserById(id);
    }

    @PostMapping("/user")
    public User getUser(User user) {
        userService.insert(user);
        return user;
    }
}    

如果你是使用的式mybatis-plus,那么你就应该i这么配置
引入maven

<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
 </dependency>

编写实体类,绑定数据库表

@Data
@TableName("tb_user")
public class User {
    private Long id;
    private String name;
    private Integer age;
}

编写mapper接口,mapper继承BaseMapper,就可以使用mybatis-plus的基本增删改查方法了

@Mapper
public interface NewUserMapper  extends BaseMapper<User> {
}

@Service
public class UserServiceIpml implements UserService {
    @Autowired
    private NewUserMapper newUserMapper;
    @Override
    public User getNewUserById(Long id) {
        return  newUserMapper.selectById(id);
    }
}
@Slf4j
@RestController
@RequestMapping("/hello")
public class HelloController {
	@Autowired
    private UserService userService;
	@GetMapping("/user/{id}")
    public User getUser(@PathVariable("id") Long id) {
        log.info("id" + id);
        return userService.getNewUserById(id);
    }
} 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

量化接口stockapi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值