pom文件保持不变:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>spring_boot</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies> </project>
mapper文件:
<?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="org.example.mapper.UserMapper"> <select id="findAll" resultType="org.example.entity.MyUser"> select * from users </select> <insert id="saveUser" parameterType="org.example.entity.MyUser"> insert into users(id,name,age) values(#{id},#{name},#{age}) </insert> </mapper>
mapper接口:
package org.example.mapper; import org.example.entity.MyUser; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface UserMapper { public List<MyUser> findAll(); void saveUser(MyUser user); }
dao接口:
package org.example.service; import org.example.entity.MyUser; import java.util.List; public interface UserService { List<MyUser> findAll(); void saveUser(MyUser user); }
实现dao接口:
package org.example.service.Impl; import org.example.entity.MyUser; import org.example.mapper.UserMapper; import org.example.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImpl implements UserService { @Autowired UserMapper userMapper; @Override public List<MyUser> findAll() { return userMapper.findAll(); } @Override public void saveUser(MyUser user) { userMapper.saveUser(user); } }
控制器:
package org.example.controller; import java.util.List; import org.example.entity.MyUser; import org.example.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyUserController { @Autowired private UserService userService; @GetMapping(value = "/1") public List<MyUser> home1() { // 查询所有网站 List<MyUser> users = userService.findAll(); for (MyUser user : users) { System.out.println(user.getId()+" "+user.getName()+" "+user.getAge()); } return users; } @GetMapping(value = "/2") public void home2() { MyUser user2 = new MyUser(); user2.setName("ttt"); user2.setAge(25198); userService.saveUser(user2); } }
启动运行:
再次运行,换个数据:
标签:spring,void,boot,org,import,example,MyUser From: https://www.cnblogs.com/xiaobaibailongma/p/17023471.html