window.cnblogsConfig = { blogUser: 'MoYu', blogAvatar: 'https://gitee.com/MoYu-zc/picgo/raw/master/img/20210213094450.jpg', blogStartDate: '2020-02-09', webpageTitleOnblur: '(o゚v゚)ノ Hi,Back', webpageTitleOnblurTimeOut: 500, webpageTitleFocus: '(*´∇`*) 欢迎回来!', webpageTitleFocusTimeOut: 1000, webpageIcon: "https://gitee.com/MoYu-zc/picgo/raw/master/img/20210213094450.jpg", enable: true, // 是否开启日/夜间模式切换按钮 auto: { // 自动切换相关配置 enable: false, // 开启自动切换 dayHour: 7, // 日间模式开始时间,整数型,24小时制 nightHour: 20 // 夜间模式开始时间,整数型,24小时制 } switchDayNight: { enable: true, auto: { enable: true } }, progressBar: { id : 'top-progress-bar', // 请勿修改该值 color : '#77b6ff', height : '2px', duration: 0.2, }, loading: { rebound: { tension: 16, friction: 5, }, spinner: { id: 'spinner', radius: 90, sides: 3, depth: 4, colors: { background: '#f0f0f0', stroke: '#272633', base: null, child: '#272633', }, alwaysForward: true, // When false the spring will reverse normally. restAt: 0.5, // A number from 0.1 to 0.9 || null for full rotation renderBase: false, } }, homeTopAnimationRendered: true, homeTopAnimation: { radius: 15, density: 0.2, color: 'rgba(255,255,255, .2)', // 颜色设置,“random” 为随机颜色 clearOffset: 0.3, }, essayTopAnimationRendered: true, essayTopAnimation: { triW : 14, triH : 20, neighbours : ["side", "top", "bottom"], speedTrailAppear : .1, speedTrailDisappear : .1, speedTriOpen : 1, trailMaxLength : 30, trailIntervalCreation : 100, delayBeforeDisappear : 2, colorsRandom: false, // v1.2.4 是否开启随机颜色 colors: [ '#96EDA6', '#5BC6A9', '#38668C', '#374D84', '#BED5CB', '#62ADC6', '#8EE5DE', '#304E7B' ] }, homeTopImg: [ "https://cdn.jsdelivr.net/gh/BNDong/Cnblogs-Theme-SimpleMemory@master/img/webp/home_top_bg.webp", "https://cdn.jsdelivr.net/gh/BNDong/Cnblogs-Theme-SimpleMemory@master/img/webp/home_top_bg.webp" ], homeBannerTextType: "one", essayTopImg: [ "https://cdn.jsdelivr.net/gh/BNDong/Cnblogs-Theme-SimpleMemory@master/img/webp/nothome_top_bg.webp", "https://cdn.jsdelivr.net/gh/BNDong/Cnblogs-Theme-SimpleMemory@master/img/webp/nothome_top_bg.webp", "https://gitee.com/MoYu-zc/picgo/raw/master/img/20210208190902.jpg", "https://gitee.com/MoYu-zc/picgo/raw/master/img/20210208190954.jpg", ], codeMaxHeight: true, codeLineNumber: true, essayCode: { fontFamily: "'Ubuntu Mono',monospace", // 代码框字体 fontSize: "14px" // 代码框字体大小 }, }

Mybatis-05 注解开发

Mybatis-05 注解开发

Mybatis系列文章已经完成上传:
一、什么是Mybatis
二、CRUD
三、配置解析
四、日志、分页
五、注解开发
六、动态Sql

注解开发

注解的核心是反射机制

面向接口编程的根本原因:解耦,可拓展,提高复用,分层开发中、上层不用管具体的实现,大家都遵守共同的标准,使得开发变得容易,规范性好。

1.接口类

注解在接口上实现

public interface UserMapper {

    @Select("select * from user")
    List<user> getUser();

    @Select("select * from user where id=#{id}")
    user getUserById(@Param("id")int id);

    @Insert("insert into user(id,name,pwd) values (#{id},#{name},#{pwd})")
    int insertUser(user user);

    @Update("update user set pwd=#{pwd} where name=#{name}")
    int updateUser(Map map);

    @Delete("delete from user where id=#{iod}")
    int deleteUser(@Param("iod")int id);

}

一定记得在配置文件中绑定:

<mappers>
     <mapper class="com.Dao.UserMapper"/>
</mappers>

2.测试

public class Test1 {

    @Test
    public void test(){
        SqlSession sqlSession = mybatis_util.getSqlSession1();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<user> userList = mapper.getUser();
        for (user user : userList) {
            System.out.println(user);
        }
        sqlSession.close();
    }

}

上述,以运行getUser方法举例,结果如下:

1

注意:

  • sql语句不能写错
  • 记得在配置文件中绑定<mapper>
  • sql语句中的#{xxx}@Param("xxx")内容相同

Mybatis执行流程分析

4

Lombok

该功能 偷懒专用!!! 但是不推荐使用。

1.安装插件

第一步,先安装Lombok插件

File---Settings---Plugins,搜索Lombok

2

2.导jar包

导入依赖:

 <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
        </dependency>

3.注解

这个注解可以在实体类、单个属性上设置。

一般下面这三个就够用了:

@Data
@NoArgsConstructor
@AllArgsConstructor
3

个人博客为:
MoYu's Github Blog
MoYu's Gitee Blog

posted @ 2021-02-20 20:43  MoYu-zc  阅读(115)  评论(0编辑  收藏  举报