Mybatis注解CRUD之对象、变量、集合三种方式封装数据的实现 体验

创建User对象

public class User implements Serializable{
    private Integer id;
    private String username;
    private String address;
    private String sex;
    private Date birthday;
    //getter和setter和toString方法已略
}

创建dao包UserDao.java

public interface UserDao {

    //传递2个条件或者多个条件,使用@Param注解获取传递数据的对应值,注意value的值必须有传递的参数名相同
@Select(value = "select * from user where username like #{name} and sex = #{sex}")
List<User> findByNameAndSex(@Param(value = "name") String username,@Param(value = "sex") String sex);

//使用map集合传递值
@Select(value = "select * from user where username like #{name} and sex = #{sex}")
List<User> findByNameAndSex2(Map map);

//使用user对象传递数据,直接在#{}中括号内写user对象的变量名
@Select(value = "select * from user where username like #{username} and sex = #{sex}")
List<User> findByUserNameAndSex(User user);

}

创建测试类TestAnnotation.java

public class TestAnnotation{
    InputStream is = null;
    SqlSession sqlSession = null;
    UserDao userDao = null;
    AccountDao accountDao = null;
    @Before
    public void init() throws IOException {
        // 1:加载配置文件和映射文件(读取InputStream)
        is = Resources.getResourceAsStream("sqlMapConfig.xml");
        // 2:获取SqlSessionFactory(相当于DataSource)
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        // 3:获取SqlSession(相当于Connection)
        sqlSession = sqlSessionFactory.openSession();
        // 4:使用SqlSession获取UserDao(代理)
        userDao = sqlSession.getMapper(UserDao.class);
        accountDao = sqlSession.getMapper(AccountDao.class);
    }

    @After
    public void destory() throws IOException {
        is.close();
        //sqlSession.close();
        sqlSession.commit();
    }
    
    @Test //方案1:使用变量传递参数
public void testfindByNameAndSex(){
    String username = "%王%";
    String sex = "女";
    List<User> list = userDao.findByNameAndSex(username, sex);
    for (User user : list) {
        System.out.println(user);
    }
}

@Test //方案2:使用user对象传递参数
public void testByUserNameAndSex(){
    String username = "%王%";
    String sex = "女";
    User user = new User();
    user.setUsername(username);
    user.setSex(sex);
    List<User> list = userDao.findByUserNameAndSex(user);
    for (User user1 : list) {
        System.out.println(user1);
    }
}

@Test//方案3:使用map集合封装数据实现
public void findByMapNameAndSex(){
    String username = "%王%";
    String sex = "女";
    Map<String,Object> map = new HashMap<String ,Object>();
    map.put("name",username);
    map.put("sex",sex);
    List<User> list = userDao.findByNameAndSex2(map);
    for (User user : list) {
        System.out.println(user);
    }
}
 }

注意:本人使用maven创建的项目

pom.xml配置

<dependencies>
        <!--mybatis核心包-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <!--mysql连接包-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.18</version>
        </dependency>
        <!-- 日志坐标 -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <!--测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>
    </dependencies>

建表语句:

CREATE TABLE `user` (
  `id` int(11) NOT NULL auto_increment,
  `username` varchar(32) NOT NULL COMMENT '用户名称',
  `birthday` datetime default NULL COMMENT '生日',
  `sex` char(1) default NULL COMMENT '性别',
  `address` varchar(256) default NULL COMMENT '地址',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert  into `user`(`id`,`username`,`birthday`,`sex`,`address`) values (22,'老王','2018-02-27 17:47:08','男','北京'),(18,'二王','2018-03-02 15:09:37','女','北京金燕龙'),(17,'三王','2018-03-04 11:34:34','女','北京金燕龙'),(20,'传智播客','2018-03-04 12:04:06','男','北京金燕龙'),(18,'老王','2018-03-07 17:37:26','男','北京'),(18,'小马宝莉','2018-03-08 11:44:00','女','北京修正');

数据库连接、映射mappers请自行配置.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值