Mybatis的select查询的三种方式
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查询的三种方式的更多相关文章
- MyBatis实现模糊查询的几种方式
在学习MyBatis过程中想实现模糊查询,可惜失败了.后来上百度上查了一下,算是解决了.记录一下MyBatis实现模糊查询的几种方式. 数据库表名为test_student,初始化了几条记录,如图: ...
- Android之——ContentResolver查询的三种方式
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47785491 今天做到一个小项目.查询手机中短信的信息,当然得去系统暴露出来的数据 ...
- 五 Mybatis一对一关联查询的两种方式(基于resultType&基于resultMap)
关联查询: 一个用户对应多个订单,一个订单只有一个用户 订单关联用户:两种方式 一:基于resultTYpe,一个与表关系一样的pojo实现 主表订单,从表用户 首先要有一个与关联查询表关系一样的po ...
- mybatis查询的三种方式
查询最需要关注的问题:①resultType自动映射,②方法返回值: interface EmpSelectMapper: package com.atguigu.mapper; import ja ...
- Spring之jdbcTemplate:查询的三种方式(单个值、单个对象、对象集合)
JdbcTemplateDemo2.java package helloworld.jdbcTemplate; import org.springframework.jdbc.core.JdbcTem ...
- mybatis批量添加数据的三种方式
原文地址:https://www.cnblogs.com/gxyandwmm/p/9565002.html
- android sqlite使用之模糊查询数据库数据的三种方式
android应用开发中常常需要记录一下数据,而在查询的时候如何实现模糊查询呢?很少有文章来做这样的介绍,所以这里简单的介绍下三种sqlite的模糊查询方式,直接上代码把: package com.e ...
- 表单模糊查询的三种简单方式(springboot-h2-mybatis)
前几天运营提到说后台管理系统有几个地方想要模糊查询.. 想了下是简单的,就是要注意以前方法的被调用情况,进行增量改动,以免牵一发而动全身.整理一波记录下(本次案例是按名字模糊查询学生信息). 三种 ...
- vue+element ui项目总结点(一)select、Cascader级联选择器、encodeURI、decodeURI转码解码、mockjs用法、路由懒加载三种方式
不多说上代码: <template> <div class="hello"> <h1>{{ msg }}</h1> <p> ...
随机推荐
- 图解VS2005之单元测试
据说VS2005里即提供了测试功能,可是对于像我或者我们这样的开发人或团队真还没有进化到用测试这块.一直以来都是手工测试或等到用户发现问题.今天在网上找了一个介绍单元测试的WORD文档,按里面说的做了 ...
- bzoj2337 XOR和路径
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2337 首先:因为是异或和,所以可以考虑每一位考虑. 就在每一位上求一下该位是1的概率,乘以1 ...
- c# 爬虫(二) 模拟登录
有了上一篇的介绍,这次我们来说说模拟登录,上一篇见 :c# 爬虫(一) HELLO WORLD 原理 我们知道,一般需要登录的网站,服务器和客户端都会有一段时间的会话保持,而这个会话保持是在登录时候建 ...
- PHP 通过实现 Iterator(迭代器)接口来读取大文件文本
读了NGINX的access日志,bnb_manage_access.log(31M) 和 bnb_wechat_access.log(50M) 附上代码: <?php /** * User: ...
- erlang otp中的socket参数设置
抄自http://www.zackzod.me/2012/10/24/socket-options-in-erlang-otp.html Erlang的inet模块里提供了对Socket进行一系列参数 ...
- python学习日志
马上就中秋节,想着再学点新的知识,本来想去继续研究前端知识来着,但是内个烦人的样式css还有js搞的有点脑壳头,以后就主学后端吧,要去死了前端这条心了? 那么寻寻觅觅就入坑最近几年大热的python吧 ...
- Bootstrap-CL:导航栏
ylbtech-Bootstrap-CL:导航栏 1.返回顶部 1. Bootstrap 导航栏 导航栏是一个很好的功能,是 Bootstrap 网站的一个突出特点.导航栏在您的应用或网站中作为导航页 ...
- 使用GridFsTemplate在Mongo中存取文件
Maven依赖(还有一些springboot需要的) <parent> <groupId>org.springframework.boot</groupId> ...
- sqoop导出到mysql中文乱码问题总结、utf8、gbk
sqoop导出到mysql中文乱码问题总结.utf8.gbk 今天使用sqoop1.4.5版本的(hadoop使用cdh5.4)因为乱码问题很是头痛半天.下面进行一一总结 命令: [root@sdzn ...
- SQL常用条件操作符
1.SQL的六类内容: (1)数据定义语言(DDL): 创建.删除表结构的语句,包括Create.Drop (2)数据控制语言(DCL): 为定义数据访问及修改权限而实现的语句,包括Grant.Rev ...