5.2.3.1 引入pom依赖
导入mybatis相关依赖,如果需要的话,还需引入JDBC和MysqlConnector相关依赖
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<!-- JDBC -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- MySQL连接器 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
5.2.3.2 编写DAO实体类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
public String id;
public String name;
public String pwd;
public Integer gender;
}
5.2.3.3 编写Mapper接口
@Mapper 标记这是一个映射接口
import com.xietong.web.dao.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface UserMapper {
List<User> queryUserList();
User queryUserById(int id);
int addUser(User user);
int deleteUser(int id);
int updateUser(User user);
}
5.2.3.4 编写XML文件
在/resources/mybatis/mapper下新建mapper文件 usermapper.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.xietong.web.mapper.UserMapper">
<select id="queryUserList" resultType="User">
select * from user
</select>
<select id="queryUserById" resultType="User">
select * from user where id = #{id}
</select>
<insert id="addUser" parameterType="User">
insert into user (id,name,pwd,gender) values (#{id}, #{name}, #{pwd}, $#{gender})
</insert>
<delete id="deleteUser" parameterType="int">
delete * from user where id = #{id}
</delete>
<update id="updateUser" parameterType="User">
update user set name = #{name}, pwd = #{pwd}, gender = #{gender} where id = #{ide}
</update>
</mapper>
5.2.3.5 整合MyBatis
在配置文件中加入mybatis相关配置,使spring能够正确识别mapper.xml文件并加载对应的类
mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml
type-aliases-package: com.xietong.web.dao
5.2.3.6 编写Controller类
import com.xietong.web.dao.User;
import com.xietong.web.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/mybatis")
public class MyBatisController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/queryAll")
public List<User> queryAll(){
List<User> userList = userMapper.queryUserList();
return userList;
}
@RequestMapping("/getById")
public User getById(@RequestParam int id){
return userMapper.getUserById(id);
}
@RequestMapping("/addUser")
public String addUser(){
int count = userMapper.addUser(new User(7, "白骨精", "1234", 1));
return count > 1 ? "ok" : "添加异常";
}
@RequestMapping("/updateUser")
public String updateUser(){
int count = userMapper.updateUser(new User(7, "白骨精", "12347890", 1));
return count > 1 ? "ok" : "修改异常";
}
@RequestMapping("/deleteUser")
public String deleteUser(){
int count = userMapper.deleteUser(7);
return count > 1 ? "ok" : "删除异常";
}
}