mybatis
一.整合Mybatis框架
1.步骤:
-
在整合Druid的基础上继续(略)
-
pom.xml中添加mybatis依赖,以及fastjson
-
编写实体类
-
编写mapper层(dao层)
-
编写mapper映射文件
-
在application.yml文件中添加配置
-
编写控制器
2.pom.xml中添加mybatis依赖
<!-- 引入 myBatis,这是 MyBatis官方提供的适配 Spring Boot 的,而不是Spring Boot自己的--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.0</version> </dependency>
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.60</version> </dependency>
3.编写实体类
-
新建pojo目录,建立User.java类
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class User { private int id; private String name; private String pwd; }
4.编写mapper层(dao层)
-
新建mapper目录,添加UserMapper.java接口
import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import ustc.wzh.pojo.User; import java.util.List; //@Mapper : 表示本类是一个 MyBatis 的 Mapper,等价于以前 Spring 整合 MyBatis 时的 Mapper 接口 @Mapper @Repository public interface UserMapper { //选择全部用户 List<User> selectUser(); //根据id选择用户 User selectUserById(int id); //添加一个用户 int addUser(User user); //修改一个用户 int updateUser(User user); //根据id删除用户 int deleteUser(int id); }
5.编写mapper映射文件
-
在resources目录下建立mybatis目录,在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="ustc.wzh.mapper.UserMapper"> <select id="selectUser" resultType="User"> select * from user </select> <select id="selectUserById" resultType="User"> select * from user where id = #{id} </select> <insert id="addUser" parameterType="User"> insert into user (id,name,pwd) values (#{id},#{name},#{pwd}) </insert> <update id="updateUser" parameterType="User"> update user set name=#{name},pwd=#{pwd} where id = #{id} </update> <delete id="deleteUser" parameterType="int"> delete from user where id = #{id} </delete> </mapper>
6.在application.yml文件中添加配置
-
在application.yml文件末尾追加mybatis配置的映射以及实体类的路径
-
myBatis 与 spring 整合后,配置数据源、事务、连接数据库的账号、密码等就交由 spring 管理。
#指定myBatis的核心配置文件与Mapper映射文件 mybatis: mapper-locations: classpath:mybatis/mapper/*.xml # 注意:对应实体类的路径 type-aliases-package: ustc.wzh.pojo
7.编写控制器
import com.alibaba.fastjson.JSON; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import ustc.wzh.mapper.UserMapper; import ustc.wzh.pojo.User; import java.util.List; @RestController @RequestMapping("/mybatis") public class UserController { @Autowired private UserMapper userMapper; //选择全部用户 @GetMapping("/selectUser") public String selectUser(){ List<User> users = userMapper.selectUser(); for (User user : users) { System.out.println(user); } return "mybatis-selectUser:"+JSON.toJSONString(users); } //根据id选择用户 @GetMapping("/selectUserById") public String selectUserById(){ User user = userMapper.selectUserById(1); System.out.println(user); return "mybatis-selectUserById:"+JSON.toJSONString(user); } //添加一个用户 @GetMapping("/addUser") public String addUser(){ User user = new User(5,"阿毛","456789"); userMapper.addUser(user); return "mybatis-addUser:"+JSON.toJSONString(user); } //修改一个用户 @GetMapping("/updateUser") public String updateUser(){ User user = new User(5,"阿毛","421319"); userMapper.updateUser(user); return "mybatis-updateUser:"+JSON.toJSONString(user); } //根据id删除用户 @GetMapping("/deleteUser") public String deleteUser(){ userMapper.deleteUser(5); return "mybatis-deleteUser:ok"; } }