1.导入坐标

<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>3.7.5</version></dependency><dependency>
<!--      解析器--><groupId>com.github.jsqlparser</groupId><artifactId>jsqlparser</artifactId><version>0.9.1</version></dependency>

2.创建实体类User

public class User {private int id;private String username;private String password;private Date birthday;public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}@Overridepublic String toString() {return "User{" +"id=" + id +", username='" + username + '\'' +", password='" + password + '\'' +", birthday=" + birthday +'}';}
}

3.创建UserMapper(dao)接口

public interface UserMapper {@Insert("insert into user values(#{id},#{username},#{password},#{birthday})")public void save(User user);@Update("update user set  username=#{username},password=#{password},birthday=#{birthday} where id=#{id}")public void update(User user);@Delete("delete from user where id=#{id}")public void delete(int id);@Select("select * from user where id=#{id}")public User findById(int id);@Select("select * from user")public List<User> findAll();}

4.配置Mybatis核心配置文件SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--    加载jdbc.properties配置文件--><properties resource="jdbc.properties"/><!--    定义别名--><typeAliases><typeAlias type="com.hao.domain.User" alias="user"/></typeAliases><!--    配置数据源环境--><environments default="development"><!-- 表示默认情况下使用id为development的环境--><environment id="development"><transactionManager type="JDBC"></transactionManager><dataSource type="POOLED"><property name="driver" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></dataSource></environment></environments><!--    加载映射关系--><mappers>
<!--        指定接口所在的包--><package name="com.hao.mapper"/></mappers>
</configuration>

5.测试

public class MapperTest {private UserMapper userMapper;@Beforepublic void before() throws IOException {InputStream stream = Resources.getResourceAsStream("SqlMapConfig.xml");SqlSessionFactory build = new SqlSessionFactoryBuilder().build(stream);SqlSession sqlSession = build.openSession(true);userMapper = sqlSession.getMapper(UserMapper.class);}@Testpublic void testSave(){User user=new User();user.setUsername("tom");user.setPassword("123");userMapper.save(user);}@Testpublic void testUpdate(){User user = new User();user.setId(2);user.setUsername("dou");user.setPassword("bi");userMapper.update(user);}@Testpublic void testDelete(){User user = new User();userMapper.delete(3);}@Testpublic void testFindById(){User user=userMapper.findById(2);System.out.println(user);}@Testpublic void testFindAll(){List<User> list = userMapper.findAll();for (User user : list) {System.out.println(user);}}
}

6.结果

Mybatsi注解开发-基础操作相关推荐

  1. springmvc教程--注解开发基础详解

    springmvc教程系列 springmvc史上最好教程(2) springmvc史上最好教程(1) 一. 注解开发-基础 1.1 需求 使用springmvc+mybatis架构实现商品信息维护. ...

  2. Django开发基础----操作数据库

    Django中对数据库的操作是由Models来完成的 Models是什么? 通常,一个Model对应数据库的一张数据表 Django中Models以类的形式出现 它包含了一些基本字段以及数据的一些行为 ...

  3. SylixOS软件开发-基础操作

    学习sylixos SylixOS开发环境获取 代码结构 第一个APP 移植开源软件libcurl 下载libcurl的源码 预处理 新建移植工程 修改配置文件 编译 链接库 curl参考代码 核心文 ...

  4. 安全基础--29--Grails开发基础操作与配置

    本篇介绍了: 1.Grails安装 2.Grails项目的创建与运行 3.以指定环境运行Grails项目 4.Grails项目数据库搭建与配置 一.Grails安装 安装JDK 不介绍 安装IDEA ...

  5. Mybatis注解开发(一对一)

    其他代码访问:Mybatis注解开发基础操作 1.添加OrderMapper接口 public interface OrderMapper {// @Select("select *,o.i ...

  6. Mybatis注解开发笔记

    Mybatis注解开发(笔记) 欢迎来到菜鸟研究所 创建新的Maven项目 配置文件 prom.xml log4j.properties jdbcConfig.properties SqlMapCom ...

  7. JavaSSM笔记(一)Spring基础(JavaBean)(IoC理论)(AOP)(使用注解开发)

    在JavaWeb阶段,我们已经学习了如何使用Java进行Web应用程序开发,我们现在已经具有搭建Web网站的能力,但是,我们在开发的过程中,发现存在诸多的不便,在最后的图书管理系统编程实战中,我们发现 ...

  8. IOC操作Bean管理注解方式(完全注解开发)

    IOC操作Bean管理注解方式(完全注解开发) (1)创建配置类,替代xml配置文件 需要让Spring 把一个普通的类认为是配置类 结构图: SpringConfig类代码如下: package c ...

  9. Android游戏开发基础part4--Bitmap位图的渲染与操作

    游戏开发基础part4--Bitmap位图的渲染与操作 知识点1:Bitmap与BitmapFactory 如果想通过一张图片资源文件创建一个位图,则要通过位图工厂来索引图片资源文件,从而生成一张位图 ...

最新文章

  1. asp.net实现图片在线上传并在线裁剪
  2. 可以与空间耦合的神经网络分子微扰模型BeO
  3. 神器 JMH + Arthas 性能监控
  4. [蓝桥杯][算法训练VIP]麦森数(Java大数+快速幂)
  5. 关于Object.clone克隆方法的测试
  6. day27-python并发编程之多进程
  7. ViBe算法原理详解
  8. 烽火戏诸侯暂排第四,第四届橙瓜网络文学奖入围20年十佳仙侠大神
  9. 计算机专业论文推荐,计算机专业论文参考文献推荐
  10. 树莓派python串口收发数据
  11. cf----2019-10-12(Bus Video System,Bus Video System,Petya's Exams)
  12. 分享一个干货满满的网址导航
  13. [EE261学习笔记] 13.离散傅里叶逆变换及离散傅里叶变换的一些性质
  14. MySQL Replication 梳理详解
  15. python进阶数据分析_数据分析--Part 2: Python进阶
  16. 前端图片拖拽功能实现
  17. [论文笔记]EMNLP2019: Fine-Grained Entity Typing via Hierarchical Multi Graph Convolutional Networks
  18. Unity实用小工具或脚本—以对象方式访问MySql数据库
  19. 缉拿隐藏进程以及隐藏CPU利用率的进程
  20. 如何阅读英文原版教材

热门文章

  1. greenplum分区表查看所占空间大小
  2. Eliminate Witches!【2011年北京赛区正赛赛题-2】
  3. Office SharePoint Server 2007 规划和体系结构2
  4. php 主页子标题修改,关于有部分用户默认PC主页大标题标签修改无效的答疑.
  5. python怎么做q检验_统计学_Cochran’s Q Test(python代码实现)
  6. css中哪些属性与创建多列相关,css3中的新增属性有哪些
  7. php errorcode,php中pdo错误处理方法详解
  8. 没学c语言可以学python_学了Python,但是没有学c,直接去学c++是可行的吗?
  9. nginx无法访问index.html,ThinkPHP5 + nginx配置(index.php无法访问404)
  10. 汉字我在计算机中的处理过程,详细说明汉字在计算机中的处理流程以及汉字编码在处理过程中的转化关系...