• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

cokelike

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

mybatis springboot

maven仓库

https://mvnrepository.com/artifact/org.mybatis/mybatis-spring

导入依赖

 

 <dependencies>
        <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.2.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

  

  

mybatis .xml文档

https://mybatis.org/mybatis-3/zh/getting-started.html

<?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="org.mybatis.example.BlogMapper"><!--UserInterFace包名位置-->
    <!--<select id="selectBlog" resultType="Blog"> resultType=“User类”-->
    <!--select * from Blog where id = #{id} Blog mysql table 名
    </select>
        </cache> 开启缓存-->
    <!--<select id="queryUserlist" resultType="User" useCache="true">
    select * from ss
    </select> -->
    <select id="requiteUser" resultType="User">
   select * from ss
</select>
    <select id="requiteUserById" resultType="User">
select * from ss where id= #{id}
</select>
    <insert id="addUser" parameterType="User">
    insert  into ss (id,name,sex) values (#{id},#{name},#{sex})
</insert>
    <update id="updateUser" parameterType="User">
    update ss set name=#{name},sex=#{sex} where id=#{id}
</update>
    <delete id="deleteUser" parameterType="int">
    delete from ss where id=#{id}
</delete>
</mapper> 

 application.properties:

mybatis.type-aliases-package=com.text.pojo  //User类的位置
mybatis.mapper-locations=classpath:mybatis/*.xml //mybatis 配置xml 位置

 User

 

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User { //mysql table 属性
    private int id;
    private String name;
    private String sex;
}

  

 

User-interface

 

@Mapper
@Repository
public interface UserInterFace {
    List<User> requiteUser(); //查找User下的所有用户
    User requiteUserById(int id);//用id查找用户
    int addUser(User user); //添加用户信息
    int updateUser(User user);//修改用户信息
    int deleteUser(int id);//删除用户信息
}

  

写一个JdbcControl类

 

@RestController
@RequestMapping("/text")
public class JdbcControl {
    @Autowired
    private UserInterface userInterface;
    @GetMapping("/get")
    public List<User> requiteUser(){
        List<User> users = userInterface.requiteUser();
        return users;
    }
    @GetMapping("/get/{id}")
    public User requiteUserById(@PathVariable int id){
       return userInterface.requiteUserById(id);
    }
    @PostMapping("/pos/{str}/")
    public int addUser(@PathVariable String str,@RequestBody User user){
        if(str.equals("update")){
          return userInterface.updateUser(user);
        }else if(str.equals("add")){
            return userInterface.addUser(user);
        }else {
            return 0;
        }
    }
}

  

 总结 lombok 坑:

ideal 没有安装插件导致@Data无法注入;

posted on 2022-06-02 12:46  cokelike  阅读(27)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3