(亲测解决)Unsatisfied dependency expressed through bean property ‘sqlSessionFactory’
java.sql.SQLException: The server time zone value 的解决办法
spring.datasource.url=jdbc:mysql://localhost:3306/user_manager?serverTimezone=UTC
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
一、设置关联的表名
1、默认情况下,如果数据库表是使用标准的下划线命名,并且能对应上实体类的类名,我们就不需要特别去手动匹配。比如有张 user_info 表,那么会自动匹配下面这个实体类:
@Data
public class UserInfo {
private Integer id;
private String userName;
private String passWord;
}
使用TK框架中selectByPrimaryKey(Object key),需要注意要在entity里注明哪个字段是主键,否则会不知道哪个是PrimaryKey会随机一个字段就报错。
通用mapper的查询,selectByPrimaryKey、select、selectByExample等
package com.leyou.controller;
import com.leyou.pojo.TbUser;
import com.leyou.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class HelloController {
private final static Logger logger = LoggerFactory.getLogger(HelloController.class);
@Autowired
private UserService userService;
@GetMapping("hello")
public String helloo(){
logger.trace("我是trace");
logger.debug("我是debug");
logger.info("我是info");
logger.warn("我是warn");
logger.error("我是error");
return "hello,springboot!";
}
@GetMapping("/hello1")
public TbUser hello() {
TbUser user = this.userService.queryById(1L);
return user;
}
@GetMapping("/hello2")
public List<TbUser> hello2() {
TbUser tbUser = new TbUser();
tbUser.setId(4L);
List<TbUser> listUser = this.userService.queryByObj(tbUser);
return listUser;
}
}
package com.leyou.pojo;
import javax.persistence.Id;
import java.util.Date;
public class TbUser {
@Id
private Long id;
// 用户名
private String userName;
// 密码
private String password;
// 姓名
private String name;
// 年龄
private Integer age;
// 性别,1男性,2女性
private Integer sex;
// 出生日期
private Date birthday;
// 创建时间
private Date created;
// 更新时间
private Date updated;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public Date getUpdated() {
return updated;
}
public void setUpdated(Date updated) {
this.updated = updated;
}
@Override
public String toString() {
return "User [id=" + id + ", userName=" + userName + ", password=" + password + ", name=" + name
+ ", age=" + age + ", sex=" + sex + ", birthday=" + birthday + ", created=" + created
+ ", updated=" + updated + "]";
}
}