文件目录
1. 依赖导入
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.6</version> <relativePath/> <!-- lookup parent from repository --> </parent> <artifactId>springboot_day3</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
2. 创建数据表
CREATE TABLE USER( id INT PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR (30) DEFAULT '该用户什么都没写', phone VARCHAR (20) UNIQUE, pwd INT NOT NULL ) ;
3. 根据表结构创建实体类
@Data public class User { private Long id; private String name; private String phone; private String pwd; }
4. 创建 UserDAO 接口
package com.liu.dao; import com.liu.entity.User; import java.util.List; public interface UserDAO { List<User> findAll(); User findById(Long id); void save(User user); void update(User user); void deleteById(Long id); }
5. 在 resources/mapping 下新建 UserDAO接口对应的 UserDAO.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.liu.dao.UserDAO"> <select resultType="com.liu.entity.User" id="findAll"> select * from user; </select> <select id="findById" parameterType="java.lang.Long" resultType="com.liu.entity.User"> select * from user where id = #{id}; </select> <insert id="save" parameterType="com.liu.entity.User"> insert into user (name, phone, pwd) values (#{name},#{phone},#{pwd}); </insert> <update id="update" parameterType="java.lang.Long"> update user set name = #{name},phone=#{phone},pwd=#{pwd} where id = #{id}; </update> <delete id="deleteById" parameterType="java.lang.Long"> delete from user where id = #{id}; </delete> </mapper>
6. 控制层
@RestController @RequestMapping("user") public class UserController { @Autowired private UserDAO userDAO; @GetMapping("/findAll") public List<User> findAll() { return userDAO.findAll(); } @GetMapping("/findById/{id}") public User findById(@PathVariable("id") long id) { return userDAO.findById(id); } /** * @param user * @RequestBody 这个注解把 json 格式的请求封装成 java 对象 * @ResponseBody 这个注解把 java 对象转换成 json 格式返回给 client */ @PostMapping("/save") public void save(@RequestBody User user) { userDAO.save(user); } @PostMapping("/update") public void update(@RequestBody User user) { userDAO.update(user); } @DeleteMapping("/deleteById/{id}") public void deleteById(@PathVariable("id") long id) { userDAO.deleteById(id); }
7. application.yml
spring: datasource: url: jdbc:mysql://localhost:3306/db08?useUnicode=true&characterEncoding=UTF-8 username: root password: 8485 driver-class-name: com.mysql.cj.jdbc.Driver mybatis: mapper-locations: classpath:/mapping/*.xml type-aliases-package: com.liu.entity 1
8. 创建启动类
注意位置,要覆盖项目所有包
package com.liu; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.liu.dao") public class SpringbootDay3Application { public static void main(String[] args) { SpringApplication.run(SpringbootDay3Application.class, args); }