springmyboot+batis的代码(代码要有注释)写一个,带有依赖,yml配置,mapper.xml,entity,mapper,service,controller,application
时间: 2024-05-02 20:20:30 浏览: 125
代码如下:
1. 依赖配置(pom.xml)
```xml
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!-- MySQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
```
2. 配置文件(application.yml)
```yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/db_example
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:/mapper/*.xml
```
3. 实体类(User.java)
```java
public class User {
private Long id;
private String name;
private Integer age;
// getters and setters
}
```
4. Mapper 接口(UserMapper.java)
```java
@Mapper
public interface UserMapper {
@Select("SELECT * FROM users")
List<User> findAll();
@Select("SELECT * FROM users WHERE id = #{id}")
User findById(Long id);
@Insert("INSERT INTO users(name, age) VALUES(#{name}, #{age})")
@Options(useGeneratedKeys = true, keyProperty = "id")
void save(User user);
@Update("UPDATE users SET name=#{name}, age=#{age} WHERE id=#{id}")
void update(User user);
@Delete("DELETE FROM users WHERE id=#{id}")
void delete(Long id);
}
```
5. Service 层(UserService.java)
```java
@Service
public class UserService {
private final UserMapper userMapper;
@Autowired
public UserService(UserMapper userMapper) {
this.userMapper = userMapper;
}
public List<User> findAll() {
return userMapper.findAll();
}
public User findById(Long id) {
return userMapper.findById(id);
}
public void save(User user) {
userMapper.save(user);
}
public void update(User user) {
userMapper.update(user);
}
public void delete(Long id) {
userMapper.delete(id);
}
}
```
6. Controller 层(UserController.java)
```java
@RestController
@RequestMapping("/users")
public class UserController {
private final UserService userService;
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping("")
public List<User> findAll() {
return userService.findAll();
}
@GetMapping("/{id}")
public User findById(@PathVariable Long id) {
return userService.findById(id);
}
@PostMapping("")
public void save(@RequestBody User user) {
userService.save(user);
}
@PutMapping("/{id}")
public void update(@PathVariable Long id, @RequestBody User user) {
user.setId(id);
userService.update(user);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable Long id) {
userService.delete(id);
}
}
```
7. 启动类(Application.java)
```java
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
8. Mapper 文件(UserMapper.xml)
```xml
<mapper namespace="com.example.demo.mapper.UserMapper">
<resultMap id="userMap" type="com.example.demo.entity.User">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="age" column="age" />
</resultMap>
<select id="findAll" resultMap="userMap">
SELECT * FROM users
</select>
<select id="findById" parameterType="java.lang.Long" resultMap="userMap">
SELECT * FROM users WHERE id = #{id}
</select>
<insert id="save" parameterType="com.example.demo.entity.User">
INSERT INTO users(name, age) VALUES(#{name}, #{age})
</insert>
<update id="update" parameterType="com.example.demo.entity.User">
UPDATE users SET name=#{name}, age=#{age} WHERE id=#{id}
</update>
<delete id="delete" parameterType="java.lang.Long">
DELETE FROM users WHERE id=#{id}
</delete>
</mapper>
```
阅读全文