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> ...
随机推荐
- oracle之 关闭透明大页
方法一: 1.设置/etc/grub.conf文件,添加 transparent_hugepage=never ,在系统启动是禁用 [root@hbdw1 ~]# cat /etc/grub.conf ...
- Android 基础题目
1. BroadcastReceiver 在UI thread? BroadcastReceiver 总是在UI thread, If you register your BroadcastRecei ...
- SpringCloud初体验:一、Eureka 服务的注册与发现
Eureka :云端服务发现,一个基于 REST 的服务,用于定位服务,以实现云端中间层服务发现和故障转移. Eureka 可以大致理解为 房产中介 和 房东 的关系,房东想让租客租房子,首先要把房子 ...
- thinkpad t420安装debian需要注意的细节
关闭双显卡,使用集成显卡,32位可以用独显跑起来,但是64位的wheezy只能兼容集成显卡,不知道为啥,否则会在某些usb插入/或者拔出来以后,重启提示 failed to execute /lib/ ...
- OGNL遍历list、map的常用三种方法
package com.mylife.po; public class User { private String uname; private String pwd; public String g ...
- websocket之四:WebSocket 的鉴权授权方案
引子 WebSocket 是个好东西,为我们提供了便捷且实时的通讯能力.然而,对于 WebSocket 客户端的鉴权,协议的 RFC 是这么说的: This protocol doesn’t pres ...
- Check failed: mdb_status == 0 (2 vs. 0) No such file or directory
运行 ./examples/mnist/train_lenet.sh 时,碰到了这个问题. 一定是路径问题!!!仔细查看prototxt文件里面的各种路径!!! 解决方案: 把.prototxt里 ...
- FTP和TCP、UDP
应用:TFTP客户端 1. TFTP协议介绍 TFTP(Trivial File Transfer Protocol,简单文件传输协议) 是TCP/IP协议族中的一个用来在客户端与服务器之间进行简单文 ...
- VisualStudio2012轻松把JSON数据转换到POCO的代码(转)
VisualStudio2012轻松把JSON数据转换到POCO的代码 在Visual Studio 2012中轻松把JSON数据转换到POCO的代码,首先你需要安装Web Essentials 20 ...
- springmvc怎么在启动时自己执行一个线程
之前使用springmvc的时候,都是写好controller和对应的数据库操作. 外界发请求的时候,controller进行一堆操作后返回相应的json数据. 似乎springmvc就是外界驱动的一 ...