1、首先建立一个测试的dao

 public interface IStudentDao {

     // 根据姓名查询
List<Student> selectStudentsByName(String name);
}

2、对这个dao进行实现

 public class StudentDaoImpl implements IStudentDao {

     private SqlSession sqlSession;

     // 根据姓名查询
public List<Student> selectStudentsByName(String name) {
List<Student> students = null; try {
sqlSession = MybatisUtil.getSqlSession();
students = sqlSession.selectList("selectStudentsByName", name);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
return students;
}
}

3、utils的书写

 public class MybatisUtil {
private static SqlSessionFactory sqlSessionFactory; public static SqlSession getSqlSession() {
try {
if (sqlSessionFactory == null) {
// 读取配置文件
InputStream inputStream = Resources
.getResourceAsStream("Mybatis.xml");
// 创建工厂
sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
}
} catch (IOException e) {
e.printStackTrace();
}
return sqlSessionFactory.openSession();
}
}

4、配置文档

(1)Mybatis.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> <properties resource="jdbc.properties" /> <!-- 别名标签 -->
<typeAliases>
<typeAlias type="com.liuya.demo.mybaits.crud.pojo.Student"
alias="Student" />
</typeAliases> <!-- 配置运行的数据库环境 -->
<environments default="mysqlenvironment">
<environment id="mysqlenvironment">
<!-- 連接池在本地连接中使用,在SSM中不用,用C3P0和DBCP -->
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments> <!-- 连接映射文件 -->
<mappers>
<!-- 最终使用的都是package -->
<mapper resource="com/liuya/demo/mybaits/crud/mapper/StudentMapper.xml" />
</mappers>
</configuration>

(2)properties的书写

#
##正式服务器
driver = com.mysql.jdbc.Driver
url = jdbc:mysql://127.0.0.1:3306/crud?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
username = root
password = 123456

(3)mapper的书写

<?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"> <!-- 配置数据库和实体类的字段 -->
<mapper namespace="test"> <!-- 根据姓氏模糊查询 -->
<select id="selectStudentsByName" resultType="Student">
select
NAME,AGE,SCORE from STUDENT where NAME like concat ('%',#{ooo},'%')
</select> </mapper>

5、测试的书写

 public class MyTest {

     private IStudentDao idao;

     @Before
public void before() {
idao = new StudentDaoImpl();
}
// 根据name查询一个学生
@Test
public void testSelectStudentsByName() {
System.out.println("开始查询学生");
List<Student> students = idao.selectStudentsByName("张");
for (Student student : students) {
System.out.println(student);
}
System.out.println("查询学生成功");
}
}

这就是一个完整的select查询的书写,重点在于mapper中的select书写

写法一(比较复杂):

<!-- 根据姓氏模糊查询 -->
<select id="selectStudentsByName" resultType="Student">
select
NAME,AGE,SCORE from STUDENT where NAME like concat ('%',#{ooo},'%')
</select>
写法二(一般使用的比较多):
<!-- 根据姓氏模糊查询 -->
<select id="selectStudentsByName" resultType="Student">
select
NAME,AGE,SCORE from STUDENT where NAME like '%'#{ooo}'%'
</select>
写法三(不建议使用,会有sql注入问题和加载速度慢的问题):
<!-- 根据姓氏模糊查询 -->
<select id="selectStudentsByName" resultType="Student">
select
NAME,AGE,SCORE from STUDENT where NAME like '%&{value}%'
</select>
like后的括号值,只能是value。

Mybatis的select查询的三种方式的更多相关文章

  1. MyBatis实现模糊查询的几种方式

    在学习MyBatis过程中想实现模糊查询,可惜失败了.后来上百度上查了一下,算是解决了.记录一下MyBatis实现模糊查询的几种方式. 数据库表名为test_student,初始化了几条记录,如图: ...

  2. Android之——ContentResolver查询的三种方式

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47785491 今天做到一个小项目.查询手机中短信的信息,当然得去系统暴露出来的数据 ...

  3. 五 Mybatis一对一关联查询的两种方式(基于resultType&基于resultMap)

    关联查询: 一个用户对应多个订单,一个订单只有一个用户 订单关联用户:两种方式 一:基于resultTYpe,一个与表关系一样的pojo实现 主表订单,从表用户 首先要有一个与关联查询表关系一样的po ...

  4. mybatis查询的三种方式

    查询最需要关注的问题:①resultType自动映射,②方法返回值:  interface EmpSelectMapper: package com.atguigu.mapper; import ja ...

  5. Spring之jdbcTemplate:查询的三种方式(单个值、单个对象、对象集合)

    JdbcTemplateDemo2.java package helloworld.jdbcTemplate; import org.springframework.jdbc.core.JdbcTem ...

  6. mybatis批量添加数据的三种方式

    原文地址:https://www.cnblogs.com/gxyandwmm/p/9565002.html

  7. android sqlite使用之模糊查询数据库数据的三种方式

    android应用开发中常常需要记录一下数据,而在查询的时候如何实现模糊查询呢?很少有文章来做这样的介绍,所以这里简单的介绍下三种sqlite的模糊查询方式,直接上代码把: package com.e ...

  8. 表单模糊查询的三种简单方式(springboot-h2-mybatis)

    前几天运营提到说后台管理系统有几个地方想要模糊查询..   想了下是简单的,就是要注意以前方法的被调用情况,进行增量改动,以免牵一发而动全身.整理一波记录下(本次案例是按名字模糊查询学生信息). 三种 ...

  9. vue+element ui项目总结点(一)select、Cascader级联选择器、encodeURI、decodeURI转码解码、mockjs用法、路由懒加载三种方式

    不多说上代码: <template> <div class="hello"> <h1>{{ msg }}</h1> <p> ...

随机推荐

  1. QT4.8.5环境移植到嵌入式平台

    QT4.8.5环境移植到嵌入式平台 参考:Qt移植到ARM Linux教程 http://www.veryarm.com/930.html 清除配置: sudo make confclean 配置: ...

  2. Python 定期检查Build_setting的编译情况

    #!/usr/bin/python #_*_ coding:utf-8 _*_ import time from email.MIMEText import MIMEText from email.M ...

  3. [正经分析] DAG上dp两种做法的区别——拓扑序与SPFA

    在下最近刷了几道DAG图上dp的题目. 要提到的第一道是NOIP原题<最优贸易>.这是一个缩点后带点权的DAG上dp,它同时规定了起点和终点. 第二道是洛谷上的NOI导刊题目<最长路 ...

  4. cookies封装

    /** * @author wxf */var cookie=new function(){ this.set=function(name,value,hours){ var life=new Dat ...

  5. 十二.jQuery源码解析之.eq().first().last().slice()

    eq(index):将集合中的索引为index的元素提取出来. first():返回集合中的第一个元素. .last():防护集合中的最后一个元素. .slice(start[,end]):返回集合中 ...

  6. Solr分组聚合查询之Facet

    摘要: Solr的分组聚合是一个笼统的概念,目的就是把查询结果做分类,有多种方式可以做到很类似的结果.也正是由于它们的不同表现,可以适合于多种场景. 何为Facet Facet是一种手段,用来将搜索结 ...

  7. python实现进度条--主要用在上传下载文件

    在python中进行socket上传文件的时候使用进度条,然后在网上找了好久,找寻相关的进度的条的使用,看了几个,发现总是无法进行调用,主要原因是在进行上传文件的时候,每次传送的数据量是固定的,数据的 ...

  8. Sonar及其eclipse插件的安装 详细 http://www.importnew.com/10017.html

    参考:http://www.importnew.com/10017.html

  9. java后端时间处理工具类,返回 "XXX 前" 的字符串

    转自:https://www.cnblogs.com/devise/p/9974672.html 我们经常会遇到显示 "某个之间之前" 的需求(比如各种社交软件,在回复消息时,显示 ...

  10. 关闭 iTunes 自动同步

    关闭 iTunes 自动同步 iTunes>Edit>perererset>device>Prevent iPods,iPhone, and iPads from syncin ...