配置文件:
springboot 默认的配置文件是application.properties:
server.port=8083
spring.datasource.url=jdbc:mysql://localhost:3306/first_sql?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=888888
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.mapper-locations=classpath:mybatis/*.xml
也可以修改配置文件类型,改为:application.yml:
server:
port: 8083
spring:
datasource:
url: jdbc:mysql://localhost:3306/first_sql?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false
username: root
password: 888888
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
type-aliases-package: com.example.demo.entity
mapper-locations: classpath:mybatis/*.xml
做好相关配置后,开始上代码~
代码:
1. User.java:
package com.example.demo.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String name;
private int age;
private int salary;
}
2. UserMapper.java:
package com.example.demo.mapper;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface UserMapper {
public List<User> getUserList();
public User getUserById(int userId);
public void addUser(User user);
public void updateUser(User user);
public void deleteUser(int id);
}
3. userMapper.xml:
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace 绑定一个对应的Mapper接口-->
<mapper namespace="com.example.demo.mapper.UserMapper">
<select id="getUserList" resultType="User">
select * from first_sql.user
</select>
<select id="getUserById" resultType="User" parameterType="int">
select * from first_sql.user where id = #{userId}
</select>
<insert id="addUser" parameterType="User">
insert into first_sql.user (name, age, salary) values (#{name}, #{age}, #{salary})
</insert>
<update id="updateUser" parameterType="User">
update first_sql.user set name=#{name}, age=#{age}, salary=#{salary} where id=#{id}
</update>
<delete id="deleteUser" parameterType="int">
delete from first_sql.user where id = #{id}
</delete>
</mapper>
4. UserService.java:
package com.example.demo.service;
import com.example.demo.entity.User;
import java.util.List;
public interface UserService {
public List<User> getUserList();
public User getUserById(int id);
public void addUser(User user);
public void updateUser(User user);
public void deleteUser(int id);
}
5. UserServiceImpl.java:
package com.example.demo.service;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
@Override
public List<User> getUserList() {
return userMapper.getUserList();
}
@Override
public User getUserById(int id) {
return userMapper.getUserById(id);
}
@Override
public void addUser(User user) {
userMapper.addUser(user);
}
@Override
public void updateUser(User user) {
userMapper.updateUser(user);
}
@Override
public void deleteUser(int id) {
userMapper.deleteUser(id);
}
}
6. UserController.java:
package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/all")
public List<User> getAllUsers () {
return userService.getUserList();
}
@GetMapping("/getUser/{idParam}")
public User getUser (@PathVariable("idParam") int id) {
return userService.getUserById(id);
}
@PostMapping("/add")
public String addUser (@RequestBody User user) {
userService.addUser(user);
return "add success";
}
@PostMapping("/update")
public String updateUser (@RequestBody User user) {
userService.updateUser(user);
return "update success";
}
@GetMapping("/delete/{id}")
public String deleteUser(@PathVariable("id") int id) {
userService.deleteUser(id);
return "delete success";
}
}
【踩坑】:
报错:Failed to configure a DataSource: 'url' attribute is not specified and no embe...
在pom.xml中添加依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>