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. 走在linux 的路上

    终于现在不看鸟哥的私房菜基础篇了,以后再慢慢看,像我这种初学者,感觉还是不太适合看鸟哥的私房菜. 于是从图书馆借了本书继续学习我的linux. 这样看着linux容易多了,进而熟悉了几个命令:ls c ...

  2. Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.elasticsearch.threadpool.ThreadPool

    springboot中遇到的, 将guava添加到项目中即可.(当时添加的是guava 18)

  3. MyBatis_Study_002(进阶,增删改查)

    源码:https://github.com/carryLess/mbtsstd-002.git 1.主配置文件 <?xml version="1.0" encoding=&q ...

  4. ambassador 学习一基本试用

    安装使用docker for mac Without RBAC 安装ambassador 安装 kubectl apply -f https://getambassador.io/yaml/ambas ...

  5. Eclipse设置Courier New字体

    使用Eclipse我们会发现在字体设置里找不到钟爱的Courier New字体.其实这个字体不是没有,只是没有显示而已,它其实隐藏起来了,只需几步便可让其现原形—— 1.找到Eclipse设置字体的地 ...

  6. PDO exec 执行时出错后如果修改数据会被还原?

    PDO exec 执行时出错后如果修改数据会被还原? 现象 FastAdmin 更新了 1127 版本,但是使用在线安装方式出现无法修改管理员密码的问题. 一直是默认的 admin 123456 密码 ...

  7. Windows 10 上的 Git 如何清除密码? Git Credential Manager for Windows

    Windows 10 上的 Git 如何清除密码? 因为一台新的电脑是 Windows 10 在第一次使用 Git 要求输入密码时把密码给输错了. 之前提交都是说 Token 错了,不再出现提示密码. ...

  8. [LeetCode系列] 最长回文子串问题

    给定字符串S, 找到其子串中最长的回文字符串.   反转法: 反转S为S', 找到其中的最长公共子串s, 并确认子串s在S中的下标iS与在S'中的下标iS'是否满足式: length(S) = iS ...

  9. 显示等待WebDriverWait

    显示等待:WebDriverWait 等待页面加载完成,找到某个条件发生后再继续执行后续代码,如果超过设置时间检测不到则抛出异常 WebDriverWait(driver, timeout, poll ...

  10. genmotion 安装 app 报错 This application is't compatible with your mobile phone解决办法

    请下载这个文件:http://pan.baidu.com/s/1jIyMNbg(一个zip包) 将这个zip包拖放到genymotion的屏幕中,安装,然后重启就行了 我安装的Samsung Gala ...