接下来我将详细讲解关于“详解Spring Boot集成MyBatis XML方式”的攻略。
1. 添加相关依赖
在pom.xml中添加以下依赖:
<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.0.0</version>
</dependency>
2. 添加相关配置
在application.properties中添加以下配置信息:
#MyBatis
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.demo.entity
# DataSource
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
3. 编写Mapper接口
可以在 src/main/java/com/example/demo/mapper
下添加 UserMapper.java
接口
@Mapper
public interface UserMapper {
User getUserById(int id);
void addUser(User user);
void updateUser(User user);
void deleteUser(int id);
List<User> getAllUser();
}
4. 编写Mapper.xml文件
可以在 src/main/resources/mapper/UserMapper.xml
文件下添加以下示例代码
<mapper namespace="com.example.demo.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.example.demo.entity.User">
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="username" property="username" jdbcType="VARCHAR"/>
<result column="password" property="password" jdbcType="VARCHAR"/>
</resultMap>
<select id="getUserById" resultMap="BaseResultMap">
select id, username, password from user where id=#{id}
</select>
<select id="getAllUser" resultMap="BaseResultMap">
select id, username, password from user
</select>
<insert id="addUser">
insert into user(username, password) values(#{username}, #{password})
</insert>
<update id="updateUser">
update user set username=#{username}, password=#{password} where id=#{id}
</update>
<delete id="deleteUser">
delete from user where id=#{id}
</delete>
</mapper>
5. 编写Service层和Controller层
现在可以在 src/main/java/com/example/demo/service
下添加 UserService.java
接口文件和 UserServiceImpl.java
实现类文件,代码如下:
public interface UserService {
List<User> getAllUser();
User getUserById(int id);
void addUser(User user);
void updateUser(User user);
void deleteUser(int id);
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> getAllUser() {
return userMapper.getAllUser();
}
@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);
}
}
在 src/main/java/com/example/demo/controller
下添加 UserController.java
文件,代码如下:
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/all")
public List<User> getAllUser() {
return userService.getAllUser();
}
@GetMapping("/{id}")
public User getUserById(@PathVariable int id) {
return userService.getUserById(id);
}
@PostMapping("/")
public void addUser(@RequestBody User user) {
userService.addUser(user);
}
@PutMapping("/")
public void updateUser(@RequestBody User user) {
userService.updateUser(user);
}
@DeleteMapping("/{id}")
public void deleteUser(@PathVariable int id) {
userService.deleteUser(id);
}
}
6. 启动Spring Boot
最后,启动Spring Boot应用程序,程序会自动扫描 UserMapper.xml
文件并将所有的Mapper映射至Mybatis框架中,从而实现对数据库的操作。
以上就是关于“详解Spring Boot集成MyBatis XML方式”的攻略了。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:详解springboot集成mybatis xml方式 - Python技术站