1.mybatis中的动态SQL
Mybatis动态SQL是用来根据不同的条件动态生成SQL语句的一项功能。它可以实现:
1.条件查询
Mybatis动态SQL允许开发人员根据不同的条件动态构建查询语句。例如,通过使用标签,可以根据某些条件包含或排除查询的某些部分,从而实现更灵活的查询。2.拼接SQL语句
动态SQL还可以用于拼接SQL语句。这使得开发人员可以在运行时根据具体需求构建SQL语句,从而适应更复杂的业务逻辑。3.提高代码重用性
通过使用动态SQL,可以减少重复的SQL代码,提高代码的可重用性。例如,可以定义可重复使用的SQL片段,并通过标签在不同的查询中重用它们。4.增强SQL语句的灵活性
Mybatis动态SQL通过使用诸如、、等标签提供了对SQL语句的精细控制。这增强了SQL语句的灵活性,允许开发人员根据具体的业务需求精确地控制查询的行为。5.避免SQL注入风险
正确使用Mybatis动态SQL可以减少SQL注入的风险。通过使用参数化查询和避免字符串拼接,可以增强应用程序的安全性。
MyBatis的动态SQL是指对sql语句进行灵活操作;它是基于OGNL表达式,通过if,choose,when,otherwise,trim,where,set,foreach标签,对SQL语句进行灵活拼接、组装,从而提高开发人员的效率。
bid, bname, price
delete from t_mvc_book
where bid = #{bid,jdbcType=INTEGER}
insert into t_mvc_book (bid, bname, price
)
values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
)
insert into t_mvc_book
bid,
bname,
price,
#{bid,jdbcType=INTEGER},
#{bname,jdbcType=VARCHAR},
#{price,jdbcType=REAL},
update t_mvc_book
bname = #{bname,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL},
where bid = #{bid,jdbcType=INTEGER}
update t_mvc_book
set bname = #{bname,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL}
where bid = #{bid,jdbcType=INTEGER}
bid, bname, price
delete from t_mvc_book
where bid = #{bid,jdbcType=INTEGER}
insert into t_mvc_book (bid, bname, price
)
values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
)
insert into t_mvc_book
bid,
bname,
price,
#{bid,jdbcType=INTEGER},
#{bname,jdbcType=VARCHAR},
#{price,jdbcType=REAL},
update t_mvc_book
bname = #{bname,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL},
where bid = #{bid,jdbcType=INTEGER}
update t_mvc_book
set bname = #{bname,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL}
where bid = #{bid,jdbcType=INTEGER}
bid, bname, price
delete from t_mvc_book
where bid = #{bid,jdbcType=INTEGER}
insert into t_mvc_book (bid, bname, price
)
values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
)
insert into t_mvc_book
bid,
bname,
price,
#{bid,jdbcType=INTEGER},
#{bname,jdbcType=VARCHAR},
#{price,jdbcType=REAL},
update t_mvc_book
bname = #{bname,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL},
where bid = #{bid,jdbcType=INTEGER}
update t_mvc_book
set bname = #{bname,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL}
where bid = #{bid,jdbcType=INTEGER}
bid, bname, price
delete from t_mvc_book
where bid = #{bid,jdbcType=INTEGER}
insert into t_mvc_book (bid, bname, price
)
values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
)
insert into t_mvc_book
bid,
bname,
price,
#{bid,jdbcType=INTEGER},
#{bname,jdbcType=VARCHAR},
#{price,jdbcType=REAL},
update t_mvc_book
bname = #{bname,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL},
where bid = #{bid,jdbcType=INTEGER}
update t_mvc_book
set bname = #{bname,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL}
where bid = #{bid,jdbcType=INTEGER}
package com.zlj.mapper;
import com.zlj.model.Book;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BookMapper {
int deleteByPrimaryKey(Integer bid);
int insert(Book record);
int insertSelective(Book record);
Book selectByPrimaryKey(Integer bid);
int updateByPrimaryKeySelective(Book record);
int updateByPrimaryKey(Book record);
List selectByBids(@Param("bids") List bids);
}
package com.zlj.biz.impl;
import com.zlj.biz.BookBiz;
import com.zlj.mapper.BookMapper;
import com.zlj.model.Book;
import java.util.List;
/**
* @author zlj
* @create 2023-08-21 8:49
*/
public class BookBizImpl implements BookBiz {
private BookMapper bookMapper;
public BookMapper getBookMapper() {
return bookMapper;
}
@Override
public int deleteByPrimaryKey(Integer bid) {
return bookMapper.deleteByPrimaryKey(bid);
}
@Override
public int insert(Book record) {
return bookMapper.insert(record);
}
@Override
public int insertSelective(Book record) {
return bookMapper.insertSelective(record);
}
@Override
public Book selectByPrimaryKey(Integer bid) {
return bookMapper.selectByPrimaryKey(bid);
}
@Override
public int updateByPrimaryKeySelective(Book record) {
return bookMapper.updateByPrimaryKeySelective(record);
}
@Override
public int updateByPrimaryKey(Book record) {
return bookMapper.updateByPrimaryKey(record);
}
@Override
public BookMapper setBookMapper(BookMapper mapper) {
return this.bookMapper = mapper;
}
@Override
public List selectByBids(List bids) {
return bookMapper.selectByBids(bids);
}
}
package com.zlj.biz;
import com.zlj.mapper.BookMapper;
import com.zlj.model.Book;
import java.util.List;
public interface BookBiz {
int deleteByPrimaryKey(Integer bid);
int insert(Book record);
int insertSelective(Book record);
Book selectByPrimaryKey(Integer bid);
int updateByPrimaryKeySelective(Book record);
int updateByPrimaryKey(Book record);
BookMapper setBookMapper(BookMapper mapper);
List selectByBids(List bids);
}
package com.zlj.demo;
import com.zlj.biz.impl.BookBizImpl;
import com.zlj.mapper.BookMapper;
import com.zlj.model.Book;
import com.zlj.utils.SessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
/**
* @author zlj
* @create 2023-08-21 9:07
*/
public class demo1 {
private SqlSession sqlSession;
private BookBizImpl bookBiz;
@Before
public void a(){
System.out.println("执行测试方法之前会执行的初始化代码块");
sqlSession= SessionUtil.openSession();
BookBizImpl bookBiz=new BookBizImpl();
BookMapper mapper=sqlSession.getMapper(BookMapper.class);
bookBiz.setBookMapper(mapper);
this.bookBiz=bookBiz;
}
@After
public void b(){
System.out.println("执行测试方法之后会执行");
sqlSession.commit();
}
@Test
public void test1(){
System.out.println("测试方法。。。");
Book book = bookBiz.selectByPrimaryKey(33);
System.out.println(book);
}
@Test
public void test2(){
bookBiz.deleteByPrimaryKey(34);
}
@Test
public void testById(){
List bids = Arrays.asList(new Integer[]{56, 57, 58, 59, 60});
bookBiz.selectByBids(bids).forEach(System.out::print);//jdk1.8新特性
// for (Book selectByBid : bookBiz.selectByBids(bids)) {
// System.out.println(selectByBid);
// }
}
}
2.mybatis中的模糊查询(三种:like1,like2,like3)
mybatis中#与$的区别
1.$是占位符传参(like2),#是预处理SQL(like1,like3)
2.外在形式,$传参不带引号,#传参自带引号
3.$传参存在sql注入,#不存在
4.$可以用来做动态列,完成动态sql的开发
bid, bname, price
delete from t_mvc_book
where bid = #{bid,jdbcType=INTEGER}
insert into t_mvc_book (bid, bname, price
)
values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
)
insert into t_mvc_book
bid,
bname,
price,
#{bid,jdbcType=INTEGER},
#{bname,jdbcType=VARCHAR},
#{price,jdbcType=REAL},
update t_mvc_book
bname = #{bname,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL},
where bid = #{bid,jdbcType=INTEGER}
update t_mvc_book
set bname = #{bname,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL}
where bid = #{bid,jdbcType=INTEGER}
bid, bname, price
delete from t_mvc_book
where bid = #{bid,jdbcType=INTEGER}
insert into t_mvc_book (bid, bname, price
)
values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
)
insert into t_mvc_book
bid,
bname,
price,
#{bid,jdbcType=INTEGER},
#{bname,jdbcType=VARCHAR},
#{price,jdbcType=REAL},
update t_mvc_book
bname = #{bname,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL},
where bid = #{bid,jdbcType=INTEGER}
update t_mvc_book
set bname = #{bname,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL}
where bid = #{bid,jdbcType=INTEGER}
bid, bname, price
delete from t_mvc_book
where bid = #{bid,jdbcType=INTEGER}
insert into t_mvc_book (bid, bname, price
)
values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
)
insert into t_mvc_book
bid,
bname,
price,
#{bid,jdbcType=INTEGER},
#{bname,jdbcType=VARCHAR},
#{price,jdbcType=REAL},
update t_mvc_book
bname = #{bname,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL},
where bid = #{bid,jdbcType=INTEGER}
update t_mvc_book
set bname = #{bname,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL}
where bid = #{bid,jdbcType=INTEGER}
bid, bname, price
delete from t_mvc_book
where bid = #{bid,jdbcType=INTEGER}
insert into t_mvc_book (bid, bname, price
)
values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
)
insert into t_mvc_book
bid,
bname,
price,
#{bid,jdbcType=INTEGER},
#{bname,jdbcType=VARCHAR},
#{price,jdbcType=REAL},
update t_mvc_book
bname = #{bname,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL},
where bid = #{bid,jdbcType=INTEGER}
update t_mvc_book
set bname = #{bname,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL}
where bid = #{bid,jdbcType=INTEGER}
package com.zlj.mapper;
import com.zlj.model.Book;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BookMapper {
int deleteByPrimaryKey(Integer bid);
int insert(Book record);
int insertSelective(Book record);
Book selectByPrimaryKey(Integer bid);
int updateByPrimaryKeySelective(Book record);
int updateByPrimaryKey(Book record);
List selectByBids(@Param("bids") List bids);
List like1(@Param("bname") String bname);
List like2(@Param("bname") String bname);
List like3(@Param("bname") String bname);
}
package com.zlj.biz;
import com.zlj.mapper.BookMapper;
import com.zlj.model.Book;
import java.util.List;
public interface BookBiz {
int deleteByPrimaryKey(Integer bid);
int insert(Book record);
int insertSelective(Book record);
Book selectByPrimaryKey(Integer bid);
int updateByPrimaryKeySelective(Book record);
int updateByPrimaryKey(Book record);
BookMapper setBookMapper(BookMapper mapper);
List selectByBids(List bids);
List like1(String bname);
List like2(String bname);
List like3(String bname);
}
package com.zlj.biz.impl;
import com.zlj.biz.BookBiz;
import com.zlj.mapper.BookMapper;
import com.zlj.model.Book;
import java.util.List;
/**
* @author zlj
* @create 2023-08-21 8:49
*/
public class BookBizImpl implements BookBiz {
private BookMapper bookMapper;
public BookMapper getBookMapper() {
return bookMapper;
}
@Override
public int deleteByPrimaryKey(Integer bid) {
return bookMapper.deleteByPrimaryKey(bid);
}
@Override
public int insert(Book record) {
return bookMapper.insert(record);
}
@Override
public int insertSelective(Book record) {
return bookMapper.insertSelective(record);
}
@Override
public Book selectByPrimaryKey(Integer bid) {
return bookMapper.selectByPrimaryKey(bid);
}
@Override
public int updateByPrimaryKeySelective(Book record) {
return bookMapper.updateByPrimaryKeySelective(record);
}
@Override
public int updateByPrimaryKey(Book record) {
return bookMapper.updateByPrimaryKey(record);
}
@Override
public BookMapper setBookMapper(BookMapper mapper) {
return this.bookMapper = mapper;
}
@Override
public List selectByBids(List bids) {
return bookMapper.selectByBids(bids);
}
@Override
public List like1(String bname) {
return bookMapper.like1(bname);
}
@Override
public List like2(String bname) {
return bookMapper.like2(bname);
}
@Override
public List like3(String bname) {
return bookMapper.like3(bname);
}
}
package com.zlj.demo;
import com.zlj.biz.impl.BookBizImpl;
import com.zlj.mapper.BookMapper;
import com.zlj.model.Book;
import com.zlj.utils.SessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
/**
* @author zlj
* @create 2023-08-21 9:07
*/
public class demo1 {
private SqlSession sqlSession;
private BookBizImpl bookBiz;
@Before
public void a(){
System.out.println("执行测试方法之前会执行的初始化代码块");
sqlSession= SessionUtil.openSession();
BookBizImpl bookBiz=new BookBizImpl();
BookMapper mapper=sqlSession.getMapper(BookMapper.class);
bookBiz.setBookMapper(mapper);
this.bookBiz=bookBiz;
}
@After
public void b(){
System.out.println("执行测试方法之后会执行");
sqlSession.commit();
}
@Test
public void test1(){
System.out.println("测试方法。。。");
Book book = bookBiz.selectByPrimaryKey(33);
System.out.println(book);
}
@Test
public void test2(){
bookBiz.deleteByPrimaryKey(34);
}
@Test
public void testById(){
List bids = Arrays.asList(new Integer[]{56, 57, 58, 59, 60});
bookBiz.selectByBids(bids).forEach(System.out::print);//jdk1.8新特性
// for (Book selectByBid : bookBiz.selectByBids(bids)) {
// System.out.println(selectByBid);
// }
}
@Test
public void testlike1(){
bookBiz.like1("%圣墟%").forEach(System.out::print);//jdk1.8新特性
}
@Test
public void testlike2(){
bookBiz.like2("%圣墟%").forEach(System.out::print);//jdk1.8新特性
}
@Test
public void testlike3(){
bookBiz.like3("%圣墟%").forEach(System.out::print);//jdk1.8新特性
}
}
3.mybatis中结果映射
使用mybatis的各种场景,返回的结果是多样的,resultType/resultMa
p
1.返回单表对应的实体类,仅有一个查询结果,可以用resultType,可以用resultMap(selectByPrimaryKey)
2.返回单表对应的实体类,有多个查询结果,可以用resultType,可以用resultMap(list01,list02)
3.返回多表对应的实体类,仅有一个查询结果,通常用resultType,也可以用resultMap(list03)
4.返回多表对应的实体类,有多个查询结果,通常用resultType,也可以用resultMap (list04)
5.返回单个列段,仅有一个查询结果,就用resultType(bname1)
6.返回单个列段,有多个查询结果,就用resultType(bname2)
bid, bname, price
delete from t_mvc_book
where bid = #{bid,jdbcType=INTEGER}
insert into t_mvc_book (bid, bname, price
)
values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
)
insert into t_mvc_book
bid,
bname,
price,
#{bid,jdbcType=INTEGER},
#{bname,jdbcType=VARCHAR},
#{price,jdbcType=REAL},
update t_mvc_book
bname = #{bname,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL},
where bid = #{bid,jdbcType=INTEGER}
update t_mvc_book
set bname = #{bname,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL}
where bid = #{bid,jdbcType=INTEGER}
package com.zlj.mapper;
import com.zlj.model.Book;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface BookMapper {
int deleteByPrimaryKey(Integer bid);
int insert(Book record);
int insertSelective(Book record);
Book selectByPrimaryKey(Integer bid);
int updateByPrimaryKeySelective(Book record);
int updateByPrimaryKey(Book record);
List selectByBids(@Param("bids") List bids);
List like1(@Param("bname") String bname);
List like2(@Param("bname") String bname);
List like3(@Param("bname") String bname);
List list01();
List list02();
Map list03(Map map);
List
package com.zlj.biz;
import com.zlj.mapper.BookMapper;
import com.zlj.model.Book;
import java.util.List;
import java.util.Map;
public interface BookBiz {
int deleteByPrimaryKey(Integer bid);
int insert(Book record);
int insertSelective(Book record);
Book selectByPrimaryKey(Integer bid);
int updateByPrimaryKeySelective(Book record);
int updateByPrimaryKey(Book record);
BookMapper setBookMapper(BookMapper mapper);
List selectByBids(List bids);
List like1(String bname);
List like2(String bname);
List like3(String bname);
List list01();
List list02();
Map list03(Map map);
List
package com.zlj.biz.impl;
import com.zlj.biz.BookBiz;
import com.zlj.mapper.BookMapper;
import com.zlj.model.Book;
import java.util.List;
import java.util.Map;
/**
* @author zlj
* @create 2023-08-21 8:49
*/
public class BookBizImpl implements BookBiz {
private BookMapper bookMapper;
public BookMapper getBookMapper() {
return bookMapper;
}
@Override
public int deleteByPrimaryKey(Integer bid) {
return bookMapper.deleteByPrimaryKey(bid);
}
@Override
public int insert(Book record) {
return bookMapper.insert(record);
}
@Override
public int insertSelective(Book record) {
return bookMapper.insertSelective(record);
}
@Override
public Book selectByPrimaryKey(Integer bid) {
return bookMapper.selectByPrimaryKey(bid);
}
@Override
public int updateByPrimaryKeySelective(Book record) {
return bookMapper.updateByPrimaryKeySelective(record);
}
@Override
public int updateByPrimaryKey(Book record) {
return bookMapper.updateByPrimaryKey(record);
}
@Override
public BookMapper setBookMapper(BookMapper mapper) {
return this.bookMapper = mapper;
}
@Override
public List selectByBids(List bids) {
return bookMapper.selectByBids(bids);
}
@Override
public List like1(String bname) {
return bookMapper.like1(bname);
}
@Override
public List like2(String bname) {
return bookMapper.like2(bname);
}
@Override
public List like3(String bname) {
return bookMapper.like3(bname);
}
@Override
public List list01() {
return bookMapper.list01();
}
@Override
public List list02() {
return bookMapper.list02();
}
@Override
public Map list03(Map map){
return bookMapper.list03(map);
}
@Override
public List
package com.zlj.demo;
import com.zlj.biz.impl.BookBizImpl;
import com.zlj.mapper.BookMapper;
import com.zlj.model.Book;
import com.zlj.utils.SessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author zlj
* @create 2023-08-21 9:07
*/
public class demo1 {
private SqlSession sqlSession;
private BookBizImpl bookBiz;
@Before
public void a(){
System.out.println("执行测试方法之前会执行的初始化代码块");
sqlSession= SessionUtil.openSession();
BookBizImpl bookBiz=new BookBizImpl();
BookMapper mapper=sqlSession.getMapper(BookMapper.class);
bookBiz.setBookMapper(mapper);
this.bookBiz=bookBiz;
}
@After
public void b(){
System.out.println("执行测试方法之后会执行");
sqlSession.commit();
}
@Test
public void test1(){
System.out.println("测试方法。。。");
Book book = bookBiz.selectByPrimaryKey(33);
System.out.println(book);
}
@Test
public void test2(){
bookBiz.deleteByPrimaryKey(34);
}
@Test
public void testById(){
List bids = Arrays.asList(new Integer[]{56, 57, 58, 59, 60});
bookBiz.selectByBids(bids).forEach(System.out::print);//jdk1.8新特性
// for (Book selectByBid : bookBiz.selectByBids(bids)) {
// System.out.println(selectByBid);
// }
}
@Test
public void testlike1(){
bookBiz.like1("%圣墟%").forEach(System.out::print);//jdk1.8新特性
}
@Test
public void testlike2(){
bookBiz.like2("%圣墟%").forEach(System.out::print);//jdk1.8新特性
}
@Test
public void testlike3(){
bookBiz.like3("%圣墟%").forEach(System.out::print);//jdk1.8新特性
}
@Test
public void testlist01(){
bookBiz.list01().forEach(System.out::print);//jdk1.8新特性
}
@Test
public void testlist02(){
bookBiz.list02().forEach(System.out::print);//jdk1.8新特性
}
@Test
public void testlist03(){
Map map=new HashMap();
map.put("sid","01");
map.put("cid","01");
System.out.println(bookBiz.list03(map));
}
@Test
public void testlist04(){
Map map=new HashMap();
map.put("sid","01");
map.put("cid","01");
bookBiz.list04(map).forEach(System.out::print);
}
@Test
public void testbname1(){
System.out.println(bookBiz.bname1(66));
}
@Test
public void testbname2(){
bookBiz.bname2("圣墟").forEach(System.out::print);
}
}