springboot和mysql怎么交互_Springboot--与数据库交互

1.目录结构

d04b411a2d6d

图片.png

2.代码

2.1 启动类-Demo

@SpringBootApplication

@MapperScan(value = "com.may.project.dao")

@ComponentScan(basePackages = {"com.may.project.action","com.may.project.service.impl"})

public class Demo {

public static void main(String[] args) {

SpringApplication.run(Demo.class, args);

}

}

2.2 Action-SendMessageController

@Slf4j

@RequestMapping("/send")

@RestController

public class SendMessageController {

@Autowired

private IUserTestService userTestService;

@RequestMapping(value="/getbymemberid", method= RequestMethod.POST)

public UserTest getByMember(HttpServletRequest request, @RequestBody Map param) throws Exception {

log.info("/send/getbymemberid"+ param);

String id = (String) param.get("id");

log.info("id ="+id);

UserTest userTest = userTestService.findNameByid(id);

log.info("userTest ="+userTest);

return userTest;

}

}

2.3 DAO-IUserTestDAO

@Mapper

public interface IUserTestDAO {

UserTest findNameByid(@Param("id") String id);

}

2.4 实体-UserTest

@Data

public class UserTest {

private String id;

private String userName;

private String phone;

private String birthday;

private Date createTime;

}

2.5 service

@Component

public class UserTestServiceImpl implements IUserTestService{

@Autowired

private IUserTestDAO userTestDAO;

@Override

public UserTest findNameByid(String id) {

return userTestDAO.findNameByid(id);

}

}

public interface IUserTestService{

UserTest findNameByid(String id);

}

2.6 mapping-UserTestDAO.xml

select *

from t_user

where id =#{id}

2.6 配置文件-application.properties

#应用名称

spring.application.name=test-project

#访问端口号

server.port=8749

#编码格式

server.tomcat.uri-encoding=utf-8

#数据库相关配置

spring.datasource.url=jdbc:mysql://localhost:3306/sql_test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true

spring.datasource.username=root

spring.datasource.password=***

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.max-idle=10

spring.datasource.max-wait=10000

spring.datasource.min-idle=5

spring.datasource.initial-size=5

#session生命周期

server.servlet.session.timeout=30m

mybatis.mapper-locations=classpath*:mapping/**/*.xml

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值