介绍
MyBatis 的真正强大在于它的语句映射,这是它的魔力所在。由于它的异常强大,映射器的 XML 文件就显得相对简单。如果拿它跟具有相同功能的 JDBC 代码进行对比,你会立即发现省掉了将近 95% 的代码。MyBatis 致力于减少使用成本,让用户能更专注于 SQL 代码。
官网中文文档:https://www.mybatis.net.cn/sqlmap-xml.html
Mapper映射文件使用
application.properties 添加配置
# mapper.xml 配置文件路径
mybatis-plus.mapper-locations=classpath:mapper/*.xml
# 配置XML映射文件中指定的实体类别名路径
mybatis-plus.type-aliases-package=cn.qvtu.web.domain
在项目mapper目录中创建Mapper文件
mapper.xml文件放在resources/mapper目录中
UserMapper.xml 文件
简单增删改查SQL配置,namespace对应 UserMapper.java 的包名
<?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="cn.qvtu.web.mapper.UserMapper">
<select id="selectUserById" parameterType="long" resultType="User">
SELECT * FROM user WHERE id = #{id}
</select>
<insert id="insertUser" parameterType="User" >
insert into user (id,name,age,email)
values (#{id},#{name},#{age},#{email})
</insert>
<update id="updateUser" parameterType="User" >
update user set
name = #{name},
age = #{age},
email = #{email}
where id = #{id}
</update>
<delete id="deleteUser" parameterType="long">
delete from user where id = #{id}
</delete>
<select id="selectUserList" parameterType="User" resultType="User">
SELECT * FROM user WHERE 1=1
<if test="name != null and name != ''">
AND name = #{name}
</if>
<if test="email != null and email != ''">
AND email = #{email}
</if>
<if test="age != null">
AND age = #{age}
</if>
</select>
</mapper>
UserMapper.java 文件
UserMapper.xml文件中 每个id名称对应UserMapper.java的方法名称
@Mapper
public interface UserMapper extends BaseMapper<User> {
public User selectUserById(Long id);
public void updateUser(User user);
public void insertUser(User user);
public void deleteUser(Long id);
public List<User> selectUserList(User user);
}
IUserService 文件
定义具体业务逻辑的接口
public interface IUserService extends IService<User> {
public User selectUserById(Long id);
public void updateUser(User user);
public void insertUser(User user);
public void deleteUser(Long id);
public List<User> selectUserList(User user);
}
UserServiceImpl文件
实现具体业务逻辑的接口,调用UserMapper实现数据的CRUD
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements IUserService {
@Autowired
private UserMapper userMapper;
@Override
public User selectUserById(Long id) {
return userMapper.selectUserById(id);
}
@Override
public void updateUser(User user) {
userMapper.updateUser(user);
}
@Override
public void insertUser(User user) {
userMapper.insertUser(user);
}
@Override
public void deleteUser(Long id) {
userMapper.deleteUser(id);
}
@Override
public List<User> selectUserList(User user) {
return userMapper.selectUserList(user);
}
}
UserController 文件
前端访问控制器
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@GetMapping("/select")
public User select(Long id){
return userService.selectUserById(id);
}
@GetMapping("/insert")
public String insert(User user){
userService.insertUser(user);
return "insert SUCCESS";
}
@GetMapping("/update")
public String update(User user){
userService.updateUser(user);
return "update SUCCESS";
}
@GetMapping("/delete")
public String delete(Long id){
userService.deleteUser(id);
return "delete SUCCESS";
}
@GetMapping("/selectList")
public List<User> select(User user){
return userService.selectUserList(user);
}
}