application.properties


更新如下,添加mybatis配置与数据源配置。

  1. #thymeleaf
  2. spring.thymeleaf.prefix=classpath:/templates/
  3. spring.thymeleaf.suffix=.html
  4. spring.thymeleaf.cache=false
  5. spring.thymeleaf.content-type=text/html
  6. spring.thymeleaf.enabled=true
  7. spring.thymeleaf.encoding=UTF-8
  8. spring.thymeleaf.mode=HTML5
  9. # mybatis
  10. mybatis.type-aliases-package=com.fengqiuhua.pro.entity
  11. mybatis.configuration.map-underscore-to-camel-case=true
  12. mybatis.configuration.default-fetch-size=100
  13. mybatis.configuration.default-statement-timeout=3000
  14. mybatis.mapperLocations = classpath:com/fengqiuhua/pro/mapper/xml/*.xml
  15. # druid
  16. spring.datasource.url=jdbc:mysql://mysql-test.agilenaas.net:3307/company?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
  17. spring.datasource.username=root
  18. spring.datasource.password=ags@2020
  19. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  20. spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
  21. spring.datasource.filters=stat
  22. spring.datasource.maxActive=1000
  23. spring.datasource.initialSize=100
  24. spring.datasource.maxWait=60000
  25. spring.datasource.minIdle=500
  26. spring.datasource.timeBetweenEvictionRunsMillis=60000
  27. spring.datasource.minEvictableIdleTimeMillis=300000
  28. spring.datasource.validationQuery=select 'x'
  29. spring.datasource.testWhileIdle=true
  30. spring.datasource.testOnBorrow=false
  31. spring.datasource.testOnReturn=false
  32. spring.datasource.poolPreparedStatements=true
  33. spring.datasource.maxOpenPreparedStatements=20

mysql 新建 user表


  1. CREATE TABLE `user` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `name` varchar(255) DEFAULT NULL,
  4. PRIMARY KEY (`id`)
  5. )
  6. INSERT INTO `company`.`user` (`id`, `name`) VALUES ('1', '张三');

entity包下建实体类user


  1. package com.fengqiuhua.pro.entity;
  2. public class User {
  3. private int id;
  4. private String name;
  5. public int getId() {
  6. return id;
  7. }
  8. public void setId(int id) {
  9. this.id = id;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. }

新建UserDao


  1. @Mapper
  2. public interface UserDao {
  3. @Select("select * from user where id = #{id}")
  4. public User getById(@Param("id")int id );
  5. }

新建UserService


  1. @Service
  2. public class UserService {
  3. @Autowired
  4. UserDao userDao;
  5. public User getById(int id) {
  6. return userDao.getById(id);
  7. }
  8. }

测试是否可访问数据库


修改如下:查出id为1的数据。

  1. @Controller
  2. public class TestController {
  3. @Autowired
  4. UserService userService;
  5. @GetMapping("/hello")
  6. @ResponseBody
  7. public Result<User> thymeleafTest(){
  8. User byId = userService.getById(1);
  9. return Result.success(byId);
  10. }
  11. }