引入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);
}
}