select * from usertable
sql语句执行完毕后会把结果转为java对象(com.javase.entity.student)
查询结果为id username password sex email
相当于new student(id,username,password,sex,email);
如果有多个对象则生成List集合储存
返回简单类型
接口:
int SelectStudentsCount();
sql映射文件: 建议resultType写类型的全限定名称
select count(username) from usertable
测试:
@Test
public void testSelectsCount(){
SqlSession sqlSession = MyBatisUtil.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
int result = dao.SelectStudentsCount();
System.out.println("总人数为:" + result);
}
select id,username,password,sex,email from usertable
测试:
@Test
public void testSelectByResultMap() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
List studentList = dao.SelectStudentsByResultMap();
for (student stu:studentList) {
System.out.println(stu.toString());
}
}
结果:
也可以自己指定列名赋值
select id,username,password,sex,email from usertable
结果:
resultType解决列名和属性名不一致的第二种解决方案 设一个普通类
package com.javase.entity;
public class NewStudent {
private Integer id;
private String name;
private String psw;
private String sex;
private String email;
public NewStudent() {
}
public NewStudent(Integer id, String name, String psw, String sex, String email) {
this.id = id;
this.name = name;
this.psw = psw;
this.sex = sex;
this.email = email;
}
@Override
public String toString() {
return "NewStudent{" +
"id=" + id +
", name='" + name + ''' +
", psw='" + psw + ''' +
", sex='" + sex + ''' +
", email='" + email + ''' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPsw() {
return psw;
}
public void setPsw(String psw) {
this.psw = psw;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
sql映射文件: sql语句内的列名与转为的结果类型的属性名不一样时可以使用sql语句的别名as
select id,username as name,password as psw,sex,email from usertable
like模糊查询参数
模糊查询语法: 接口:
List SelectStudentsByLike(String name);
sql映射文件:
第一种写法:
select * from usertable where username like #{name}
第二种写法:
select * from usertable where username like "%" #{name} "%"
测试:
@Test
public void testSelectByLike() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
第一种sql映射文件写法的传参:
List studentList = dao.SelectStudentsByLike("%张%");
第二种传参
List studentList = dao.SelectStudentsByLike("张");
for (student stu:studentList) {
System.out.println(stu.toString());
}
}
结果:
动态sql
if标签
使用动态sql时需要使用java对象作为参数 接口:
List SelectSql(student stu);
sql映射文件:
select * from usertable
where
username = #{username}
or id < #{id}
如果第一个if不成立而第二个if成立的话sql会变成select * from usertable where or id < ?
这样会语法错误,改进代码where 1=1:
select * from usertable
where 1=1
username = #{username}
or id < #{id}
> 以上代码中or id < #{}中的<表示小于的意思,因为直接写<会造成标签冲突
mybatis的对应符号:
原符号
对应符号
<
<
>
>
<>
<>
&
&
’
'
"
"
测试:
@Test
public void testSelectSql(){
SqlSession sqlSession = MyBatisUtil.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
student stu = new student();
stu.setId(69);
stu.setUsername("艾伦");
List studentList = dao.SelectSql(stu);
for (student stuList:studentList) {
System.out.println(stuList.toString());
}
}
where
接口:
List SelectSqlWhere(student stu);
sql映射文件:
语法格式:
select * from usertable
username = #{username}
or id < #{id}
如果条件成立则会组装成select * from usertable WHERe username = ? or id < ?
注:如果第一个if不成立那where标签会自动删除第二个if的or[自动删除无效条件]
如果所有的if都不满足则where也没有
foreach
foreach标签主要是用在sql语句中in的语法
比如select * from usertable where id in (69,68,67)
foreach的底层原理:
目标语句:select * from usertable where id in(69,68,67);
//使用List集合存储in里面的参数
List list = new ArrayList();
list.add(69);
list.add(68);
list.add(67);
//创建基本语句用来字符串拼接的方式完成
String sql = "select * from usertable where id in";
StringBuilder builder = new StringBuilder();
//添加开始时的小括号
builder.append("(");
for(Integer i:list){
//这个i就是集合中的值69 68 67每个数字后面需要加,就是69,68,67,
builder.append(i).append(",");
}
//删除最后一个逗号
builder.deleteCharAt(builder.length()-1);
//添加结束的小括号
builder.append(")");
//执行字符串拼接
sql += builder;
System.out.println(sql);
结果:
foreach的语法: 接口:
//参数基础类型的写法:
List SelectForeach(List list);
//参数对象类型的写法:
List SelectForeach(List list);
sql映射文件:
list集合中是基础类型的写法:
select * from usertable where id in
#{id}
list集合是对象类型的写法:
select * from usertable where username in
#{stu.username}
conllection:表示接口中的方法的参数的类型,如果是数组使用array,如果是集合使用list(小写)
item:自定义,表示数组和集合的成员的变量
open:循环开始的符号 比如原理中的拼接字符串开始字符是(
close:循环结束的符号 结束字符是)
这两个是(69,68,67)中的小括号()
separator:表示循环每一个数据中分割的符号 在(69,68,67)中的逗号,
测试:
@Test
public void testForeach(){
SqlSession sqlSession = MyBatisUtil.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
//基础类型的写法:
List list = new ArrayList<>();
list.add(69);
list.add(68);
list.add(67);
//对象类型的写法:
List stuList = new ArrayList<>();
stuList.add(new student(null,"爱丽丝",null,null,null));
stuList.add(new student(null,"艾伦",null,null,null));
stuList.add(new student(null,"张三",null,null,null));
List studentList = dao.SelectForeach(list);//对象类型是stuList
for (student stu:studentList) {
System.out.println(stu.toString());
}
}
基础类型的结果:
对象类型的结果:
sql代码片段
代码片段是用来复用语句的 语法:
先使用sql语句
在使用
sql映射文件:
select * from usertable
username = #{username}
or id < #{id}
测试:
@Test
public void testSelectSqlWhere(){
SqlSession sqlSession = MyBatisUtil.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
student stu = new student();
stu.setUsername("艾伦");
stu.setId(69);
List studentList = dao.SelectSqlWhere(stu);
for (student stuList:studentList) {
System.out.println(stuList.toString());
}
}