1.需求

  将下边的功能实现Dao:

    根据用户id查询一个用户信息

    根据用户名称模糊查询用户信息列表

    添加用户信息

2. 原始Dao开发方法需要程序员编写Dao接口和Dao实现类

3.User.xml映射文件的内容为:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace:命名空间,做Sql隔离 -->
<!-- 在mapper标签中要写很多sql语句。在开发项目的过程中有很多人都会写Sql
语句,在最后整合的时候可能会重复。现在我们使用命名空间开进行隔离,比如zhangsan
写的select * from user,我们可以写为:zhangsan:select * from user来进行标识。 -->
<mapper namespace="test">
<!-- id:sql语句的唯一标识 test:findUserById就可以唯一标识sql语句
paremeterType:指定传入的参数类型
resultSetType:返回值结果类型
#{}占位符:起到占位的左永刚,如果传入的基本类型{String,long,double,int boolean等},那么
#{}中的变量名称可以随意写。
-->
<select id="findUserById" parameterType="java.lang.Integer" resultType="com.huida.po.User">
<!-- select语句返回的是user对象,所以resultType中写User类的全路径 -->
select * from user where id=#{id}
</select> <!-- 模糊查询
返回结果可能为集合;如果返回结果为集合,调用selectList(),并且返回类型配置集合中的泛型。集合中存放的就是User,所以返回类型就是User类型
${}拼接符:字符串原样拼接。如果传入的基本类型{String,long,double,int boolean等},那么
${}中的变量名必须是value.
-->
<select id="findUserByUsername" parameterType="java.lang.String" resultType="com.huida.po.User">
<!-- 模糊查询的占位符需要进行拼接 -->
select * from user where username like "%${value}%"
</select> <!-- 添加
添加操作返回值可有可无
#{}:如果传入的是po类型,那么#{}中的变量名称必须是po中对应的属性
-->
<!-- <select id="insertUser" parameterType="com.huida.po.User">
insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})
</select> -->
<!-- 自增主键返回 -->
<insert id="insertUser" parameterType="com.huida.po.User">
<!-- selectKey将主键返回,需要再返回 -->
<!-- keyProperty:将返回的主键放入传入参数的id中保存。也就是最后的结果通过id保存起来
order:当前函数相对于insert语句的执行顺序,在insert前执行的是before,在insert之后执行的是after
resultType:id的类型
-->
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
select LAST_INSERT_ID()
</selectKey>
insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address});
</insert>
</mapper>

4.Dao接口

package com.huida.dao;

import java.util.List;

import com.huida.po.User;

public interface UserDao {

    public User findUserById(Integer id);
public List<User> findUserByUserName(String name);
}

5.Dao接口实现方法

package com.huida.dao;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory; import com.huida.po.User; public class UserDaoImpl implements UserDao { //拿到工厂,才能得到Session,才能对sql语句进行处理
private SqlSessionFactory factory;
//通过构造方法将工厂传入,也就是注入
public UserDaoImpl(SqlSessionFactory factory) {
super();
this.factory = factory;
}
@Override
public User findUserById(Integer id) {
//创建session
//sqlSession是线程不安全的,它的最佳使用是在方法体内
SqlSession openSession=factory.openSession();
User user=openSession.selectOne("test.findUserById", id);
return user;
}
//模糊查询
@Override
public List<User> findUserByUserName(String name) {
//每个方法创建一个sqlSession
SqlSession openSession=factory.openSession();
List<User> list=openSession.selectList("test.findUserByUsername",name);
return list;
} }

6.Dao测试

  创建一个JUnit的测试类,对UserDao进行测试。  

  这里我们使用了一个小技巧,因为没执行一个方法都需要创建工厂,所以我们可以将创建工厂的方法拿出来,放在所有测试方法之前,并在前面加一个@Before的注解,这样就会在测试方法前执行这个方法。不能将建了SqlSession的方法提到前面,因为SqlSession的作用范围应该是在方法内。

package com.huida.test;

import java.io.InputStream;
import java.util.List; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test; import com.huida.dao.UserDao;
import com.huida.dao.UserDaoImpl;
import com.huida.po.User; public class UserDaoTest { private SqlSessionFactory factory=null;
//before的作用:在测试方法前执行这个方法
@Before
public void init() throws Exception{
// 通过流将核心配置文件读取进来
InputStream inputStream = Resources.getResourceAsStream("config/SqlMapConfig.xml");
// 通过核心配置文件输入流来创建工厂
factory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void testFindById(){
UserDao userDao=new UserDaoImpl(factory);
User user=userDao.findUserById(1);
System.out.println(user);
} @Test
public void testFindByUserName(){
UserDao userDao=new UserDaoImpl(factory);
List<User> list=userDao.findUserByUserName("li");
System.out.println(list);
}
}

使用mybatis开发Dao的原始方法,实现根据用户id查询一个用户信息 、根据用户名称模糊查询用户信息列表 、添加用户信息等功能的更多相关文章

  1. MyBatis开发Dao的原始Dao开发和Mapper动态代理开发

    目录 咳咳...初学者看文字(Mapper接口开发四个规范)属实有点费劲,博主我就废了点劲做了如下图,方便理解: 原始Dao开发方式 1. 编写映射文件 3.编写Dao实现类 4.编写Dao测试 Ma ...

  2. 【mybatis基础】mybatis开发dao两种方法

    mybatis是一个支持普通SQL查询,存储过程和高级映射的优秀的持久层的框架,是apache下的顶级项目.mybatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.mybat ...

  3. MyBatis学习--mybatis开发dao的方法

    简介 使用Mybatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper接口开发方法. 主要概念介绍: MyBatis中进行Dao开发时候有几个重要的类,它们是SqlSessionFac ...

  4. 四 mybatis开发dao的方法

    mybatis开发dao的方法 1.1     SqlSession使用范围 1.1.1     SqlSessionFactoryBuilder //以流的方式读取总的配置文件 Reader rea ...

  5. MyBatis开发Dao层的两种方式(原始Dao层开发)

    本文将介绍使用框架mybatis开发原始Dao层来对一个对数据库进行增删改查的案例. Mapper动态代理开发Dao层请阅读我的下一篇博客:MyBatis开发Dao层的两种方式(Mapper动态代理方 ...

  6. 使用mybatis开发dao方法

    使用mybatis开发dao的时候, 主要涉及到SqlSessionFactoryBuilder.SqlSessionFactory.SqlSession 这三个类 现在将这三个类的使用方法简单的说下 ...

  7. MyBatis开发Dao

    MyBatis开发Dao有两种方法: 1.原始Dao开发方法,就是程序需要编写Dao的接口和Dao的实现类. 2.MyBatis的mapper接口(相当于Dao接口)代理开发方法.(更重要) ---- ...

  8. MyBatis开发Dao层的两种方式(Mapper动态代理方式)

    MyBatis开发原始Dao层请阅读我的上一篇博客:MyBatis开发Dao层的两种方式(原始Dao层开发) 接上一篇博客继续介绍MyBatis开发Dao层的第二种方式:Mapper动态代理方式 Ma ...

  9. 使用mybatis开发dao问题总结

    代码片段: @Override public User getUserById(Integer id) { SqlSession sqlSession = sqlSessionFactory.open ...

随机推荐

  1. React Native入门指南

    转载自:http://www.jianshu.com/p/b88944250b25 前言 React Native 诞生于 2015 年,名副其实的富二代,主要使命是为父出征,与 Apple 和 Go ...

  2. 第10课 struct和union分析

    struct的小秘密:空结构体占多大内存呢? 直观的答案有两种: 1.空结构体的大小为0 2.结构体本来就是为了将不同的变量集合在一起使用的,定义空结构体会导致编译错误 实例分析: #include ...

  3. linux vi常用操作

    1.基本操作 进入vi vi 或者 vim 进入一个文件或者新建一个文件 例如:vim 11.txt vi有3种模式 一般模式:刚进入时.按esc时. 编辑模式:按下字母[i, I, o, O, a, ...

  4. hdu 3613 Best Reward

    After an uphill battle, General Li won a great victory. Now the head of state decide to reward him w ...

  5. nginx brotli 压缩试用

    brotli 的压缩比相对gzip 有好多提升 测试试用docker 测试代码 https://github.com/rongfengliang/rollup-babel-demolibrary 运行 ...

  6. Cockpit 容器&&kubernetes 管理可视化工具

    安装 在k8s 的master 上 yum install -y cockpit cockpit-ws cockpit-kubernetes cockpit-bridge cockpit-dashbo ...

  7. flash exe to flv swf

    一般婚纱视频的文件都是用adobe软件转化为exe文件,所以只能用adobe flash打开,想上传到网上供朋友欣赏,却发现格式不对,那么我们可以用以下的方法将exe格式的视频转化为swf和flv等视 ...

  8. bzoj 3730 震波——动态点分治+树状数组

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3730 查询一个点可以转化为查询点分树上自己到根的路径上每个点对应范围答案.可用树状数组 f ...

  9. linux Posix 信号量 三 (经典例子)

    本文将阐述一下信号量的作用及经典例子,当中包括“<越狱>寄信”,“家庭吃水果”,“五子棋”,“接力赛跑”,“读者写者”,“四方恋爱”等 首先,讲 semWait操作(P操作)和semSig ...

  10. 解决Python代码编码问题 SyntaxError: Non-UTF-8 code starting with '\xc1'

    本文转载自:http://blog.csdn.net/wyb_hardworking/article/details/19562971 程序中出现中文,运行的时候出现如下错误: SyntaxError ...