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);
        }
    }
相关推荐
霸道流氓气质36 分钟前
MyBatis 与 SQL 层面关键技术详解
windows·sql·mybatis
₍˄·͈༝·͈˄*₎◞ ̑̑码12 小时前
MyBatis操作数据库
数据库·mybatis
干到60岁退休的码农21 小时前
13.构建登录接口响应数据
java·spring boot·mybatis
SQL-First布道者2 天前
我为什么把 MyBatis 从项目中删了?
java·spring·tomcat·mybatis·mybatis plus·spring jdbc
爱折腾的编程老炮2 天前
潮玩品牌自建抽盒机小程序——商城 + 抽盒 + 一番赏 + 会员 + 社区,一套代码怎么搭
spring boot·小程序·vue·mybatis·uniapp
kakawzw2 天前
mybatis源码笔记1——JDBC和整体架构
java·mybatis
Java小白笔记3 天前
MySQL 8.0 常用内置函数用例
数据库·mysql·mybatis
勿忘,瞬间3 天前
Mybatis高阶
java·数据库·mybatis
大梦想家a3 天前
新能源汽车共享充电桩收费管理系统的设计与实现(SpringBoot+MyBatis-Plus+Vue,前后台分离完整源码)
spring boot·汽车·mybatis
计算机学姐3 天前
基于SpringBoot的高校爱心慈善管理系统
java·vue.js·spring boot·后端·spring·tomcat·mybatis