错误
Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property) to use a more specific time zone value if you want to utilize time zone support.] with root cause
数据库问题
mysql8.0必须设置时区url
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot?serverTimezone=GMT%2B8&useSSL=false&useUnicode=true&characterEncoding=UTF-8&allowPublicKeyRetrieval=true
username: root
password: wj
500但是添加成功了
注解
Config
springboot项目中的mvc配置大概有两种写法,一种是基于实现接口WebMvcConfigurer,一种是基于继承WebMvcConfigurationSupport,这两种写法会冲突,最终spring容器会执行继承WebMvcConfigurationSupport类中的重写方法,而忽略实现接口WebMvcConfigurer所重写的方法。
因此,一个应用中,应只选择一种方法进行配置。
@Configuration
@Bean
DAO
注意update,delete,insert不要返回对象(也返回不了,会报错),全返回int或者boolean
@mapper:在接口上添加了这个注解表示这个接口是基于注解实现的CRUD(增删改查)
@Repository:将数据访问层(Dao层)的类标识为Spring Bean(Bean是被实例的,组装的及被Spring容器管理的Java对象,Spring容器会自动完成@bean对象的实例化),创建应用对象之间的协作关系的行为称为:装配(wiring)这就是依赖注入的本质。
Results:返回的map结果集,property表示User类的字段,column表示对应数据库的字段
Param:sql条件的字段
@Mapper
@Repository
@Insert
@Select
@Delete
@Update
@Param("id")
id必须和SQL语句的id一致
Service
@Service
Controller
model.addAttribute("a","123");
向前端传数据,类似于request.setAttribute("a","123")
@RestController
添加位置:Controller类上
用thymeleaf时还是用@Controller使用
作用:相当于@Controller+@ResponseBody两个注解的结合,告诉容器Controller,返回json数据;不需要在方法前面加@ResponseBody注解了
但使用@RestController这个注解,就不能返回jsp,html页面,视图解析器无法解析jsp,html页面
@RequestMapping("/user")
添加请求前缀
@Autowired
自动注入组件
@resource
两种情况:
name属性有值
- 按照name值去spring容器找bean,找不到报错
name属性无值
- 先判断该属性名字在spring容器是否存在bean对象,不存在执行下一步
- 根据属性类型去spring容器找bean对象,找到一个就注入
区别:
- @Autowired注解由Spring提供@resource注解由J2EE提供
- @Autowired默认按类型进行装配,存在多个类型则通过名称注入,也可以配合@Qualifier中指定bean的名字来注入bean@Resource默认按照名称进行装配,如名称无法找到则通过类型注入。
RESTFUL风格请求---------------
@GetMapping("/get")
@PostMapping("/add")
@DeleteMapping("/del")
@PutMapping("/update")
获取JSON数据-----------------
@RequestBody
可以封装json数据为java对象
单个pojo对象
{
"id" : 5,
"name" : "wuliuqi",
}
@PostMapping("/saveUser")
public User saveUser(@RequestBody User user) {
return userService.saveUser(user);
}
@PostMapping("/saveUser")
@ResponseBody
public Map<String,Object> getJsonVal(@RequestBody Map<String,Object> user) {
System.out.println("id = " + user.get("id"));
System.out.println("name = " + user.get("name"));
return user;
}
集合--多个pojo对象
[{"id": 1, "name": "小明"},{"id": 2, "name": "小红"}]
@PostMapping("/getJson")
@ResponseBody
public List<User> getJsonVal(@RequestBody List<User> users) throws IOException {
for(User user : users){
System.out.println("user = " + user);
}
return users;
}
@RequestParam
接受json对象
{
"id": 1,
"name": "小明"
};
@PostMapping("/getJson")
@ResponseBody
public User getJsonVal(@RequestParam("id") String id,@RequestParam("name") String name) {
User user = new User();
user.setId(Integer.parseInt(id));
user.setName(name);
return user;
}
@PathVariable
看图
@DeleteMapping("/delUser/{id}")
public Integer DELETE(@PathVariable Integer id){
return userService.DELETE(id);
};
Entity
@Data
get和set方法
@AllArgsConstructor
有参
@NoArgsConstructor
无参
案例
注解开发
application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC
username: root
password: 123456
User
package com.curd.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 注释
*
* @author ss
* @date 2022-09-06 15:27
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
/**
* 主键ID
*/
private Long id;
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private Integer age;
/**
* 邮箱
*/
private String email;
}
UserMapper
package com.curd.mapper;
import org.apache.ibatis.annotations.*;
import com.curd.entity.User;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* 注释 Mapper
*
* @author ss
* @date 2022-09-06 15:27
*/
@Mapper
@Repository
public interface UserMapper {
/**
* 添加信息
* @param user
* @return
*/
@Insert("insert into user(id,name,age,email) values(#{id},#{name},#{age},#{email})")
public User saveUser(User user);
/**
* 返回所有用户
* @return
*/
@Select("select * from user")
public List<User> listUser();
@Delete("delete from user where id = #{id}")
public Integer DELETE(Integer id);
@Update("update user set name = #{name},age = #{age},email = #{email} where id = #{id}")
public User updateUser(User user);
}
UserService
package com.curd.service;
import com.curd.entity.User;
import java.util.List;
/**
* 服务类接口
*
* @author ss
* @date 2022-09-06 15:27
*/
public interface UserService {
public User saveUser(User user);
public List<User> listUser();
public Integer DELETE(Integer id);
public User updateUser(User user);
}
UserServiceImpl
package com.curd.service.serviceImpl;
import com.curd.entity.User;
import com.curd.mapper.UserMapper;
import com.curd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 服务实现类
*
* @author ss
* @date 2022-09-06 15:27
*/
@Service
public class UserServiceImpl implements UserService {
//注入属性
@Autowired
UserMapper userMapper;
@Override
public User saveUser(User user) {
return userMapper.saveUser(user);
}
@Override
public List<User> listUser() {
List<User> users = userMapper.listUser();
return users;
}
@Override
public Integer DELETE(Integer id) {
Integer delete = userMapper.DELETE(id);
return delete;
}
@Override
public User updateUser(User user) {
return userMapper.updateUser(user);
}
}
UserController
package com.curd.controller;
import com.curd.entity.User;
import com.curd.service.serviceImpl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* 前端控制器
*
* @author ss
* @date 2022-09-06 15:27
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
UserServiceImpl userService;
@PostMapping("/saveUser")
public User saveUser(@RequestBody User user) {
return userService.saveUser(user);
}
@GetMapping("/list")
public List<User> listUser() {
return userService.listUser();
}
@DeleteMapping("/delUser/{id}")
public Integer DELETE(@PathVariable Integer id){
return userService.DELETE(id);
}
@PutMapping("/updateUser")
public User updateUser(@RequestBody User user){
return userService.updateUser(user);
}
}