MyBatis——MyBatis的注解开发(了解)

MyBatis的注解开发-了解

创建工程:

1.注解开发的缺点

MyBatis可以在接口中直接添加MyBatis注解,完成CRUD。

但注解模式属于硬编码到.java文件中,失去了使用配置文件外部修改的优势,可结合需求选用。

2.mapper

java 复制代码
public interface UserDao {
    /**
     * 查询所有用户
     * @return
     */
    @Select("select * from user")
    public List<User> findAll();

    /**
     * 保存操作
     * @param user
     * @return
     */
    @Insert("insert into user(username,sex,birthday,address)values(#{username},"+
            "#{sex},#{birthday},#{address})")
    @SelectKey(keyColumn="id",keyProperty="id",resultType=Integer.class,before =
            false,statement = { "select last_insert_id()" })
    int saveUser(User user);
    /**
     * 更新操作
     * @param user
     * @return
     */
    @Update("update user set username=#{username},address=#{address}," +
            "sex=#{sex},birthday=#{birthday} where id =#{id} ")
    void updateUser(User user);
    /**
     * 删除用户
     * @param id
     * @return
     */
    @Delete("delete from user where id = #{id} ")
    void deleteUser(Integer id);
    /**
     * 查询使用聚合函数
     * @return
     */
    @Select("select count(*) from user ")
    int findTotal();
    /**
     * 根据id查询用户
     * @return
     */
    @Select("select * from user where id = #{id} ")
    public User findById(Integer id);
    /**
     * 一对多
     * @return
     */
    @Results(id="resultMap",
            value= {
                    @Result(id=true,column="id",property="id"),
                    @Result(column="username",property="username"),
                    @Result(column="sex",property="sex"),
                    @Result(column="address",property="address"),
                    @Result(column="birthday",property="birthday"),
                    @Result(column="id",property="accounts",
                            many=@Many(
                                    select="com.by.dao.AccountDao.findByuId",
                                    fetchType= FetchType.LAZY
                            )
                    )
            })
    @Select("select * from user")
    public List<User> findAll2();
}
java 复制代码
public interface AccountDao {
    @Results(id="accountMap",
        value= {
                @Result(id=true,column="id",property="id"),
                @Result(column="uid",property="uid"),
                @Result(column="money",property="money"),
                @Result(column="uid",
                        property="user",
                        one=@One(select="com.by.dao.UserDao.findById",
                                fetchType= FetchType.LAZY)
                )
        })
    @Select("select * from account")
    List<Account> findAll();

    @Select("select * from account where uid = #{uid} ")
    List<Account> findByuId(Integer id);
}

3.pojo

java 复制代码
public class User implements Serializable {
    private Integer id;
    private String username;
    private String password;
    private Date birthday;
    private String sex;
    private String address;
    private List<Account> accounts;
    //一对多关系映射
    private List<Account> accounts;
	// get set toString方法省略
}	
java 复制代码
public class Account implements Serializable {
    private Integer id;
    private Integer uid;
    private Double money;
    //一对一关系映射
    private User user;
    // get set toString方法省略
}

4.测试

java 复制代码
    @Test
    public void testFindAll() throws Exception{
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        List<User> userList = userDao.findAll2();
        for(User user : userList){
            System.out.println(user);
        }
    }
相关推荐
阿里云云原生14 小时前
AI Agent 上线容易稳定难?阿里云 AgentLoop 推出“经验自进化”闭环治理方案
人工智能·阿里云·mybatis·agentscope
CodeStats1 天前
【Spring事务】Spring事务注解 @Transactional 完整体系:从 MySQL 隔离级别到 MyBatis 原理详解
java·spring·mybatis·事务·transactional
Nuanyt2 天前
SSM 学习记录 第二部分 Spring整合Mybatis&Junit AOP核心概念 Spring事务管理 SpringMVC 请求与响应 Rest风格
java·spring·junit·mybatis·restful
米码收割机2 天前
【SSM】Spring MVC_MyBatis SSM商城系统(源码+论文)【独一无二】
spring·mvc·mybatis
Devin~Y3 天前
互联网大厂 Java 面试实录:Spring Boot、MyBatis、Redis、Kafka、Spring Security、RAG 与 MCP 全链路问答
java·redis·kafka·mybatis·spring security·spring mvc·sprint boot
VX_bysjlw9853 天前
基于微信小程序的宠物用品商城系统-后端74346-计算机毕设原创(免费领源码+带部署教程)
java·redis·微信小程序·eclipse·mybatis·idea·微信开发者工具
hexu_blog4 天前
springboot3集成shardingsphere4.0 分表分库
java·spring boot·mybatis
乐观的Terry4 天前
9、发布系统-Webhook自动发布
java·spring boot·spring·spring cloud·mybatis
聆听。。花开雨落4 天前
mybatis的typeHandler 作用
数据库·mybatis
天若有情6737 天前
SpringBoot4 + MyBatis 前后端分离实战|从零实现坏习惯管理系统,支持局域网手机访问CRUD
智能手机·mybatis