标签:druid struct ast 自己 而不是 cti tis pat sel
<!-- 引入 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>
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class User { private int id; private String name; private String pwd; }
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); }
<?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>
#指定myBatis的核心配置文件与Mapper映射文件
mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml
# 注意:对应实体类的路径
type-aliases-package: ustc.wzh.pojo
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"; } }
标签:druid struct ast 自己 而不是 cti tis pat sel
原文地址:https://www.cnblogs.com/zhihaospace/p/12406051.html